5

I'm trying to change app language, but when I run this code in main.h language chages after I shut down an App and run it again. Is this possible to change language without restarting?

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSArray *languages = [NSArray arrayWithObject:@"en_GB"];
    [[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}
1

1 Answer 1

3

Update the answer "How to change the languages within the app"

NSLocalizedString() (and variants thereof) access the "AppleLanguages" key in NSUserDefaults to determine what the user's settings for preferred languages are. This returns an array of language codes, with the first one being the one set by the user for their phone, and the subsequent ones used as fallbacks if a resource is not available in the preferred language.

You can override the global setting for your own application if you wish by using the setObject:forKey: method to set your own language list just like you've done it. This will take precedence over the globally set value and be returned to any code in your application that is performing localization. The code for this would look something like:

[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"]; 

Note: To be on safe side make sure that you use the appropriate pre-defined languages name.

Below is the code snippet, but you MUST have all localization files in your project.

@implementation LocalizeLanguage

static NSBundle *bundle = nil;

+(void)initialize {
     NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
     NSArray* languages = [defs objectForKey:@"AppleLanguages"];
     NSString *current = [[languages objectAtIndex:0] retain];
     [self setLocalizeLanguage:current];
}

/*
  [LocalizeLanguage setLocalizeLanguage:@"en"];
  [LocalizeLanguage setLocalizeLanguage:@"fr"];
*/

+(void)setLocalizeLanguage:(NSString *)lang {
     NSLog(@"preferredLang: %@", lang);
     NSString *path = [[ NSBundle mainBundle ] pathForResource:lang ofType:@"lproj" ];
     bundle = [[NSBundle bundleWithPath:path] retain];
}

+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
    return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end
7
  • 3
    Ok, but I have done this already:) And everything works perfectlly fine. I want to add a possibility to change language in application Settings.app, so when the current iPhone language is English. but user set French i Settings.app for my application I have to force my application to localize in French. I'm trying to do that using code I wrote earlier and it works but I need to restart application after that and this is the problem I'am trying to solve.
    – pawelini1
    May 6, 2011 at 14:10
  • 1
    I got the exact same problem! Mar 15, 2012 at 19:33
  • 4
    When using this code and change language from inside the app ios standard buttons(done,save...) don't change until the app is restarted. Is there a way to do it with out restart? May 1, 2013 at 15:37
  • 1
    @Deminem thanks I have seen this page before, It helps in localizing every other control than the already created iOS standard controls. I have to restart the app to get the OS recreate them in the new locale. May 9, 2013 at 16:02
  • 1
    this should not be the answer to the question
    – JAHelia
    May 28, 2014 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.