简体   繁体   English

创建本地化的iPhone应用程序,但允许用户更改应用程序的语言

[英]Creating a localized iPhone app but allowing the user to change language for the application

I'm working on a localized app and everything is working fine. 我正在开发一个本地化的应用程序,并且一切正常。 The problem is I want to allow the user to specifically select the lenguage for the specific app, in the app settings folder. 问题是我要允许用户在应用程序设置文件夹中专门为特定应用程序选择语言。 This should users that their phone is set to one language (eg french) to set the app to work in English. 用户应该将其手机设置为一种语言(例如法语),以将应用程序设置为以英语运行。

I'm currently using NSLocalizedString to get localized string but looking through all variation of the macro I can't find one that will let me specify the language. 我目前正在使用NSLocalizedString来获取本地化的字符串,但是通过宏的所有变体,我找不到一个可以让我指定语言的字符串。

Any ideas on how to do it? 关于如何做的任何想法?

There are three issues here: 这里有三个问题:

  • Strings 弦乐
  • Other resources (including NIBs) 其他资源(包括NIB)
  • System messages 系统讯息

The last is almost certainly not fixable, so we will leave it be. 最后一个几乎可以肯定是不可修复的,因此我们将保留它。 They're going to show up in the device language. 它们将以设备语言显示。

The other two are solvable, but you will need to do more things by hand. 其他两个可以解决,但是您将需要手工做更多的事情。 For strings, instead of creating a single Localizable.strings and then localizing it, create completely separate tables (English.strings, French.strings, etc.) Then, use NSLocalizedStringFromTable() , passing the language as the table. 对于字符串,而不是创建单个Localizable.strings然后对其进行本地化,而是创建完全独立的表(English.strings,French.strings等)。然后,使用NSLocalizedStringFromTable() ,将语言作为表进行传递。

For NIBs, there are two approaches. 对于NIB,有两种方法。 You can put each set of localized NIBs into its own Bundle and then pass that Bundle rather than nil to -initWithNibName:bundle: . 您可以将每组本地化NIB放入其自己的Bundle中,然后将该Bundle而不是nil传递给-initWithNibName:bundle: Alternately, you can hand-load the NIBs after finding them with [NSBundle -pathForResource:ofType:inDirectory:forLocalization:] . 或者,您可以在[NSBundle -pathForResource:ofType:inDirectory:forLocalization:]找到[NSBundle -pathForResource:ofType:inDirectory:forLocalization:]之后手动加载它们。

There is a better way to do this. 有一个更好的方法可以做到这一点。 You can force the language like so: 您可以像这样强制使用该语言:

[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];

And undo this setting by: 并通过以下方式撤消此设置:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"];

NB. 注意 you will normally have to restart the app for this to take affect. 您通常必须重新启动应用程序才能生效。

Consider if you need to call [[NSUserDefaults standardUserDefaults] synchronize]; 考虑是否需要调用[[NSUserDefaults standardUserDefaults] synchronize];

I agree there is little need to allow the user to specify a language. 我同意几乎没有必要允许用户指定语言。 However the one exception is being able to override the language and set it to the developer's native language. 但是,唯一的例外是能够覆盖该语言并将其设置为开发人员的母语。 If the user can speak the developer's language (eg English for me) then they may wish to use the App in that language, if the translations are incorrect. 如果用户会说开发人员的语言(例如,对我来说是英语),那么如果翻译不正确,则他们可能希望以该语言使用该应用程序。

I reference this answer: How to force NSLocalizedString to use a specific language (the answer doesn't actually work for me, but following the ideas in the comments did. The undo stuff I worked out. 我引用以下答案: 如何强制NSLocalizedString使用特定语言 (答案实际上对我而言不起作用,但是遵循注释中的想法起作用了。我做了一些撤销操作。

The trick to use specific language by selecting it from the app is to force the NSLocalizedString to use specific bundle depending on the selected language , 通过从应用程序中选择特定语言的技巧是强制NSLocalizedString根据所选语言使用特定捆绑软件,

here is the post i have written for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html 这是我为此http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html写的帖子

and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps 这是一个示例应用程序的代码https://github.com/object2dot0/Advance-Localization-in-ios-apps

The correct "User experience" is for the user to select their language via the system preference panel; 正确的“用户体验”是供用户通过系统偏好设置面板选择其语言的; not your app (or your app's settings panel, etc.). 不是您的应用程序(或应用程序的设置面板等)。 There is no way to override this per-app and we wouldn't want any app changing the system wide setting. 无法覆盖每个应用程序,我们也不想让任何应用程序更改系统范围的设置。

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

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