妄徒之命 2010-10-11 21:58 采纳率: 100%
浏览 1182
已采纳

在 iOS 中获取当前的设备语言?

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

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

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

转载于:https://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios

  • 写回答

27条回答

  • 零零乙 2010-11-19 01:41
    关注

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

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

    List of ISO 639-1 codes

    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

    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
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(26条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题