Search code examples
c++winapilocale

How to make GetLocaleInfoEx return its data in a specific language?


I'm using GetLocaleInfoEx to obtain the "display name" of a locale : however, I would like the "display name" to be returned in the language that's used in that locale.

The code amounts to this (ex-MX is used here as an example) :

const wchar_t localeName[] = L"es-MX";
wchar_t buf[1024];
GetLocaleInfoEx(localeName, LOCALE_SLOCALIZEDDISPLAYNAME, buf, sizeof(buf) / sizeof(*buf));
std::cout << to_utf8(buf) << '\n';

However, the returned string is

Hiszpański (Meksyk)

presumably because my system locale, and the display language, is set to Polish.

What can I do to force GetLocaleInfoEx to return the string in Spanish (in this particular case)? I would expect it to be something like

Español (México)

What I've tried so far (both things were done before calling GetLocaleInfoEx) :

  1. Converting the locale name to a LCID and setting the thread locale :
SetThreadLocale(LocaleNameToLCID(localeName, 0));
  1. Setting the preferred UI language :
const wchar_t preferredLangs[] = L"es-MX\0";
unsigned long numLangsSet;
SetProcessPreferredUILanguages(MUI_LANGUAGE_NAME, preferredLangs, &numLangsSet);
if (numLangsSet != 1) {
  return 1;
}

However, none of these methods seem to work : the returned string is still Hiszpański (Meksyk).

What else can be done to set the desired language of GetLocaleInfoEx's output?


Solution

  • You can use LOCALE_SNATIVEDISPLAYNAME instead of LOCALE_SLOCALIZEDDISPLAYNAME iin your call to GetLocaleInfoEx.