Change iOS app's language on the fly
I'm writing an iOS app, where I want the u开发者_高级运维ser 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?
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"];
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.
精彩评论