我想要显示设备UI使用的当前语言。我应该使用什么代码?
我想把它作为一个NSString完全拼写出来的格式。(@ en_US)
编辑:对于那些开车路过的人来说,这里有大量有用的评论,因为随着新iOS版本的发布,答案也在不断变化。
我想要显示设备UI使用的当前语言。我应该使用什么代码?
我想把它作为一个NSString完全拼写出来的格式。(@ en_US)
编辑:对于那些开车路过的人来说,这里有大量有用的评论,因为随着新iOS版本的发布,答案也在不断变化。
当前回答
接受的答案和其他答案都没有考虑到首选语言可以是设备语言以外的另一种语言。
设备语言是操作系统元素和苹果应用程序所使用的语言。
首选语言是用户希望应用程序本地化的语言。苹果只提供有限的一组翻译。如果首选语言是苹果将其应用程序翻译成的一种语言,那么它也将是设备语言。但是,如果用户喜欢苹果没有提供翻译的语言,那么设备和首选语言将不匹配。设备语言将不在首选语言列表的第一位置。
下面的函数将遍历首选语言列表,并检查Apple框架中是否有翻译。第一种有翻译的语言是设备语言。函数将返回其语言代码。
func deviceLanguage() -> String? {
let systemBundle: NSBundle = NSBundle(forClass: UIView.self)
let englishLocale: NSLocale = NSLocale(localeIdentifier: "en")
let preferredLanguages: [String] = NSLocale.preferredLanguages()
for language: String in preferredLanguages {
let languageComponents: [String : String] = NSLocale.componentsFromLocaleIdentifier(language)
guard let languageCode: String = languageComponents[NSLocaleLanguageCode] else {
continue
}
// ex: es_MX.lproj, zh_CN.lproj
if let countryCode: String = languageComponents[NSLocaleCountryCode] {
if systemBundle.pathForResource("\(languageCode)_\(countryCode)", ofType: "lproj") != nil {
// returns language and country code because it appears that the actual language is coded within the country code aswell
// for example: zh_CN probably mandarin, zh_HK probably cantonese
return language
}
}
// ex: English.lproj, German.lproj
if let languageName: String = englishLocale.displayNameForKey(NSLocaleIdentifier, value: languageCode) {
if systemBundle.pathForResource(languageName, ofType: "lproj") != nil {
return languageCode
}
}
// ex: pt.lproj, hu.lproj
if systemBundle.pathForResource(languageCode, ofType: "lproj") != nil {
return languageCode
}
}
return nil
}
如果首选语言列表是:
南非荷兰语(iOS不翻译成南非荷兰语) 西班牙语(设备语言)
首选语言列表可在“设置”中编辑。app ->通用->语言和地区->首选语言顺序
您可以使用设备语言代码并将其转换为语言名称。下面几行将以设备语言打印设备语言。例如,如果设备设置为西班牙语,则为“Español”。
if let deviceLanguageCode: String = deviceLanguage() {
let printOutputLanguageCode: String = deviceLanguageCode
let printOutputLocale: NSLocale = NSLocale(localeIdentifier: printOutputLanguageCode)
if let deviceLanguageName: String = printOutputLocale.displayNameForKey(NSLocaleIdentifier, value: deviceLanguageCode) {
// keep in mind that for some localizations this will print a language and a country
// see deviceLanguage() implementation above
print(deviceLanguageName)
}
}
其他回答
斯威夫特
获取设备的当前语言
NSLocale.preferredLanguages()[0] as String
获取应用程序语言
NSBundle.mainBundle().preferredLocalizations[0] as NSString
注意:
它获取你在info.plist的CFBundleDevelopmentRegion中给出的语言
如果CFBundleAllowMixedLocalizations在info中为true。plist然后info中的cfbundlelocizations的第一项。返回Plist
选择的答案返回当前设备语言,但不是应用程序中使用的实际语言。如果你没有在应用程序中为用户的首选语言提供本地化,则使用第一个可用的本地化,按用户的首选顺序排序。
要发现在本地化中选择的当前语言,请使用
[[NSBundle mainBundle] preferredLocalizations];
例子:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
迅速:
let language = NSBundle.mainBundle().preferredLocalizations.first as NSString
这可能会给你你想要的:
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];
它会在语言本身中显示语言的名称。 例如:
Français (France)
English (United States)
如果你只想得到语言,这里是我建议的答案:
NSString *langplusreg = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString * langonly = [[langplusreg componentsSeparatedByString:@"-"]
objectAtIndex:0];
在我的情况下,我只是想Locale语言而不是Locale区域。
输出: 如果你的区域语言是日语,区域区域是日本,那么:
朗加雷格 = 是-JP
朗只 = 和
将语言代码(如en_US)翻译成英语(United States)是NSLocale的内置特性,NSLocale并不关心你从哪里获得语言代码。因此,确实没有理由像公认的答案所建议的那样实现自己的翻译。
// 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);
印刷:英语,德语,瑞典语