在 iOS 中获取当前设备语言? [英] Getting current device language in iOS?

查看:26
本文介绍了在 iOS 中获取当前设备语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示设备 UI 使用的当前语言.我会使用什么代码?

I'd like to show the current language that the device UI is using. What code would I use?

我希望这是一个完整拼写格式的 NSString.(不是@en_US")

I want this as an NSString in fully spelled out format. (Not @"en_US")

对于那些路过的人,这里有大量有用的评论,因为答案随着新的 iOS 版本而发展.

For those driving on by, there are a ton of useful comments here, as the answer has evolved with new iOS releases.

推荐答案

所提供的解决方案实际上将返回设备的当前区域 - 而不是当前选择的语言.这些通常是一回事.但是,如果我在北美并将我的语言设置为日语,我所在的地区仍将是英语(美国).为了检索当前选择的语言,您可以:

The solutions provided will actually return the current region of the device - not the currently selected language. These are often one and the same. However, if I am in North America and I set my language to Japanese, my region will still be English (United States). In order to retrieve the currently selected language, you can do:

NSString * language = [[NSLocale preferredLanguages] firstObject];

这将返回当前所选语言的两个字母代码.恩"对于英语,es"西班牙语,de"对于德语等.更多示例,请参阅此维基百科条目(特别是 639-1 列):

This will return a two letter code for the currently selected language. "en" for English, "es" for Spanish, "de" for German, etc. For more examples, please see this Wikipedia entry (in particular, the 639-1 column):

ISO 639-1 代码列表

然后将两个字母代码转换为您想要显示的字符串是一件简单的事情.因此,如果是en",则显示English".

Then it's a simple matter of converting the two letter codes to the string you would like to display. So if it's "en", display "English".

编辑

值得引用 NSLocale.h 中的头信息:

Worth to quote the header information from 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

对应用语言感兴趣的人可以看看 @mindvision 的回答

People interested in app language take a look at @mindvision's answer

这篇关于在 iOS 中获取当前设备语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆