Get the current device language in iOS?

Posted by jursten on Sat, 01 Feb 2020 18:03:10 +0100

I want to display the current language used by the device UI. What code will I use?

I want it to appear as an NSString in a fully spelled format. (not @ "en UUS")

Editor: for those who continue to move forward, there are a lot of useful comments here, as the answer evolves with the new version of iOS.

#1 building

For MonoTouch C developers, use:

NSLocale.PreferredLanguages.FirstOrDefault() ?? "en"

Note: I know this is an iOS problem, but since I am a MonoTouch developer, the answer on this page leads me in the right direction, and I think I will share the results.

#2 building

The selected answer returns the current device language, but not the actual language used in the application. If you do not provide a localized version of the user's preferred language in the app, use the first available localized version sorted by user preference.

To discover the current language selected in your localized language, use the

[[NSBundle mainBundle] preferredLocalizations];

Example:

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

Quickly:

let language = NSBundle.mainBundle().preferredLocalizations.first as NSString

#3 building

I use this.

    NSArray *arr = [NSLocale preferredLanguages];
for (NSString *lan in arr) {
    NSLog(@"%@: %@ %@",lan, [NSLocale canonicalLanguageIdentifierFromString:lan], [[[NSLocale alloc] initWithLocaleIdentifier:lan] displayNameForKey:NSLocaleIdentifier value:lan]);
}

Ignore memory leaks.

The result is

2013-03-02 20:01:57.457 xx[12334:907] zh-Hans: zh-Hans Chinese (Simplified Chinese)
2013-03-02 20:01:57.460 xx[12334:907] en: en English
2013-03-02 20:01:57.462 xx[12334:907] ja: ja Japanese language
2013-03-02 20:01:57.465 xx[12334:907] fr: fr français
2013-03-02 20:01:57.468 xx[12334:907] de: de Deutsch
2013-03-02 20:01:57.472 xx[12334:907] nl: nl Nederlands
2013-03-02 20:01:57.477 xx[12334:907] it: it italiano
2013-03-02 20:01:57.481 xx[12334:907] es: es español

#4 building

According to Apple File

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];

#5 building

In order to get the current language of the user device, please use the following code for me.

NSString * myString = [[NSLocale preferredlanguage]objectAtIndex:0];

Topics: iOS