IOS9 AppleLanguages ​​is different from older iOS

How can I get the real system language? After the last dash, they seem to have added a regional suffix. Therefore, before cs now cs-DE , if the language is Czech and regional is German. But there are some languages ​​that do not have a suffix, such as GB, en-GB , but the regional settings are German.

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

Use the componentsFromLocaleIdentifier method from the NSLocale class

Here is the documentation

You can do the following:

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

EDIT

Getting the language this way will create some problems if the language in which the application is currently translated is not the language of the device. In fact, components[NSLocaleLanguageCode] will return the device language.

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

To get the scope of the device, you can still use components[NSLocaleCountryCode]

+9
source

I recently ran into this problem. According to Apple's documentation , you will get a locale identifier with a region pointer, which, like [language designator]-[region designator] on iOS 9. I found a solution, if you just want to get the locale identifier, you can use [[NSBundle mainBundle] preferredLocalizations] .

+5
source

Another solution. If any of you love,

  NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]; NSString *currentLanguage = [languages objectAtIndex:0]; if ([[currentLanguage componentsSeparatedByString:@"-"] count] == 2) currentLanguage = [[currentLanguage componentsSeparatedByString:@"-"] objectAtIndex:0]; // Only for chinese Language. else if ([[currentLanguage componentsSeparatedByString:@"-"] count] == 3) currentLanguage = [NSString stringWithFormat:@"%@-%@", [[currentLanguage componentsSeparatedByString:@"-"] objectAtIndex:0], [[currentLanguage componentsSeparatedByString:@"-"] objectAtIndex:1] ]; 

"currentLanguage" will provide you with the current langauge so you can use it for localization or future use.

+1
source

All Articles