Search code examples
iosxcodecocoa-touchios9

iOS9 AppleLanguages different from older iOS


How I get now the actual system language? It seems that they put regional suffix after last dash. So before cs is now cs-DE if the language is Czech and regional setting is German. But there are some languages which don't have the suffix like GB language is en-GB but regional setting is German.

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* language = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [language objectAtIndex:0];
NSLog(@"localeIdentifier: %@", preferredLang);

Solution

  • Use the componentsFromLocaleIdentifier method from NSLocale class

    Here is the documentation

    You can do like this:

    NSString* localeID = [NSLocale currentLocale].localeIdentifier;
    NSDictionary* components = [NSLocale componentsFromLocaleIdentifier:localeID];
    NSString* languageID = components[NSLocaleLanguageCode];
    

    EDIT

    Getting the language this way will create some issues if the language the app is currently translated in is not the device's language. Indeed, components[NSLocaleLanguageCode] will return the device's language.

    To get the app's current language, you should use [[NSBundle mainBundle] preferredLocalizations].firstObject.

    To get the device's region, you can still use components[NSLocaleCountryCode]