Is there a way to change the application language during runtime?
So, after the change NSLocalizedString
immediately returns the string for the new language.
What I'm doing now is changing the language using the code below:
- (void)onChangeLanguage: (id)sender
{
NSArray *lang = [NSArray arrayWithObjects:((InfoWhatever *)sender).language, nil];
[[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"AppleLanguages"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
NSLog(@"Current language: %@", currentLanguage);
}
The language will change but only after restarting the app.
Swift is a robust and intuitive programming language created by Apple for building apps for iOS, Mac, Apple TV, and Apple Watch. It's designed to give developers more freedom than ever.
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* languages = [userDefaults objectForKey:@"AppleLanguages"];
[languages insertObject:@"de" atIndex:0]; // ISO639-1
[[NSUserDefaults standardUserDefaults] synchronize];
The trick to use specific language by selecting it from the app is to force the NSLocalizedString to use specific bundle depending on the selected language ,
here is the post i have written for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html
and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With