问题描述
在 iPhone 上 NSLocalizedString
返回 iPhone 语言的字符串.是否可以强制 NSLocalizedString
使用特定语言来拥有应用程序使用与设备不同的语言?
On iPhone NSLocalizedString
returns the string in the language of the iPhone.Is it possible to force NSLocalizedString
to use a specific language to have the appin a different language than the device ?
推荐答案
NSLocalizedString()
(及其变体)访问 NSUserDefaults
中的AppleLanguages"键来确定什么用户对首选语言的设置是.这将返回一组语言代码,第一个是用户为其手机设置的语言代码,如果资源在首选语言中不可用,则后续代码用作后备.(在桌面上,用户可以在系统偏好设置"中自定义排序来指定多种语言)
NSLocalizedString()
(and variants thereof) access the "AppleLanguages" key in NSUserDefaults
to determine what the user's settings for preferred languages are. This returns an array of language codes, with the first one being the one set by the user for their phone, and the subsequent ones used as fallbacks if a resource is not available in the preferred language. (on the desktop, the user can specify multiple languages with a custom ordering in System Preferences)
如果您愿意,您可以通过使用 setObject:forKey: 方法设置您自己的语言列表来覆盖您自己的应用程序的全局设置.这将优先于全局设置值并返回到您的应用程序中执行本地化的任何代码.代码如下所示:
You can override the global setting for your own application if you wish by using the setObject:forKey: method to set your own language list. This will take precedence over the globally set value and be returned to any code in your application that is performing localization. The code for this would look something like:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
这将使德语成为您申请的首选语言,英语和法语作为后备语言.您可能希望在应用程序启动的早期某个时候调用它.您可以在此处阅读有关语言/地区首选项的更多信息:国际化编程主题:获取当前语言和区域设置
This would make German the preferred language for your application, with English and French as fallbacks. You would want to call this sometime early in your application's startup. You can read more about language/locale preferences here: Internationalization Programming Topics: Getting the Current Language and Locale
这篇关于如何强制 NSLocalizedString 使用特定语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!