应用程序中加载语言的 NSLocale
在我的界面中,我使用以下方式显示区域设置的显示名称:
[[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier]
但这使用系统的区域设置给出显示名称,该系统区域设置并不总是与应用程序的区域设置相同。例如,如果我将系统设置为法语,而我的应用程序没有法语本地化,则它将在英语界面中提供法语显示名称。相反,我想在英文界面中使用英文显示名称,这样我就不会混合使用这些语言。
In my interface I'm showing the display name of a locale with this:
[[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier]
But this gives the display name using the locale of the system which is not always the same as the locale of the app. For example, if I set my system in French and my app doesn't have a French localisation, it will give a French display name in an English interface. Instead I'd like to have an English display name in an English interface so I don't mix the languages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是您想要的...但这是查看您的应用程序运行的语言/本地化的好方法:
如果您的应用程序支持英语和法语,它将返回两者的数组首选顺序。在测试中,0 处的似乎是已加载并运行的 xib。
I'm not sure if this is what you want... But this is a great way to see what language/localization your app is running in:
If you app supports both English and French languages, it will return an array of both in the preferred order. On testing, the one at 0 seems to be the xib that is loaded and running.
您应该创建一个本地化的 plist 文件,在您的 plist 中存储相应本地化的正确区域设置标识符,当您使用代码片段时,您应该从该本地化 plist 文件加载区域设置并使用它来获取显示名称。
这样,您的区域设置将始终与屏幕界面语言相对应。我需要对一些我想要格式化的日期执行此操作,而不是依赖系统,因为日历格式可以用另一种语言设置到系统...
希望有帮助...
并像这样使用它:
为了避免输入的区域设置字符串拼写错误等的风险,或者如果您想要更好的面向代码的解决方案,您当然可以使用名为“default_language”的属性作为 plist 中的 BOOL 并将其设置为 YES 仅适用于英语,在您的代码中只需检查此值,如果为 NO,则获取设备区域设置,如果为 YES,那么您就知道该应用程序是英语,因为这是设备设置,或者是英语,因为它不支持设备的当前区域设置,因此默认情况下已回退到英语...所以“default_language”应该为 YES,将区域设置硬编码为 en_US,或者en_UK 如果你是爱国的英国人......
这个例子中的一些东西可能会解决你的问题,NSLocale的一个类别,但是当然这会让你需要为你支持的每种语言都有一个本地化的plist......
You should create a plist file which you localise, in your plist store the correct locale identifier for the corresponding localisation, when you use your snippet of code, you should load the locale from that localised plist file and use that to get the display name.
That way your locale will always correspond to the on screen interface language. I needed to do this for some dates I wanted to format, and not rely on the system as the calendar format could be set in another language to the system...
hope that helps...
And use it like so:
To avoid the risk of those locale strings being entered being misspelt etc, or if you would like a better code oriented solution, you could of course use a property named "default_language" as a BOOL in the plist and set it to YES only for english, in your code just check this value and if it is NO, then get the device locale, should it be YES, then you know that the app is either in english because that is the device setting, or in english because it doesn't support the current locale of the device and therefore has fallen back onto english by default... so should "default_language" be YES, hard code the locale to en_US, or en_UK if you're patriotic brit...
Something along the lines of this example might solve your problem, a category for NSLocale, but of course this would make you need to have a localised plist for each language you support...