前期准备工作 https://www.jianshu.com/p/e7a3b9d610a2
主要包括添加需要本地化的语言,添加配置文件等步骤。
在Supporting Files文件夹下,New File -> Strings File。
创建的.strings文件命名为Language ,不可以命名为Localizable。
在需要手动修改语言的文件中添加以下代码:
//添加一个显示切换项目语言的按钮
- (void)btnClick
{
UIAlertController *alert =[UIAlertController alertControllerWithTitle:Localized(@"change") message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *left = [UIAlertAction actionWithTitle:Localized(@"cancel") style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *simpleC = [UIAlertAction actionWithTitle:Localized(@"chinese") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self chineseClick];
}];
UIAlertAction *comC = [UIAlertAction actionWithTitle:Localized(@"chineseC") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self chineseCClick];
}];
UIAlertAction *english = [UIAlertAction actionWithTitle:Localized(@"english") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self englishClick];
}];
[alert addAction:left];
[alert addAction:simpleC];
[alert addAction:comC];
[alert addAction:english];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)chineseClick
{
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"appLanguage"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:LanguageChanged object:nil];
}
- (void)chineseCClick
{
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:@"appLanguage"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:LanguageChanged object:nil];
}
- (void)englishClick
{
[[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"appLanguage"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:LanguageChanged object:nil];
}
//添加语言发生变化的通知
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//添加通知中心,监听语言改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageChanged) name:LanguageChanged object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//移除通知中心
[[NSNotificationCenter defaultCenter] removeObserver:self name:LanguageChanged object:nil];
}
//重新加载根视图控制器
- (void)languageChanged
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate mainVC];
}
//appDelegate.m
- (void)mainVC
{
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[MasterViewController new]];
self.window.rootViewController = nav;
}
可以定义一个宏,方便本地化相应的字符串。
//获取当前保存在NSUserDefaults的本地语言
#define currentLanguage [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"]]
//根据获取语言文件所在路径
//文件名类型Type为lproj,即.lproj的文件夹. zh-Hans.lproj和en.loproj
//存在NSUserDefaults的适合,中英文就分别设置zh-Hans和en,不可改变。
#define LanguagePath [[NSBundle mainBundle] pathForResource:currentLanguage ofType:@"lproj"]
//根据键值获取返回转换结果
//Language为语言文件名Language.string
#define Localized(key) [[NSBundle bundleWithPath:LanguagePath] localizedStringForKey:(key) value:nil table:@"Language"]
Localized(@"SpecialUserActionDanmu")
效果如下: