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

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

问题描述

我想显示装置使用者介面目前使用的语言。我将使用什么代码?

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

我想要这是一个完全拼写格式的NSString。 (not @e​​n_US)

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

编辑:对于那些开车的人,这里有很多有用的评论,因为答案随着新的iOS版本发展。 / p>

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] objectAtIndex:0];

这将返回当前所选语言的双字母代码。 en代表英语,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,显示英语。

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".

希望这有助于有人区分区域和当前选择的语言。

Hope this helps someone that's looking to differentiate between region and currently selected language.

EDIT

值得引用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的回答

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

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