Change iOS app's language on the fly

2019-01-03 09:15发布

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 NSLocalizedString works appropriately?

2条回答
祖国的老花朵
2楼-- · 2019-01-03 09:42

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.

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

[_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 viewWillAppear function of your UIView to ensure it is reloaded each time the UI reappears.

查看更多
做自己的国王
3楼-- · 2019-01-03 09:52

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

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", nil] 
                                          forKey:@"AppleLanguages"];
查看更多
登录 后发表回答