Support in-app runtime language switch, without application relaunch.
Clone or download
Latest commit 1da8074 Oct 4, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example disable UIBarButtonItem test Jan 24, 2017
GSLanguagePickerController Custom title fix Oct 4, 2017
.gitignore Initial commit Jan 23, 2017
.travis.yml update travis-ci Jan 24, 2017
CHANGELOG.md update screenshoot Jan 24, 2017
GSLanguagePickerController.podspec update podspec Jan 24, 2017
LICENSE Initial commit Jan 23, 2017
README.md Custom title fix Oct 4, 2017
_Pods.xcodeproj Initial commit Jan 23, 2017
screenshoot.gif fix cocoapods doc Jan 24, 2017

README.md

GSLanguagePickerController

CI Status Version License Platform

Screenshoot

Features

  • No additional bundle resources
  • Full language supported
  • Runtime language switch
  • No relaunch application
  • Much like system ViewController in Settings > General > Language & Region > iPhone Language.

Usage

Present GSLanguagePickerController:

- (IBAction)selectLanguageAction:(UIButton *)button {
    GSLanguagePickerController *vc = [GSLanguagePickerController new];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:navi animated:YES completion:nil];
}

Or push GSLanguagePickerController:

- (IBAction)selectLanguageAction:(UIButton *)button {
    GSLanguagePickerController *vc = [GSLanguagePickerController new];
    [self.navigationController pushViewController:vc animated:YES];
}

And you can also observing language changed notification NSCurrentLocaleDidChangeNotification:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageChanged:) name:NSCurrentLocaleDidChangeNotification object:nil];
}

- (void)languageChanged:(NSNotification *)notification {
    NSString *languageId = notification.object;
    NSLog(@"Language has changed: %@", languageId);
    
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[NSBundle defaultLanguage]];
    NSString *languageName = [locale displayNameForKey:NSLocaleIdentifier value:languageId];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Language has changed" message:[NSString stringWithFormat:@"%@: %@", languageId, languageName] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:action];
    [self presentViewController:alertController animated:YES completion:nil];
    
    // TODO: reload viewcontroller or other works
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Customizing

In order to color/font match your project. There are five simple configuration options. Find them below:

vc.cellSelectedFontColor = [UIColor whiteColor];
vc.cellSelectedBackgroundColor = [UIColor blackColor];
vc.cellTintColor = neoGreenColor;
vc.cellFont = [UIFont fontWithName:@"HelveticaNeue" size:16.0];
vc.cellDetailFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];

There are cases you don't want to press the done button to change the language. You can call a boolean on the view controller like this:

vc.useDoneButton = NO;

This way, the done button will be hidden and the notification will be dispatched on row select.

You can also set the title for the VC by specifying the title:

vc.title=@"This is my title"

Reference

Language and Locale IDs - Apple Developer

Requirements

GSLanguagePickerController require iOS 8.0+.

Installation

GSLanguagePickerController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "GSLanguagePickerController"

Known issue

The legacy localization bundle name in UIKit.framework has various types.

  1. [English Name].lproj

    Examples: English.lproj, French.lproj, German.lproj, Spanish.lproj

  2. [Language Id]_[Region Id].lproj

    Examples: zh_CN.lproj, zh_HK.lproj, zh_TW.lproj, en_GB.lproj

  3. [Language Id]-[Script Id].lproj

    Examples: zh-Hans.lproj, zh-Hant.lproj, es-ES.lproj

At present, type 2 can't be supported well, so UIBarButtonSystemItem and other components who uses UIKit bundle resource, may be affected until application relaunch.

Author

gaosen, 0x5e@sina.cn

License

GSLanguagePickerController is available under the MIT license. See the LICENSE file for more info.