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;
}
pawelini1
  • 487
  • 7
  • 15

1 Answers1

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
Deminem
  • 672
  • 8
  • 19
  • 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 06 '11 at 14:10
  • I have updated the answer - but it isn't necessary to do what you want to do. iOS has support for localization built into the OS — all you need to do is supply the appropriate language files and the user can select which language he wants. But anyways this will work for you - good luck! :) – Deminem May 06 '11 at 19:29
  • 1
    I got the exact same problem! – Van Du Tran Mar 15 '12 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? – Khaled Annajar May 01 '13 at 15:37
  • @khaledannajar - To switch between different localizations on fly without restarting the app - you need to do some customisation through NSLocalizedString. One of the guy has already done here - http://aggressive-mediocrity.blogspot.sg/2010/03/custom-localization-system-for-your.html – Deminem May 09 '13 at 09:04
  • 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. – Khaled Annajar May 09 '13 at 16:02
  • 1
    this should not be the answer to the question – JAHelia May 28 '14 at 10:59