Convert LCID string to language

In my application, I have to query for System and User Locale and return them as strings (for example, en-US instead of language codes). I can get the LCID for these languages ​​from the GetSystemDefaultLCID () and GetUserDefaultLCID () functions, but with this I come across the transition from LCID to language strings.

My application should work on Windows XP too, so I cannot use the LCIDToLocaleName () API. The only thing I managed to get the locale name for was using the GetLocaleInfo () API, passing LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT as LCID and LOCALE_SISO639LANGNAME as LCType. My code looks something like this: Query for System Locale

int localeBufferSize = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, NULL, 0); char *sysLocale = new char[localeBufferSize]; GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, sysLocale,localeBufferSize); 

But the value I get is just the name of the language (only en en-US). To get the full language, I need to call GetLocaleInfo (), passing LOCALE_SISO3166CTRYNAME twice as LCType a second time, and then add two values. Is there a better way to do this?

+4
source share
1 answer

Sorry to say, but no. Only two (or more) letter codes are standardized, their concatenation is absent. But! There is no need to allocate a dynamic buffer. Quoted from MSDN:

The maximum number of characters allowed for this string is nine, including the terminating null character.

Reading this, it sounds redundant to dynamically allocate the necessary buffer, so if you really want to keep it clean, you can do something like this:

 char buf[19]; int ccBuf = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, buf, 9); buf[ccBuf++] = '-'; ccBuf += GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME, buf+ccBuf, 9); 
+9
source

All Articles