iOS:如何获取设备当前语言设置?
我的应用程序中的一些功能应该基于其运行设备的语言设置。
我想获取实际的语言而不是某些国家/地区设置。例如,如果语言是英语,我不在乎它是美国、英国、澳大利亚等...
我熟悉 NSLocale
对象,但它似乎与 < code>Region Format 设置而不是 Language 设置(请参见下面的屏幕截图),因此当我尝试使用 [locale displayNameForKey:NSLocaleIdentifier 值从中检索语言时: [语言环境localeIdentifier]
我得到诸如 English (United States)
而不是 English
之类的内容;另外,我认为我需要的是语言数据,而不是区域格式(我是对的吗?)。
谁能指导我如何检索语言设置?
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?).
Can anyone direct me to how to retrieve the language setting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在XCode的帮助文档中找到解决方案,它写道:
获取当前语言
要从主应用程序包中获取应用程序正在使用的语言,请使用NSBundle类中的preferredLocalizations方法:
NSString * languageID = [[NSBundle mainBundle ] PreferredLocalizations].firstObject;
Find the solution in XCode's helper document, it wrote:
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:
NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;
使用下面的代码获取本地化语言,而不会遇到 en-india、en-us 等问题。
在 ios9 中和之后,需要考虑此代码
Use below code to fetch Localised language without having trouble to the en-india, en-us etc..
In and After ios9 this code need to take in cosideration
要了解本地化中选择的当前语言,请使用
示例:
要获取两个字母的单词
Swift:
To know the current language selected within your localizations use
Example:
To get two letter word
Swift:
存储的用户首选语言可以从语言环境中作为数组检索,当前语言标识符是该数组中的第一个对象:
如果您希望语言具有更易读的形式,请使用 NSLocale 的
displayNameForKey:value:
方法:User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:
If you want language in more readable form then use
displayNameForKey:value:
method of NSLocale:试试这个:
Try this:
在 Swift 中获取语言和区域:
就我而言,我得到:
Getting language and region in
Swift
:In my case I'm getting:
在 Swift 4 中:
它只会为您提供语言代码,而不会提供国家/地区代码。
In Swift 4:
It will give you just the language code, no country code.
迅速:
Swift:
工作解决方案:
Working solution: