views:

1093

answers:

1

I'm fairly new to localized programming, and I'm trying to figure out how to set the proper initial locale for a newly-launched unmanaged C++ application (from within the app).

As far as I can tell, new applications start with the C locale, rather than the proper regional locale (English, German, etc). So what I need to do is call setlocale( LC_ALL, "???" ), but I'm not sure how to get the correct value for the second argument. It will be something like "English" or "German:Germany" - basically whatever locale was set by the user via the Regional and Language Options control panel. Just to be clear, I'm not looking for how to format the locale string, I'm looking for the correct locale string for the computer where the app is running.

I'm guessing that there's some Win32 API that would give me this, or perhaps a registry key that would contain the proper value. Does anybody know what I should be doing?

+4  A: 

setlocale() is C, not C++. I vaguely remember seeing interference between the two on VC6, but that was a bug. Normally, setlocale() affects the behavior of the C functions only.

In C++, localization is controlled by the std::locale class. By default, locale-sensitive operations use the global locale, which is obtained by default-constructing a locale object, and can be set with std::locale::global(const std::locale&).

Constructing a locale object with an empty string (std::locale("")) creates a locale corresponding to the program's environment.

At program startup, the global locale is the "C" or "Classic" locale. To set the global locale to the program's environment locale (which I guess is what you're asking), you thus write:

std::locale::global(std::locale(""));

For example, my regional settings are currently set to French(Canada). Running this:

int main(void)
{
  std::cout << std::locale().name() << std::endl;
  std::locale::global(std::locale(""));
  std::cout << std::locale().name() << std::endl;
  std::locale::global(std::locale("C"));
  std::cout << std::locale().name() << std::endl;
  return 0;
}

prints:

C
French_Canada.1252
C
Éric Malenfant
Great, this is exactly what I needed. It looks like you can also pass the name() of a given C++ locale to setlocale(), for setting the C locale as well.
Charlie