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?
Asked
Active
Viewed 2.1k times
25
-
5You know people can change the language in the Settings app and your app should **not** have a separate option for this, right? – May 31 '11 at 19:57
-
1You might need a separate option for this, if you want a certain language to be the fallback language, that is not the same language as the first existing localisation in the users language list. This is a very common pattern. – brainray Jun 28 '14 at 06:11
2 Answers
27
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"];

Nick Weaver
- 47,228
- 12
- 98
- 108

sorin
- 161,544
- 178
- 535
- 806
-
Thank you for your fast response. But doesn't that change the language for the entire phone? – tommazzo May 27 '11 at 10:13
-
No, you cannot change the phone configuration from code (at least "approved" code). – sorin May 27 '11 at 10:22
-
1Here is a list of all the representation of the Names of Languages: http://www.loc.gov/standards/iso639-2/php/English_list.php – SirRupertIII Nov 19 '12 at 21:47
-
9The important thing is, that this statement is executed before the app is launched: do it in main.m in the main() function, before UIApplicationMain() is called. – jbandi Jan 02 '13 at 22:12
-
4If you want the changes to take effect immediately at some time after the app is launched, add [[NSUserDefaults standardUserDefaults] synchronize]; – Chris Aug 21 '13 at 09:26
-
I have seen this beautifully simple suggestion in several places (including the call to synchronize), but for me this is totally not working... How come..? – Erik van der Neut Sep 23 '14 at 09:48
-
How do you "reinitiate" your view controller? I tried resetting even from the app delegate (by resetting window rootController) and after that tried with the tab bar controller which resets its viewControllers properties, but still the language does not change before restart. – Fawkes Mar 14 '15 at 03:16
-
I've been using this with calling synchronize after, it doesn't always work on iOS 8.3. Sometimes it will just refuse to switch no matter what. – BooRanger Jun 11 '15 at 14:31
-
@sorin . I am new localisation . i created string files for 3 languages how can i update language on button click without restarting the app – Uma Madhavi Sep 15 '17 at 09:03
4
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.

Dan J
- 25,433
- 17
- 100
- 173
-
I just went through localizing my app and I have to agree with what you're saying. +1. – Moshe May 31 '11 at 21:22
-
Thanks, this is definitely a way to do it. I'll discuss this language change thing again with my clients. An alternative, using the NIB files for localization is to ask the user if he wants to restart the app, once a language setting change is detected. – tommazzo Jun 01 '11 at 22:33