Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display locale language in full

Tags:

iphone

I am getting the user preferred language from the below code:

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

This will return something like 'en' for 'English', or 'de' for 'Deutsch'. Is there any way I can automatically get the full language name, i.e. English instead of en?

Thanks!

Mihai Fonoage

like image 537
Mihai Fonoage Avatar asked Feb 11 '10 17:02

Mihai Fonoage


2 Answers

To get a user's list of preferred languages, use [NSLocale preferredLanguages]. This returns an array of language code strings. To convert those language codes to a display name, you need to use the NSLocale displayNameForKey:value: instance method, with the key NSLocalIdentifier.

For the display name of the language in your current locale, call that method on [NSLocale currentLocale] -- in my case "de" becomes "German". If you'd like the name in the language itself, first create an NSLocale instance for that language, then use that object as the receiver. For instance "de" becomes "Deutsch", in German.

This code should make it clear:

NSArray *languages = [NSLocale preferredLanguages];
for (NSString *language in languages) {
  NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:language];
  NSLog(@"language code = %@, display name = %@, in language = %@",
        language,
        [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language],
        [locale displayNameForKey:NSLocaleIdentifier value:language]);
}

And the output on my iPhone:

language code = en, display name = English, in language = English
language code = zh-Hans, display name = Chinese (Simplified Han), in language = 中文 (简体中文)
language code = ja, display name = Japanese, in language = 日本語
language code = pt, display name = Portuguese, in language = português
language code = de, display name = German, in language = Deutsch
language code = fr, display name = French, in language = français
language code = nl, display name = Dutch, in language = Nederlands
language code = it, display name = Italian, in language = italiano
language code = es, display name = Spanish, in language = español
language code = pt-PT, display name = Portuguese (Portugal), in language = português (Portugal)
language code = da, display name = Danish, in language = dansk
language code = fi, display name = Finnish, in language = suomi
language code = nb, display name = Norwegian Bokmål, in language = norsk bokmål
language code = sv, display name = Swedish, in language = svenska
language code = ko, display name = Korean, in language = 한국어
language code = zh-Hant, display name = Chinese (Traditional Han), in language = 中文 (繁體中文)
language code = ru, display name = Russian, in language = русский
language code = pl, display name = Polish, in language = polski
language code = tr, display name = Turkish, in language = Türkçe
language code = uk, display name = Ukrainian, in language = українська
language code = ar, display name = Arabic, in language = العربية
language code = hr, display name = Croatian, in language = hrvatski
language code = cs, display name = Czech, in language = čeština
language code = el, display name = Greek, in language = Ελληνικά
language code = he, display name = Hebrew, in language = עברית
language code = ro, display name = Romanian, in language = română
language code = sk, display name = Slovak, in language = slovenský
language code = th, display name = Thai, in language = ไทย
language code = id, display name = Indonesian, in language = Bahasa Indonesia
like image 178
Trashpanda Avatar answered Sep 20 '22 05:09

Trashpanda


NSArray *languages = [[NSBundle mainBundle] localizations];
NSString *language = [languages objectAtIndex:0];
like image 37
Thomas Joulin Avatar answered Sep 19 '22 05:09

Thomas Joulin