1

I'm working on a app that supports multiple language. Everything is going well. But I need to change the language during runtime.
I'm doing it this way -

NSArray* languages = [NSArray arrayWithObjects:@"es",@"en" nil];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

It is working well.

NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];

This langID is showing the correct language. But the app is not changing its language until its been restarted.

Is there any way to refresh localizable.strings file or NSLocalizedString(key, comment)? Or any other way to do it without restarting?

Arnab
  • 4,216
  • 2
  • 28
  • 50

2 Answers2

3

This has been asked many times already here on StackOverflow (search for things like iOS change language at runtime).

You can't change the language at runtime when using NSLocalizedString. If you really, really need that, you need to use a custom localization system instead. Better yet: simply don't do that: it's unexpected. After all, the user can set his/her preferred languages (and their order) in the system settings.

Someone even asked an Apple engineer and this is the response:

In general, you should not change the iOS system language (via use of the AppleLanguages pref key) from within your application. This goes against the basic iOS user model for switching languages in the Settings app, and also uses a preference key that is not documented, meaning that at some point in the future, the key name could change, which would break your application.

If you want to switch languages in your application, you can do so via manually loading resource files in your bundle. You can use NSBundle:pathForResource:ofType:inDirectory:forLocalization: for this purpose, but keep in mind that your application would be responsible for all loading of localized data.

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • thanks for the answer. I had separately created two storyboards for two different languages and calling them accordingly. – Arnab May 31 '16 at 14:07
0

To change the app language during runtime, I have done this manually by creating two different storyboards. I saved the language preference in NSUserDefaults but NOT with the key AppleLanguages and then called AppDelegate's didFinishLaunchingWithOptions method to select storyboard.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainEnglish" bundle:nil];
UINavigationController *navigationcontroller=[[UINavigationController alloc]init];
RegistorViewController *registor=[storyboard instantiateViewControllerWithIdentifier:@"Registor"];
[self.window setRootViewController:navigationcontroller];
[self.window makeKeyAndVisible];
[navigationvontroller pushViewController:registor animated:NO];
Arnab
  • 4,216
  • 2
  • 28
  • 50