Detect language in use by iOS application

后端 未结 2 836
野性不改
野性不改 2021-01-21 13:47

How can I detect the current application language ? I am not talking about NSLocale user preferences.

In my app there are currently two supported languages

相关标签:
2条回答
  • 2021-01-21 14:43

    [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] would be better, because it will always return a localization that your app supports. There is no need for an additional check afterwards.

    In fact, your code has a bug. If the first language in the user's preferences is not one that your app supports, iOS will continue down the list until it finds one that your app does support. So if the user's preferences were "fr", "it", "en", ..., iOS would load the it versions of your resources. However, your code would fall back to en.

    (This is perhaps more important on OS X, where it's easy for the user to change the language ordering. On iOS it's apparently possible to do that, but it's not as obvious how it works.)

    0 讨论(0)
  • 2021-01-21 14:54

    I usually reserve a special key in Localizable.strings, such as "HTTPAcceptLanguage", which I set to "en", "fr", etc. Now telling your server the language displayed by the application is as simple as NSLocalizedString(@"HTTPAcceptLanguage", nil).

    0 讨论(0)
提交回复
热议问题