Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting user's country and keyboard language

Tags:

c

macos

cocoa

In order to present the proper menu text and some other aspects of the UI I am trying to get the current user's country and keyboard language.
I know the locale (via the env. variable) but I can't find a way to get these two pieces of info.

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

Thank you!

like image 692
Jessica Avatar asked Jun 29 '11 13:06

Jessica


People also ask

How do I change input method on keyboard?

Switch between keyboard layouts or input methods On a hardware keyboard, press and hold the Windows logo key , and then press the Spacebar to cycle through your input methods.

How do I remove Hindi US keyboard from Windows 10?

Remove extra language packs or keyboard languagesSelect Start > Settings > Time & language > Language & region. Under Preferred languages, select the language you want to remove, and then select Remove.


1 Answers

Use CFLocaleCopyCurrent, CFLocaleGetValue and CFLocaleCopyPreferredLanguages (note that the preferred language may not match the locale's language). See the documentation.

Edit: ok, here's some sample code.

#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);
}
like image 154
LaC Avatar answered Oct 05 '22 22:10

LaC