Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change iOS app's language on the fly

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?

like image 213
tommazzo Avatar asked May 27 '11 09:05

tommazzo


People also ask

How do I change the language of IOS apps?

Change The App Language in iPhone or iPad AppsTap on the Settings app on the Home screen. Scroll down, select the app you wish to change its language. Select Language under Preferred Language. Choose the language you want to use.


2 Answers

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"];
like image 172
sorin Avatar answered Oct 04 '22 11:10

sorin


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.

like image 41
Dan J Avatar answered Oct 04 '22 11:10

Dan J