我想要显示设备UI使用的当前语言。我应该使用什么代码?
我想把它作为一个NSString完全拼写出来的格式。(@ en_US)
编辑:对于那些开车路过的人来说,这里有大量有用的评论,因为随着新iOS版本的发布,答案也在不断变化。
我想要显示设备UI使用的当前语言。我应该使用什么代码?
我想把它作为一个NSString完全拼写出来的格式。(@ en_US)
编辑:对于那些开车路过的人来说,这里有大量有用的评论,因为随着新iOS版本的发布,答案也在不断变化。
当前回答
两封信的格式。苹果使用ISO标准ISO-3166。
NSString *localeCountryCode = [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode];
其他回答
对于获得用户设备当前语言使用以下它的代码,它为我工作。
NSString * myString = [[NSLocale preferredlanguage]objectAtIndex:0];
I actually misread the original question, thought it asked for the "app UI" language (that's what I had googled for), not the "device UI", in which case the best answers would be the ones using preferredLocalizations, but those answers still give you a code, there is one more step to get a nice string to display. So, while the "device UI" language is already answered, if you want to display a nice string for which of the UI languages you support is currently in use, obviously the simplest solution is:
NSLocalizedString(@"currentLanguage", @"")
在你的每一个UI本地化中你都指定了你想要的显示方式。例如,在你的。strings文件的en版本中,你会有:
"currentLanguage"="English";
在你的fr版本的.strings文件中,你会有:
"currentLanguage"="Francais";
等。不要混淆代码等,你可以让你自己的字符串很好地匹配你的UI。
iOS13, Swift 5+
Locale.preferredLanguages.first
iOS 9的解决方案:
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
language = "en-US"
NSDictionary *languageDic = [NSLocale componentsFromLocaleIdentifier:language];
languageDic将拥有所需的组件
NSString *countryCode = [languageDic objectForKey:@"kCFLocaleCountryCodeKey"];
countryCode = "US"
NSString *languageCode = [languageDic objectForKey:@"kCFLocaleLanguageCodeKey"];
密码:
我用这个
NSArray *arr = [NSLocale preferredLanguages];
for (NSString *lan in arr) {
NSLog(@"%@: %@ %@",lan, [NSLocale canonicalLanguageIdentifierFromString:lan], [[[NSLocale alloc] initWithLocaleIdentifier:lan] displayNameForKey:NSLocaleIdentifier value:lan]);
}
忽略内存泄漏..
结果是
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
iOS13, Swift 5+, WWDC2019 https://developer.apple.com/videos/play/wwdc2019/403/
用户可以独立于操作系统语言选择应用程序的首选语言。
你可以使用这些:
// Returns a list of the user's preferred languages.
// Maybe more than (or none of) your app supports!
Locale.preferredLanguages
// a subset of this bundle's localizations, re-ordered into the preferred order
// for this process's current execution environment; the main bundle's preferred localizations
// indicate the language (of text) the user is most likely seeing in the UI
Bundle.main.preferredLocalizations
// The current running app language
Bundle.main.preferredLocalizations.first
// list of language names this bundle appears to be localized to
Bundle.main.localizations