如何强制NSLocalizedString使用特定的语言 [英] How to force NSLocalizedString to use a specific language

查看:477
本文介绍了如何强制NSLocalizedString使用特定的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 app in a different language than the device ?

推荐答案

NSLocalizedString()(及其变体)访问< $ c> NSUserDefaults 以确定用户对首选语言的设置。这返回一个语言代码数组,第一个是用户为他们的手机设置的,后续的一个用作回退,如果资源不可用首选语言。 (在桌面上,用户可以在系统偏好设置中使用自定义顺序指定多种语言)

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使用特定的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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