我从以下代码中获取用户首选语言:
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString *language = [languages objectAtIndex:0];
这将返回'en'代表'English'或'de'代表'Deutsch'。有什么方法可以自动获得完整的语言名称,即英语而不是en?
谢谢!
Mihai Fonoage
答案 0 :(得分:36)
要获取用户的首选语言列表,请使用[NSLocale preferredLanguages]
。这将返回一个语言代码字符串数组。要将这些语言代码转换为显示名称,您需要使用带有密钥displayNameForKey:value:
的NSLocale NSLocalIdentifier
实例方法。
对于当前语言环境中语言的显示名称,请在[NSLocale currentLocale]
上调用该方法 - 在我的情况下,“de”变为“德语”。如果您想要语言本身的名称,请先为该语言创建一个NSLocale
实例,然后将该对象用作接收者。例如,“de”在德语中变成“Deutsch”。
此代码应明确说明:
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]);
}
我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
答案 1 :(得分:4)
NSArray *languages = [[NSBundle mainBundle] localizations];
NSString *language = [languages objectAtIndex:0];
答案 2 :(得分:1)
不幸的是,我相信你需要为语言代码构建一个查找表。这些是ISO 639-1 codes。有关更多信息,请参阅“国际化编程主题指南”的"Language and Locale Designations"部分。
答案 3 :(得分:1)
对于完整语言字符串,如“英语”,而不仅仅是“en”
NSString * language = [NSString stringWithFormat:@"%@",[[NSLocale preferredLanguages] objectAtIndex:0]];
language = [NSString stringWithFormat:@"%@",[[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language]];
NSLog(@"Language=%@",language);
对于我来说,语言字符串非常简单,您将获得设备当前语言的全名。