Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setlocale in Mac OS X doesn't work

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?

like image 721
user3173333 Avatar asked Nov 02 '22 07:11

user3173333


1 Answers

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.

like image 179
user3173333 Avatar answered Nov 15 '22 04:11

user3173333