Mos*_*she 420 cocoa-touch objective-c ios nslocale
我想显示设备UI正在使用的当前语言.我会用什么代码?
我希望这是一个NSString完全拼写的格式.(不是@"en_US")
编辑:对于那些开车的人来说,这里有很多有用的评论,因为答案随着新的iOS版本的发展而演变.
Dub*_*ron 793
提供的解决方案实际上将返回设备的当前区域 - 而不是当前选定的语言.这些往往是同一个.但是,如果我在北美并将语言设为日语,我的地区仍将是英语(美国).要检索当前选择的语言,您可以执行以下操作:
NSString * language = [[NSLocale preferredLanguages] firstObject];
Run Code Online (Sandbox Code Playgroud)
这将返回当前所选语言的双字母代码.英语为"en",西班牙语为"es",德语为"de"等.有关更多示例,请参阅此Wikipedia条目(特别是639-1专栏):
然后将两个字母代码转换为您想要显示的字符串就可以了.所以,如果它是"en",则显示"English".
希望这可以帮助那些希望区分区域和当前所选语言的人.
编辑
值得引用NSLocale.h中的头信息:
+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information
Run Code Online (Sandbox Code Playgroud)
对app语言感兴趣的人会看看@ mindvision的答案
小智 275
所选答案将返回当前设备语言,但不会返回应用程序中使用的实际语言.如果您未在应用中为用户的首选语言提供本地化,则会使用按用户首选顺序排序的第一个可用本地化.
要发现在本地化中使用的当前语言
[[NSBundle mainBundle] preferredLocalizations];
Run Code Online (Sandbox Code Playgroud)
例:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
迅速:
let language = NSBundle.mainBundle().preferredLocalizations.first as NSString
Run Code Online (Sandbox Code Playgroud)
ami*_*mir 84
iOS 9解决方案:
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
language ="en-US"
NSDictionary *languageDic = [NSLocale componentsFromLocaleIdentifier:language];
Run Code Online (Sandbox Code Playgroud)
languageDic将拥有所需的组件
NSString *countryCode = [languageDic objectForKey:@"kCFLocaleCountryCodeKey"];
Run Code Online (Sandbox Code Playgroud)
countryCode ="US"
NSString *languageCode = [languageDic objectForKey:@"kCFLocaleLanguageCodeKey"];
Run Code Online (Sandbox Code Playgroud)
languageCode ="en"
Phi*_*ert 64
这可能会给你你想要的东西:
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];
Run Code Online (Sandbox Code Playgroud)
它将以语言本身显示语言的名称.例如:
Français (France)
English (United States)
Run Code Online (Sandbox Code Playgroud)
dre*_*lab 35
所接受的和其他答案都没有考虑到首选语言可以是除设备语言之外的另一种语言.
所述设备的语言是在其中操作系统元件和苹果的应用程序呈现的语言.
的首选语言是用户想要在苹果只提供了一组有限的翻译本地化的应用程序的语言.如果首选语言是Apple将其应用翻译成的一种语言,那么它也将是设备语言.但是,如果用户更喜欢Apple不提供翻译的语言,则设备和首选语言将不匹配.设备语言不会位于首选语言列表的第一个位置.
以下函数将浏览首选语言列表,并检查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
}
Run Code Online (Sandbox Code Playgroud)
如果首选语言列表是:
的首选语言列表,可以在编辑:Settings.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)
}
}
Run Code Online (Sandbox Code Playgroud)
nor*_*DEV 14
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
Run Code Online (Sandbox Code Playgroud)
enz*_*ang 12
我用这个
NSArray *arr = [NSLocale preferredLanguages];
for (NSString *lan in arr) {
NSLog(@"%@: %@ %@",lan, [NSLocale canonicalLanguageIdentifierFromString:lan], [[[NSLocale alloc] initWithLocaleIdentifier:lan] displayNameForKey:NSLocaleIdentifier value:lan]);
}
Run Code Online (Sandbox Code Playgroud)
忽略内存泄漏..
结果是
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
Run Code Online (Sandbox Code Playgroud)
Eri*_*k B 12
翻译语言代码,如EN_US为英语(美国)是一个内置在功能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);
Run Code Online (Sandbox Code Playgroud)
印刷品:英语,德语,瑞典语
即使有更好的方法来获取当前的设备语言.让我们通过以下代码尝试 -
NSLog(@"Current Language - %@", [[NSLocale preferredLanguages] firstObject]);
Run Code Online (Sandbox Code Playgroud)
您可以使用以下displayNameForKey:value:方法NSLocale:
// get a French locale instance
NSLocale *frLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
// use it to get translated display names of fr_FR and en_US
NSLog(@"%@", [frLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"]);
NSLog(@"%@", [frLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"]);
Run Code Online (Sandbox Code Playgroud)
这将打印出来:
français (France)
anglais (États-Unis)
Run Code Online (Sandbox Code Playgroud)
如果initWithLocaleIdentifier:为该displayNameForKey:value:方法和方法指定相同的区域设置标识符,则它将为您提供该语言的本机名称.我发现,如果去掉国家代码,只需使用fr和en,这也将从显示名称省略国家(在Mac OS X至少,不能确定的iOS).
我试图为自己找到合适的解决方案.我使用Locale.preferredLanguages.first时从您的应用设置中返回首选语言.
如果您想通过用户设备设置了解语言,您应该使用以下字符串:
斯威夫特3
let currentDeviceLanguage = Locale.current.languageCode
// Will return the optional String
Run Code Online (Sandbox Code Playgroud)
要打开并使用,请查看以下行:
if let currentDeviceLanguage = Locale.current.languageCode {
print("currentLanguage", currentDeviceLanguage)
// For example
if currentDeviceLanguage == "he" {
UIView.appearance().semanticContentAttribute = .forceRightToLeft
} else {
UIView.appearance().semanticContentAttribute = .forceLeftToRight
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
为了获得用户设备当前的语言,请使用以下代码对我有用。
NSString * myString = [[NSLocale preferredlanguage]objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
获取设备的当前语言
NSLocale.preferredLanguages()[0] as String
Run Code Online (Sandbox Code Playgroud)
获取应用程序语言
NSBundle.mainBundle().preferredLocalizations[0] as NSString
Run Code Online (Sandbox Code Playgroud)
注意:
它获取您在info.plist的CFBundleDevelopmentRegion中提供的语言
如果在info.plist中CFBundleAllowMixedLocalizations为true,则返回info.plist中的第一个CFBundleLocalizations项
如果您正在寻找首选语言代码(“en”、“de”、“es”...)和本地化首选语言名称(针对当前语言环境),这里有一个 Swift 中的简单扩展:
extension Locale {
static var preferredLanguageIdentifier: String {
let id = Locale.preferredLanguages.first!
let comps = Locale.components(fromIdentifier: id)
return comps.values.first!
}
static var preferredLanguageLocalizedString: String {
let id = Locale.preferredLanguages.first!
return Locale.current.localizedString(forLanguageCode: id)!
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
227593 次 |
| 最近记录: |