即时更改 iOS 应用程序的语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6150576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:07:05  来源:igfitidea点击:

Change iOS app's language on the fly

iphoneobjective-cioslocalizationxcode4

提问by tommazzo

I'm writing an iOS app, where I want the user to be able to change the UI language independent of the iPhone's or iPad's language. The question is, how do I reload the appropriate NIB file for the currently displayed view when the language changes and how do I load the appropriate .strings file so that NSLocalizedStringworks appropriately?

我正在编写一个 iOS 应用程序,我希望用户能够独立于 iPhone 或 iPad 的语言更改 UI 语言。问题是,当语言更改时,如何为当前显示的视图重新加载适当的 NIB 文件,以及如何加载适当的 .strings 文件以使其NSLocalizedString正常工作?

回答by sorin

This should do the trick, assuming that deis the new language selected by the user. Also assure that you are reinitiating the current view.

假设这de是用户选择的新语言,这应该可以解决问题。还要确保您正在重新启动当前视图。

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

回答by Dan J

Personally, I don't like the idea of giving NIB files to translators. Instead, I design the NIBs so there is enough space for non-English languages (typically they will require 50% more room for text), and then I store all text resources in Localizable.strings files.

就个人而言,我不喜欢将 NIB 文件提供给翻译人员的想法。相反,我设计了 NIB,以便为非英语语言提供足够的空间(通常它们需要 50% 以上的文本空间),然后我将所有文本资源存储在 Localizable.strings 文件中。

Then, in my code I set the text for each UI control.

然后,在我的代码中,我为每个 UI 控件设置了文本。

[_myButton setTitle:NSLocalizedString(@"My button title", 
                                      @"My description for translation file, e.g. including chars limit")
           forState:UIControlStateNormal];

I find this makes the translation process easier to manage.

我发现这使得翻译过程更容易管理。

To answer your original question, you would put this code in the viewWillAppearfunction of your UIViewto ensure it is reloaded each time the UI reappears.

要回答您的原始问题,您可以将此代码放在viewWillAppear您的函数中,UIView以确保每次 UI 重新出现时都会重新加载它。