检测本地化(xCode 6 MAC Cocoa) [英] Detect localization (xCode 6 MAC Cocoa)

查看:85
本文介绍了检测本地化(xCode 6 MAC Cocoa)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测本地化?

我想要类似的东西:

如果本地化是德语...

if localization is german...

否则...

推荐答案

德语"不是完整的本地化版本.这只是一种语言. OS X允许用户订购他们的语言首选项,因此您的应用程序很有可能以德语显示,但用户首选的语言是阿拉伯语(可能是因为他们要求阿拉伯语,然后是德语,然后是英语,而您没有本地化到阿拉伯语).

"German" is not a complete localization. It is just a language. OS X allows the user to order their language preferences, so it is completely possible that your application is displaying in German but the user's preferred language is Arabic (perhaps because they have requested Arabic, then German, then English, and you don't localize to Arabic).

所以您的意思是[NSLocale preferredLanguages].您可以搜索该内容并决定其行为方式.但是在几乎所有情况下,更好的答案是在本地化包中添加一个额外的标识符,然后看看您得到了什么.例如,如果在德语Localizeable.strings文件中具有:

So what you may mean is [NSLocale preferredLanguages]. You can search through that and decide how to behave. But in almost all cases, the better answer is to just add an extra identifier into your localization bundle and see what you get back. For example, if in your German Localizeable.strings file you have:

MY_LOCALE = "German";

然后您可以使用:

NSString *identifier = [[NSBundle mainBundle] localizedStringForKey:@"MY_LOCALE" value:nil table:nil];
if ([identifier isEqualToString:@"German"]) { ... }

也就是说,您很难做到这一点.苹果提供了一个丰富的本地化和国际化系统避免您这样做.除非您有一个非常专业且不寻常的问题,否则查找语言可能是错误的选择.

That said, it is incredibly rare that you should do this. Apple provides a rich localization and internationalization system to avoid you ever doing this. Unless you have a very specialized and unusual problem, looking up the language is probably the wrong thing to be doing.

正如我所说,德语"不是本地化.它是一种语言,除了语言以外,还有许多其他本地"内容(例如日期格式,数字格式等).如果您需要整个本地化,则需要使用[[NSLocale currentLocale] localeIdentifier].然后,您可以使用+componentsFromLocaleIdentifier:将其拆开,并在绝对必要的情况下处理各个部分.

And as I said, "German" is not a localization. It's a language, and there are many other "local" things beyond language (such as date formatting, number formatting, etc.) If you need the whole localization, you want to use [[NSLocale currentLocale] localeIdentifier]. You can then use +componentsFromLocaleIdentifier: to tear it apart and deal with the various parts if that's absolutely necessary.

这篇关于检测本地化(xCode 6 MAC Cocoa)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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