Detecting OS language within browser

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.

Detecting OS language within browser

Postby newaira » Tue Sep 22, 2015 10:08 am

So I'm trying to figure out what is the best approach to detecting what is the current OS language. From what I've read and tried so far, it looks like CEF starts up without in any way trying to figure out what is the currently selected OS language. It always defaults to --lang=en-US.

I can get the lang value from Javascript by calling navigator.language, so it's good that I can access it.

Now, what would be the best approach to set the --lang/CefSettings.locale to the correct OS language? Do I need to make native system calls myself in order to get the current language, or is there something that's already built-in?
newaira
Techie
 
Posts: 19
Joined: Sun Apr 19, 2015 7:43 pm

Re: Detecting OS language within browser

Postby magreenblatt » Tue Sep 22, 2015 11:28 am

newaira wrote: Do I need to make native system calls myself in order to get the current language, or is there something that's already built-in?

You will need to make the system call yourself.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Detecting OS language within browser

Postby newaira » Wed Sep 23, 2015 9:32 am

In case someone needs to do the same, here is how I did it on windows. You might want to add some error checks.

Would it be useful to include this kind of logic in the core rather than always defaulting to en_US?

Code: Select all
CefSettings settings;

// set language based on user's default lang
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);


I'm also going to do it for OS X.
newaira
Techie
 
Posts: 19
Joined: Sun Apr 19, 2015 7:43 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot], leeroy and 62 guests