开发者

Display locale language in full

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' fo开发者_JS百科r '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


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


NSArray *languages = [[NSBundle mainBundle] localizations];
NSString *language = [languages objectAtIndex:0];


For Full Language string like "English" not just "en"

NSString * language = [NSString stringWithFormat:@"%@",[[NSLocale preferredLanguages] objectAtIndex:0]];


language = [NSString stringWithFormat:@"%@",[[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language]];
NSLog(@"Language=%@",language);

Its quite simple for me in language string you will get Full name of your device current language.


Unfortunately, I believe you will need to construct a lookup table for the language codes. These are ISO 639-1 codes. For more, see the "Language and Locale Designations" section of the Internationalization Programming Topics guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜