by Michael S. Kaplan, published on 2008/03/07 08:01 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2008/03/07/7994143.aspx
Please read the disclaimer; content not approved by Microsoft!
Dujuan's question to the larger distribution list was easy enough:
Hi,
I want to get the language name in the OS, such as string ”en-US” or “it-IT” .
I use the GetLocaleInfo(GetSystemDefaultLCID(),
LOCALE_SNAME,
pszLocale,
k)
In vista, the pszLocale get the language name correctly. However, the LOCALE_SNAME can only be used in Vista.
When I run the program Under XP, the pszLocale get nothing!
So, is there some other ways I can get the language name in XP?
Thank you for help!
Well, first we'll deal with the XP results -- easy enough by looking at the LCTYPE information referenced from GetLocaleInfo:
LOCALE_SNAME
Windows Vista and later: Locale name, a multipart tag to uniquely identify this locale. The maximum number of characters allowed for this string is 85, including a terminating null character. The tag is based on the language tagging conventions of RFC 4646. The pattern to use is described in Locale Names. See also Locale Name Constants
Well, there is an obvious reason for the function to not fill in the value -- the particular LOCALE_SNAME value is not supported in XP.
Of course the ERROR_INVALID_PARAMETER last error that the GetLocaleInfo call would cause could also be considered an important clue, though as we know no one ever checks return values for failure! :-)
But let's look beyond that for a moment.
The code Dujuan alludes to is relying on a call to GetSystemDefaultLCID, which returns the LCID associated with the default system locale (known in XP as the language for non-Unicode programs. Now it is not 100% clear what Dujuan is looking for in the "language name in the OS" but it almost certainly is not a changeable/configurable setting, right?
It sounds more like the GetSystemDefaultUILanguage might be a better function to rely on, it gets the install language of Windows -- which sounds like it might be closer to the "language name in the OS" in any case. Though that setting has all of the limitations that I mentioned back when I explained how useless InstalledUICulture is.
Though without more information about exactly what the goal of having the information is, there is no way to really know what the right answer might be here....
Which is the real answer here -- until the exact requirements are understood, it is easy though ultimately ineffectual to pick holes in all of the problems encountered in attempts to solve the problem!
This blog brought to you by Ꮐ (U+13c0, aka CHEROKEE LETTER NAH)
KJK::Hyperion on 7 Mar 2008 9:53 PM:
You can use MLang to resolve a locale id to a RFC 1766/3066/4646 identifier, with LcidToRfc1766
Michael S. Kaplan on 7 Mar 2008 10:18 PM:
Although somewhat true, there are limitations to it (such as the fact that in some versions the data is badly out of date, the extra component dependency, he fact that MLang itself hasn't seen much in the way of updates since RFC1766 was the only RFC available!).