Win32 API for getting the language(localization info) of the OS?
Asked Answered
M

3

16

Can anybody please help me with how to get the language(english,chinese etc) of Windows OS through win32 API(C/C++)??

Thanks, Sourabh

Mashhad answered 24/8, 2009 at 13:51 Comment(3)
It's just not that simple. There is no such thing as "the language"; there could be multiple langauges in concurrent use. Can you be more precise? Tip: read Michael Kaplan's blog ["Sorting it All out"](blogs.msdn.com/michkap/)Ransack
@Ransack You should link directly to the relevant article.Tso
@SadlyNot: That's a bit outdated by now (covers only XP, which is of course almost EOL)Ransack
G
8

You can get the default user locale (which I think is what you're asking) using GetUserDefaultLCID. This will give you an ID which can be used to determine the culture. See here for a table containing IDs and the cultures they represent.

For Vista or Windows 7, Microsoft recommend GetUserDefaultLocaleName.

Gerfalcon answered 24/8, 2009 at 14:11 Comment(0)
C
30

If you're asking about "Which language the OS menus and dialogs are dispalyed in" (i.e. which MUI - Multilingual User Interface kit - is installed), use the following:

More info:

Colchis answered 24/8, 2009 at 14:34 Comment(5)
And then use GetLocaleInfo with LOCALE_SENGLANGUAGE to obtain ISO 639 from LANGID.Variety
I suspect this is nearer to what the OP was asking, and a much more comprehensive answer than mine.Gerfalcon
One more question to that: I just want to set the FIELDSEPARATOR in the locale settings via win api call from Delphi. Can anyone here help? ThanksHyps
The returned language ID by these can then be split into the primary language identifier using PRIMARYLANGID and the regional identifier using SUBLANGID. These can then be processed as needed. A list of available primary and sub language identifiers can be found here: Language Identifier Constants and Strings.Thorazine
Hey, since using the language ids (which is what the functions in this answer do) is now deprecated and shouldn't be used for new systems, this is a bit outdated. I did a new more detailed answer on how to get the UI and locale language using language names or the old language ids each; this might be better for new projectsAnthologize
G
8

You can get the default user locale (which I think is what you're asking) using GetUserDefaultLCID. This will give you an ID which can be used to determine the culture. See here for a table containing IDs and the cultures they represent.

For Vista or Windows 7, Microsoft recommend GetUserDefaultLocaleName.

Gerfalcon answered 24/8, 2009 at 14:11 Comment(0)
A
1

Since both of the answers are older and use the deprecated language ids, this is a new answer how to solve this problem nowadays:

First of all, the locale and the UI language aren't the same thing. They correlate often but they don't necessarily need to be the same. The locale is based on the geographic location and language of the user, while the UI language determines which should be used by user interfaces.

UI language

Using language names (Windows Vista+)

To get the UI language, you should probably use the GetUserPreferredUILanguages function. It returns either language ids or the language names, depending on the arguments. Since Microsoft deprecated the use of language ids, you should use the function directly to get the language name(s).

If you want to get other information instead of the language name, you can use GetLocaleInfoEx. The first parameter is the just obtained language name and you can select the LCTYPE, which is the information you want to retrieve, which can be all kinds of stuff, e.g. the country, the currency, or the date format.

Using language ids (Windows 2000+)

However, these functions are only available to Windows Vista and later. If you need to have compatibility to older systems, you need to use the (now discouraged) language ids. With GetUserDefaultUILanguage, you can get the current users UI language as a language id. To convert that language id to the language name or other information, use GetLocaleInfoW. It works like GetLocaleInfoEx, just that the first parameter is the language id instead of the language name.

Since the LOCALE_SNAME LCTYPE (which returns the language name) has only been introduced with the concept of language names in Windows Vista, you need to use the LCTYPEs LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME (which returns something like en and US, respectively) and build your language name with those.

Locale language

Using language names (Windows Vista+)

Getting the user locale is easier: You just need to use the GetLocaleInfoEx function. And instead of a language name as the first parameter, you can just use the constant LOCALE_NAME_USER_DEFAULT, which leads to GetLocaleInfoEx directly returning info about the current user locale. To get the language name, use LOCALE_SNAME as the LCTYPE.

Using language ids (Windows 2000+)

As with the UI language, using the language names is only supported from Windows Vista on. If you need to have compatibility to the older systems, you can use the GetLocaleInfoW function. And instead of a language id as the first parameter, you can just use the constant LOCALE_USER_DEFAULT which leads to GetLocaleInfoW directly returning info about the current user locale.

Since the LOCALE_SNAME LCTYPE (which returns the language name) has only been introduced with the concept of language names in Windows Vista, you need to use the LCTYPEs LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME (which returns something like en and US, respectively) and build your language name with those.

Anthologize answered 7/4, 2023 at 14:12 Comment(1)
Please DO NOT USE LOCALE for language detection!! Do not confuse the two, they are different concepts. It is a very common error, and a source of frustration for the users who have different language and locale.Overtop

© 2022 - 2024 — McMap. All rights reserved.