GSLanguagePickerController 1.2.0

GSLanguagePickerController 1.2.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2017年1月

0x5e 维护。




  • 作者
  • gaosen

屏幕截图

特性

  • 没有额外的 bundle 资源
  • 支持全部语言
  • 运行时语言切换
  • 无需重新启动应用
  • 类似于系统ViewController设置 > 常规 > 语言与地区 > iPhone 语言中。

使用

显示

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

或者推入

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

您还可以监听语言变化通知 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];
}

参考

语言和地区标识符 - Apple 开发者

要求

GSLanguagePickerController 需要 iOS 8.0 及以上版本。

安装

GSLanguagePickerController 可通过 CocoaPods 获取。要安装它,只需在 Podfile 中添加以下行:

pod "GSLanguagePickerController"

已知问题

UIKit.framework 中的传统本地化包名有多种类型。

  1. [English Name].lproj

    示例:English.lprojFrench.lprojGerman.lprojSpanish.lproj

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

    示例:zh_CN.lprojzh_HK.lprojzh_TW.lprojen_GB.lproj

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

    示例:zh-Hans.lprojzh-Hant.lprojes-ES.lproj

目前,类型 2 支持不够完善,因此使用 UIKit 包资源的 UIBarButtonSystemItem 等组件可能受到影响,直到应用程序重新启动。

作者

gaosen,[email protected]

许可证

GSLanguagePickerController 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。