iOS:如何获取设备当前语言设置?

时间:2021-06-12 23:59:28

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 Language setting (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 Language data and not the Region Format (am I right?).

我熟悉NSLocale对象,但它似乎与Region Format设置有关,而不是语言设置(参见下面的屏幕截图),所以当我尝试使用[locale displayNameForKey:NSLocaleIdentifier值来检索语言时: [locale localeIdentifier]我得到的是英语(美国)而不是英语;另外,我认为我需要的是语言数据而不是区域格式(我是对的吗?)。

Can anyone direct me to how to retrieve the language setting?

任何人都可以指导我如何检索语言设置?

iOS:如何获取设备当前语言设置?

9 个解决方案

#1


51  

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];

#2


5  

Try this:

尝试这个:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];

#3


3  

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
)'

#4


2  

Swift:

迅速:

let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString

#5


1  

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 

#6


1  

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;

#7


1  

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)

#8


0  

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

#9


0  

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中和之后,这段代码需要考虑到这一点

#1


51  

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];

#2


5  

Try this:

尝试这个:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];

#3


3  

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
)'

#4


2  

Swift:

迅速:

let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString

#5


1  

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 

#6


1  

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;

#7


1  

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)

#8


0  

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

#9


0  

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中和之后,这段代码需要考虑到这一点