Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change iPhone app language during runtime?

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.

like image 679
shinjin Avatar asked Oct 16 '09 08:10

shinjin


People also ask

What language do iPhone apps run?

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.


2 Answers

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* languages = [userDefaults objectForKey:@"AppleLanguages"];
[languages insertObject:@"de" atIndex:0]; // ISO639-1
[[NSUserDefaults standardUserDefaults] synchronize];
like image 65
YellowSnail Avatar answered Oct 05 '22 18:10

YellowSnail


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

like image 32
object2.0 Avatar answered Oct 05 '22 20:10

object2.0