Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 568 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting current device language in iOS?

#1
I'd like to show the current language that the device UI is using. What code would I use?

I want this as an `NSString` in fully spelled out format. (Not @"en_US")

EDIT: For those driving on by, there are a ton of useful comments here, as the answer has evolved with new iOS releases.
Reply

#2
This will probably give you what you want:

NSLocale *locale = [NSLocale currentLocale];

NSString *language = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];

It will show the name of the language, in the language itself.
For example:

Français (France)
English (United States)
Reply

#3
You can use the `displayNameForKey:value:` method of [`NSLocale`](

[To see links please register here]

):

// get a French locale instance
NSLocale *frLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];

// use it to get translated display names of fr_FR and en_US
NSLog(@"%@", [frLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"]);
NSLog(@"%@", [frLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"]);

This will print out:

français (France)
anglais (États-Unis)

If you specify the same locale identifier for the `initWithLocaleIdentifier:` and also the `displayNameForKey:value:` method, then it will give you the native name of the language. I've discovered that if you remove the country code and use just `fr` and `en`, that it will also omit the country from the display name (on Mac OS X at least, not sure about iOS).
Reply

#4
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 leak..

and result is

2013-03-02 20:01:57.457 xx[12334:907] zh-Hans: zh-Hans 中文(简体中文)
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 日本語
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
Reply

#5
According to Apple [documentation][1]


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


[1]:

[To see links please register here]

Reply

#6
For getting user device current language use the following it code it worked for me.

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

#7
Translating language codes such as **en_US** into **English (United States)** is a built in feature of `NSLocale` and `NSLocale` does not care where you get the language codes from. So there really is no reason to implement your own translation as the accepted answer suggests.

// Example code - try changing the language codes and see what happens
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSString *l1 = [locale displayNameForKey:NSLocaleIdentifier value:@"en"];
NSString *l2 = [locale displayNameForKey:NSLocaleIdentifier value:@"de"];
NSString *l3 = [locale displayNameForKey:NSLocaleIdentifier value:@"sv"];
NSLog(@"%@, %@, %@", l1, l2, l3);

Prints: **English, German, Swedish**
Reply

#8
-(NSString *)returnPreferredLanguage { //as written text

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSArray *preferredLanguages = [defaults objectForKey:@"AppleLanguages"];
NSString *preferredLanguageCode = [preferredLanguages objectAtIndex:0]; //preferred device language code
NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"]; //language name will be in English (or whatever)
NSString *languageName = [enLocale displayNameForKey:NSLocaleIdentifier value:preferredLanguageCode]; //name of language, eg. "French"
return languageName;

}
Reply

#9
For MonoTouch C# developers use:

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

Note: I know this was an iOS question, but as I am a MonoTouch developer, the answer on this page led me in the right direction and I thought I'd share the results.
Reply

#10
If you want to get only language here is my suggested answer:

NSString *langplusreg = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString * langonly = [[langplusreg componentsSeparatedByString:@"-"]
objectAtIndex:0];

In my case i just wanted only Locale language not locale region.

Output:
If your Locale language is Japanese and locale region is Japan then:

langplusreg = ja-JP

langonly = ja
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through