Arnab Hore Arnab Hore - 4 months ago 30x
Objective-C Question

how to refresh localizable.strings or NSLocalizedString(key, comment) ios during runtime

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];

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?


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];