setlocale in Mac OS X doesn't work

1.5k Views Asked by At

I'm working on cross-platform C++ application. It is multilingual and have to show all messages in usera language. In order to detect locale i'm using setlocale(LC_ALL, "") call. It returns current language and country, for example "ru_RU.UTF-8" in my case.

Everything works without problems in MacOS 10.9.1 when i'm launching my application from terminal. When I'm running my application from Finder, setlocale always return default locale ("C"). I suspect that's because LANG environment variable is not set by Finder.

What's correct way to detect user locale under MacOS X in C++ application?

1

There are 1 best solutions below

1
On

Looks like setlocale functions is broken in MacOS and there is no working C alternative. I solved problem by adding Objective-C++ module to my application which uses NSLocale object:

    std::string localeStr([[[NSLocale currentLocale] localeIdentifier] UTF8String]);

Later i'm passing localeStr to my C++ code instead of setlocale result.

It works perfectly, but I'm not 100% happy with it because I don't like to use Objective-C++ for only one line of code. If somebody knows better workaround, you are welcome.