iOS:如何获取设备当前语言设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6829448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: How to Get the Device Current Language Setting?
提问by Ohad Regev
There are some features within my application that are supposed to be based on the language settings of the device where it's running.
我的应用程序中有一些功能应该基于运行它的设备的语言设置。
I want to get the actual language and not some country settings. Foe example, if the language is English, I don't care if it's US, UK, Australia, etc...
我想获得实际语言而不是某些国家/地区设置。例如,如果语言是英语,我不在乎是美国、英国、澳大利亚等...
I'm familiar with the NSLocale
object, but it seems to relate to the Region Format
setting and not to the Languagesetting (see screen shot below) so when I try to retrieve the language out of it using [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier]
I get things like English (United States)
instead of English
; also, I think that what I need is the Languagedata and not the Region Format(am I right?).
我熟悉该NSLocale
对象,但它似乎与Region Format
设置有关,而不是与语言设置有关(请参见下面的屏幕截图),因此当我尝试使用它从中检索语言时,[locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier]
我得到了类似的东西,English (United States)
而不是English
; 另外,我认为我需要的是语言数据而不是区域格式(对吗?)。
Can anyone direct me to how to retrieve the language setting?
谁能指导我如何检索语言设置?
回答by Vladimir
User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:
存储的用户首选语言可以作为数组从语言环境中检索,当前语言标识符是该数组中的第一个对象:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
If you want language in more readable form then use displayNameForKey:value:
method of NSLocale:
如果你想要更易读的语言,那么使用displayNameForKey:value:
NSLocale 的方法:
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
回答by Sergio
Try this:
尝试这个:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];
回答by superarts.org
Getting language and region in Swift
:
获取语言和地区Swift
:
LF.log("language", NSLocale.preferredLanguages())
LF.log("locale", NSBundle.mainBundle().preferredLocalizations)
In my case I'm getting:
就我而言,我得到:
language: '(
"zh-Hans"
)'
locale: '(
en
)'
回答by Esqarrouth
Swift:
迅速:
let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString
回答by iDevAmit
Working solution:
工作解决方案:
let language = NSLocale.preferredLanguages()[0]
let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
//let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
print(languageCode)
回答by Rubaiyat Jahan Mumu
In Swift 4:
在Swift 4 中:
let currentLanguage = Locale.current.languageCode
It will give you just the language code, no country code.
它只会给你语言代码,没有国家代码。
回答by Mohit
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
回答by liwp_Stephen
Find the solution in XCode's helper document, it wrote:
在 XCode 的帮助文档中找到解决方案,它写道:
Getting the Current Language
获取当前语言
To get the language that the app is using from the main application bundle, use the preferredLocalizations method in the NSBundle class:
要从主应用程序包中获取应用程序使用的语言,请使用 NSBundle 类中的 preferredLocalizations 方法:
NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;
NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;
回答by Esha
Use below code to fetch Localised language without having trouble to the en-india, en-us etc..
使用下面的代码来获取本地化语言,而不会遇到 en-india、en-us 等问题。
NSString *Ph = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
In and After ios9 this code need to take in cosideration
在ios9中和之后这段代码需要考虑
回答by Azhar
To know the current language selected within your localizations use
要了解本地化中选择的当前语言,请使用
[[NSBundle mainBundle] preferredLocalizations]
Example:
例子:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
To get two letter word
得到两个字母的单词
NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];
Swift:
迅速:
let language = NSBundle.mainBundle().preferredLocalizations.first as NSString