How to change webpage render language?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to change webpage render language?

Postby ysaliens » Mon Oct 02, 2017 1:48 pm

I have followed the CEF simple tutorial found at https://bitbucket.org/chromiumembedded/cef/wiki/Tutorial

My question is about changing the language of rendered webpages. I can pass the --lang="de-DE" command line flag to change the locale.
This changed the right click menus but not the rendering language of webpages. When Windows is set to another language, say...fr-FR, the CEF simple app still does not pick up the language and still uses en-US to render (with or without --lang).

I read a few topics on this forum about this but all of them seemed to hard-code languages in CefSettings::AcceptLanguageList.

Screenshot of current behavior with --lang command line parameter - left is Internet Explorer, Right is CEF simple. Windows is set to fr_FR.
cef.PNG
cef.PNG (410.65 KiB) Viewed 22026 times


Is there a way to make the CEF pick up the language from the operating system or have it be passed in via a command-line argument?
How can you set request.getLocale() to return the Windows language (or passed in command line parameter) rather than the default en_US?
How would this be done?
ysaliens
Techie
 
Posts: 20
Joined: Mon Oct 02, 2017 1:08 pm

Re: How to change webpage render language?

Postby magreenblatt » Mon Oct 02, 2017 4:00 pm

Your application can read the OS language settings and set CefSettings.locale and CefSettings.accept_language_list accordingly before calling CefInitialize. See documentation on those parameters for usage.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to change webpage render language?

Postby ysaliens » Tue Oct 03, 2017 2:30 pm

Solved this for Windows. Both the locale and rendered pages now load in the default Windows locale.
Modified cefsimple_win.cc, code is below:

Code: Select all
//Set Language based on Windows OS locale
  LANGID langId = GetUserDefaultLangID();
  wchar_t languageCode[100];
  GetLocaleInfo(langId, LOCALE_SISO639LANGNAME, languageCode, 100);
  wchar_t countryCode[100];
  GetLocaleInfo(langId, LOCALE_SISO3166CTRYNAME, countryCode, 100);
  std::wstring lang(languageCode);
  lang += L"_" + std::wstring(countryCode);
  CefString(&settings.locale).FromWString(lang);
  CefString(&settings.accept_language_list).FromWString(lang);
ysaliens
Techie
 
Posts: 20
Joined: Mon Oct 02, 2017 1:08 pm

Re: How to change webpage render language?

Postby ysaliens » Wed Oct 04, 2017 12:53 pm

How can I access a command line argument passed to cefsimple.exe in cefsimple_win.cc?
In simple_app.cc, I can access them with:

Code: Select all
  CefRefPtr<CefCommandLine> command_line = CefCommandLine::GetGlobalCommandLine();
  std::string language = command_line->GetSwitchValue("language");


Code above I added a "--language=" variable that I wish to use to set both locale and accept_language_list.
I can access this variable in simple_app.cc but the code above crashes if put into cefsimple_win.cc

I need to use the variable in cefsimple_win.cc as that is where the CefSettings settings is initialized and used. Is there a simple way to do that I'm not seeing?
ysaliens
Techie
 
Posts: 20
Joined: Mon Oct 02, 2017 1:08 pm

Re: How to change webpage render language?

Postby magreenblatt » Wed Oct 04, 2017 1:21 pm

The global CommandLine object can only be used after CefInitialize. You should create a CommandLine object instead.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to change webpage render language?

Postby ysaliens » Wed Oct 04, 2017 1:29 pm

Thanks, though I'm not sure how can I do that. Would a CommandLine object have the same structure?
ysaliens
Techie
 
Posts: 20
Joined: Mon Oct 02, 2017 1:08 pm

Re: How to change webpage render language?

Postby magreenblatt » Wed Oct 04, 2017 2:03 pm

You can find examples of CefCommandLine::CreateCommandLine usage in sample applications.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to change webpage render language?

Postby ysaliens » Wed Oct 04, 2017 3:45 pm

Thank you! Got it all working for Windows.
Added a new "--language=en-GB" command-line argument. If specified it will use it for locale and render language. If not specified, it will detect the Windows language and use that.

cefsimple_win.cc
Code: Select all
   // Parse command-line arguments
   CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
   command_line->InitFromString(::GetCommandLineW());

   // Specify CEF global settings here.
   CefSettings settings;
   
   // Language not specified, use system language
   std::string language = command_line->GetSwitchValue("language");
   if (language.empty()){
      LANGID langId = GetUserDefaultLangID();
      wchar_t languageCode[100];
      GetLocaleInfo(langId, LOCALE_SISO639LANGNAME, languageCode, 100);
      wchar_t countryCode[100];
      GetLocaleInfo(langId, LOCALE_SISO3166CTRYNAME, countryCode, 100);
      std::wstring lang(languageCode);
      lang += L"_" + std::wstring(countryCode);
      CefString(&settings.locale).FromWString(lang);
      CefString(&settings.accept_language_list).FromWString(lang);
   }
   // Language is specified via "--language=", use it
   else {
      cef_string_utf8_to_utf16(language.c_str(), language.size(), &settings.locale);
      cef_string_utf8_to_utf16(language.c_str(), language.size(), &settings.accept_language_list);
   }
ysaliens
Techie
 
Posts: 20
Joined: Mon Oct 02, 2017 1:08 pm

Re: How to change webpage render language?

Postby sjames1958 » Tue Feb 08, 2022 4:32 pm

Is it possible to set the language without restarting CEF. We have a switch in our UI that changes our language display, but I would like the accept-language header to reflect that as well
sjames1958
Mentor
 
Posts: 60
Joined: Sun Jun 22, 2014 7:41 am

Re: How to change webpage render language?

Postby magreenblatt » Tue Feb 08, 2022 5:39 pm

sjames1958 wrote:Is it possible to set the language without restarting CEF. We have a switch in our UI that changes our language display, but I would like the accept-language header to reflect that as well

See viewtopic.php?f=6&t=15869&p=37923 for an example.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 38 guests