/LXLocalizationLanguage

Use code to change iOS App Language.

Primary LanguageObjective-C

LXLocalizationLanguage

Use code to change the application text language.

localizationsLanguage.gif

Integration step

  1. Create your ‘Localizable.strings’ file and create your strings in different languages.

  2. Define macro
#define Localized(key)  [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:nil table:@"Localizable"]
And use this macro to define your string.
  1. Implement function
- (void)changeLanguage:(id)sender{
    // get current app language
    NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"];
    
    // switch language between 'en' and 'zh-Hans'
    if ([language isEqualToString: @"en"]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"appLanguage"];
    }else {
        [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"appLanguage"];
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    // refreash windows.
    ViewController *vc = [[ViewController alloc] init];
    [delegate refresh:vc];
}
  1. Implement function
- (void)refresh:(UIViewController *)viewController;

In AppDelegate to force refresh related ViewController.