如何使用C ++在OSX中获取当前用户的语言环境 [英] How to get the locale of the current user in OSX using C++
问题描述
我有一个C ++应用程序,需要在其中检索当前用户的语言环境.如何使用OSX Yosemite和更新的版本?
I have a C++ application where I need to retrieve the locale of the current user. How can I do it with OSX Yosemite and newer?
我尝试了类似setlocale(LC_CTYPE, NULL);
的方法,但是它只是返回UTF-8
,其中我的系统显然是西班牙语(es_AR)
I've tried something like setlocale(LC_CTYPE, NULL);
but it just returns UTF-8
where my system is clearly in Spanish (es_AR)
推荐答案
经过一番尝试和错误,以及来自Internet和其他问题的大量帮助,我做到了.
After some try and error and lot of help from internet and other questions I did it.
如果我只想获得语言.
CFLocaleRef cflocale = CFLocaleCopyCurrent();
CFStringRef value = (CFStringRef)CFLocaleGetValue(cflocale, kCFLocaleLanguageCode);
std::string str(CFStringGetCStringPtr(value, kCFStringEncodingUTF8));
CFRelease(cflocale);
这样,在str处,我将获得带有该语言的std :: string.如果我需要其他东西,可以用CFLocale
This way, at str I'll get a std::string with the language. If I need something else, I can replace kCFLocaleLanguageCode
with any other constant from CFLocale
我还需要标题#import <CoreFoundation/CoreFoundation.h>
这篇关于如何使用C ++在OSX中获取当前用户的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!