Getting user language and keyboard language

To present the correct menu text and some other aspects of the user interface, I am trying to get the current user country and keyboard language.
I know the language (via env. Variable), but I can’t find a way to get these two pieces of information.

The code is in C for Mac OS X. I can use the Cocoa API to get them, but I need to call them from C. Any ideas?

Thank!

+5
source share
1 answer

Use CFLocaleCopyCurrent, CFLocaleGetValueand CFLocaleCopyPreferredLanguages(note that the preferred language may not match the language of the locale). See the documentation .

: ok, .

#include <CoreFoundation/CoreFoundation.h>
#include <stdio.h>

int main (int argc, char **argv)
{
    CFLocaleRef loc = CFLocaleCopyCurrent();
    CFStringRef countryCode = CFLocaleGetValue (loc, kCFLocaleCountryCode);
    CFStringRef countryName = CFLocaleCopyDisplayNameForPropertyValue (loc, kCFLocaleCountryCode, countryCode);
    CFShow(countryCode);
    CFShow(countryName);
    CFArrayRef langs = CFLocaleCopyPreferredLanguages();
    CFStringRef langCode = CFArrayGetValueAtIndex (langs, 0);
    CFStringRef langName = CFLocaleCopyDisplayNameForPropertyValue (loc, kCFLocaleLanguageCode, langCode);
    CFShow(langCode);
    CFShow(langName);
}
+7

All Articles