Latest | 1.2.0 |
---|---|
Homepage | https://github.com/0x5e/GSLanguagePickerController |
License | MIT |
Platforms | ios 8.0 |
Frameworks | UIKit, Foundation |
Authors |
Screenshoot
Features
- No additional bundle resources
- Full language supported
- Runtime language switch
- No relaunch application
- Much like system
ViewController
inSettings > General > Language & Region > iPhone Langage
.
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];
}
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.
-
[English Name].lproj
Examples:
English.lproj
,French.lproj
,German.lproj
,Spanish.lproj
-
[Language Id]_[Region Id].lproj
Examples:
zh_CN.lproj
,zh_HK.lproj
,zh_TW.lproj
,en_GB.lproj
-
[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, [email protected]
License
GSLanguagePickerController is available under the MIT license. See the LICENSE file for more info.
Latest podspec
{ "name": "GSLanguagePickerController", "version": "1.2.0", "summary": "Support in-app runtime language switch, without application relaunch.", "description": "GSLanguagePickerController is the imitate version of `Settings > General > Language & Region > iPhone Langage` ViewController. Support dynamic language switch, without application relaunch. The localizedString comes from `UIKit.framework/*.lproj` & `NSLocale`, no additional resources required.", "homepage": "https://github.com/0x5e/GSLanguagePickerController", "screenshots": "https://raw.githubusercontent.com/0x5e/GSLanguagePickerController/master/screenshoot.gif", "license": { "type": "MIT", "file": "LICENSE" }, "authors": { "gaosen": "[email protected]" }, "source": { "git": "https://github.com/0x5e/GSLanguagePickerController.git", "tag": "1.2.0" }, "platforms": { "ios": "8.0" }, "source_files": "GSLanguagePickerController/Classes/**/*", "frameworks": [ "UIKit", "Foundation" ] }
Sat, 18 Feb 2017 06:40:06 +0000