C++ Win32 API equivalent of CultureInfo.TwoLetterI

2019-02-20 03:47发布

问题:

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.

回答1:

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

Call GetLocaleInfo with the LOCALE_SISO639LANGNAME parameter.



回答2:

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.



回答3:

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.