简体   繁体   English

iOS检查电话语言的最佳和安全方法是什么?

[英]IOS What's the best and safe way to check the phone language?

Until now i have used this code to retrive phone language: 到目前为止,我一直使用此代码检索电话语言:

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 

As we already now, apple since IOS 9 has changed the return of this function, from something like "en" to something like "en-GB".In my apps i usually do an "istostring" to check with wich language i have to work. 正如我们现在所了解的,自IOS 9以来,苹果已将此功能的返回值从“ en”更改为“ en-GB”。在我的应用程序中,我通常执行“ istostring”以检查使用哪种语言工作。 Now i need to implement something like the correct answer to this question 现在我需要实现类似该问题的正确答案的方法

And i have to implement a condition in every method call to check if the device is IOS <9 or IOS >=9. 而且我必须在每个方法调用中实现一个条件,以检查设备是否为IOS <9或IOS> = 9。 This apple's change seems to not consider any older version compatility problems,i made an intensive use of this function, so i have to update all my apps...Maybe the problem is another, there's a better way to check the actual language of the phone? 这个苹果的变化似乎没有考虑任何较旧版本的兼容性问题,我对此功能进行了广泛使用,因此我必须更新我的所有应用程序...也许问题是另外一个,还有一种更好的方法来检查实际的语言电话?

Unfortunately, its a common pain shared by developer community and there is nothing much we can do to address this. 不幸的是,这是开发人员社区共同的痛苦,我们无能为力。

However, I safeguard myself by adding a category on UIDevice like this: 但是,我通过在UIDevice上添加如下类别来保护自己:

+ (NSString *)deviceLanguage {
    return [[NSLocale preferredLanguages] objectAtIndex:0];
}

Then simply call: 然后只需调用:

[UIDevice deviceLanguage]

to get the device language. 获取设备语言。 At least, like in your case, if you are using this feature multiple places, there is only one place change to make the entire app align to new language/OS support. 至少,就像您的情况一样,如果您在多个地方使用此功能,则只需更改一个地方即可使整个应用程序与新的语言/操作系统支持保持一致。

I guess this might help you 我想这可能对您有帮助

NSArray *array = [NSLocale preferredLanguages];
for (NSString *lan in array) 
{

 NSLog(@"%@: %@ %@",lan, [NSLocale canonicalLanguageIdentifierFromString:lan], [[[NSLocale alloc] initWithLocaleIdentifier:lan] displayNameForKey:NSLocaleIdentifier value:lan]);
}

In Objective C 在目标C中

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

In Swift 在斯威夫特

let language = NSBundle.mainBundle().preferredLocalizations.first! as NSString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM