Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 541
kubo Avatar asked Sep 18 '15 13:09

kubo


2 Answers

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]

like image 102
Imotep Avatar answered Oct 09 '22 20:10

Imotep


I just run into this problem recently. According to Apple's documentation, you will get the locale id with region designator which for like [language designator]-[region designator] on iOS 9. I found a solution if you just wanna get the locale id, you could use
[[NSBundle mainBundle] preferredLocalizations].

like image 21
yuhua Avatar answered Oct 09 '22 18:10

yuhua