iOS:如何获取设备当前语言设置? [英] iOS: How to Get the Device Current Language Setting?
问题描述
我的应用程序中有一些功能应该基于运行它的设备的语言设置。
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...
我熟悉 NSLocale
对象,但它似乎与区域格式
设置有关,而与语言设置无关(看下面的屏幕截图)所以当我尝试使用 [locale displayNameForKey:NSLocaleIdentifier值:[locale localeIdentifier]
检索语言时,我会得到类似英语(美国)
而不是英语
;另外,我认为我需要的是语言数据,而不是地区格式(我是对的吗?)。
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?).
有人可以指导我如何检索语言设置吗?
Can anyone direct me to how to retrieve the language setting?
推荐答案
存储的用户首选语言可以从语言环境中检索为数组和当前语言identifier是该数组中的第一个对象:
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];
如果您想要更易读的语言,请使用 displayNameForKey:value:
NSLocale的方法:
If you want language in more readable form then use displayNameForKey:value:
method of NSLocale:
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
这篇关于iOS:如何获取设备当前语言设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!