检测iOS应用程序正在使用的语言 [英] Detect language in use by iOS application
问题描述
如何检测当前的应用程序语言?我不是在谈论NSLocale
用户首选项.
How can I detect the current application language ? I am not talking about NSLocale
user preferences.
在我的应用中,当前支持两种语言.默认的"en"和特定的"it".
In my app there are currently two supported languages. A default 'en' and a specific 'it'.
我只是想知道实际使用的是哪一个.如果相关,作为进一步的说明,我将通过Web服务仅针对两种受支持的语言提供内容.调用服务时,我需要传递语言参数.目前,我正在执行此操作,但它确实有效,但是我完全不喜欢它:
I just wanted to know which one is actually in use. If it is relevant, as a further explanation, I am providing content trough a web service only for the two supported languages. When invoking the service I need to pass a language parameter. Currently I am doing this, it works, but I totally dislike it:
NSString *preferredLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
if (![preferredLanguage isEqualToString:@"it"] && ![preferredLanguage isEqualToString:@"en"]) {
return @"en";
}
return preferredLanguage;
我已经看过NSLocale
,class
和UIApplication
,但是没有找到有用的东西.
I have looked trough the NSLocale
class
and UIApplication
, but didn't manage to find anything useful.
我还注意到NSBundle
有一些本地化方法,但是它们似乎都没有具体说明正在使用哪种方法.
I also notice that NSBundle
has some localization methods, but they all seems not specific about which one is in use.
推荐答案
我通常在Localizable.strings中保留一个特殊键,例如"HTTPAcceptLanguage",我将其设置为"en","fr"等.您的服务器上,应用程序显示的语言非常简单,如NSLocalizedString(@"HTTPAcceptLanguage",nil).
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).
这篇关于检测iOS应用程序正在使用的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!