views:

176

answers:

3

The .NET framework makes it easy to get information about various locales; the Win32 C++ APIs are a bit harder to figure out.

Is there an equivalent function in Win32 to get the two-letter ISO language name given an integer locale ID?

In C# I'd do:

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(1034);
string iso = ci.TwoLetterISOLanguageName;
// iso == "es" now.

The code needs to run on XP and newer.

A: 

This is one area where Win API has evolved a lot since XP. I don't think you're going to find a function to do it that's available all the way back to XP. I believe the .NET framework stuff has its own built-in tables (at least for pre-Vista versions). GetUserDefaultLocaleName isn't even available on XP, and that doesn't do exactly what you want, and even if it did, it probably wouldn't be as complete on XP as it is on newer versions.

You might need to include your own table.

Adrian McCarthy
This answer doesn't actually provide a solution, however one does exist. See my answer below.
Brian Gillespie
A: 

See the GetLocaleInfo function. There are 2 LCType values you may be interested in: LOCALE_SABBREVCTRYNAME, and LOCALE_SABBREVLANGNAME. I did a quick test on Windows 7, and both returned 3 character strings, even though ISO 3166 uses 2 characters. The LOCALE_SABBREVLANGNAME documentation states it starts out with the 2 character ISO 3166 code and adds a third character for the sublanguage.

Trevor Balcom
Trevor, thanks for your answer - it gave me the keywords to find the correct solution.Apparently, and this is important for some languages, the LOCALE_SABBREVLANGNAME works the way you describe MOST of the time:http://blogs.msdn.com/michkap/archive/2005/02/17/375235.aspxCalling GetLocaleInfo with the LOCALE_SISO639LANGNAME parameter gets what I want. http://msdn.microsoft.com/en-us/library/dd373848(VS.85).aspx
Brian Gillespie
A: 

Thanks to Trevor for directing me toward this answer in an earlier reply.

Call GetLocaleInfo with the LOCALE_SISO639LANGNAME parameter.

Brian Gillespie
Um, this doesn't answer the question that was asked. This option requests the three-letter language abbreviations rather than the two-letter ones. This option is not available on Windows XP, only Vista and up.
Adrian McCarthy
Adrian, you are incorrect on all counts. If you read more carefully, you'll see that LOCALE_SISO639LANGNAME is supported back to Windows NT 4 and Win98, and provides two-character abbreviations. The LOCALE_SISO639LANGNAME2 parameter (note the trailing "2") is Vista only, and provides 3-letter abbreviations.Please undo your down vote, as it is unwarranted.
Brian Gillespie