在开始介绍应用个本地化之前我们先来清晰两个概念,国际化和本地化。
1、国际化:简单来说就是以母语为输入原型,根据需要输出不同语言。就拿我们自己的项目来说,客户端App把语言参数传给服务端,服务端再根据这个参数把返回给客户端的数据转化成相应的语言,比如,APP当前是繁体,那么服务端返回的数据就是繁体的,App语言是英语,服务端就返回英语。(不知道其他童鞋是怎么处理的,欢迎共同讨论😄)。
2、本地化:所谓的本地化就是把App的界面资源翻译成不同的语言,图片也显示成当前语言对应的图片。
举个例子:一App要支持繁体,简体,英文三种语言。
宏定义:
#define localization(key) \
[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSUserDefaults standardUserDefaults] objectForKey:@"userLanguage"] ofType:@"lproj"]] localizedStringForKey:@key value:nil table:@"Language"]
具体操作使用
1、给你的App添加语言支持
2、在Supporting Files下新建三个Strings File文件 分别给这三个Strings File文件设置为简体,繁体,英文。
3、给三个Strings File填上内容
简体:
"ios internationa localization" = "iOS国际化本地化";
"change language" = "切换语言";
"imgTitle" = "imgSimple@2x.png"; //图片本地化
繁体:
"ios internationa localization" = "ios國際化本地化";
"change language" = "切換語言";
"imgTitle" = "imgTradition@2x.png";
英语:
"ios internationa localization" = "ios internationa localization";
"change language" = "change language";
"imgTitle" = "imgTitle@2x.png";
4、使用
情景:
有两个FirstController和SecondCotroller两个控制器,当SecondController切换语言时FirstController也要切换到相应的语言,并且app重新重新启动的时候使用的任然是最后一次切换过的语言。
FirstController
#import@interface FirstController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *textLab;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@end
#import "FirstController.h"
#import "Macro.h"
@interface FirstController ()
@end
@implementation FirstController
- (void)viewDidLoad {
[super viewDidLoad];
self.textLab.text = localization("ios internationa localization");
self.imgView.image = [UIImage imageNamed:localization("imgTitle")];
}@end
#import@interface SecondController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *textLab;
@property (weak, nonatomic) IBOutlet UIButton *changBtn;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
-(IBAction)changeBtnClick:(UIButton *)sender;
@end
`SecondController`
#import "SecondController.h"
#import "Macro.h"
@interface SecondController ()
@end
@implementation SecondController
- (void)viewDidLoad {
[super viewDidLoad];
self.textLab.text = localization("ios internationa localization");
self.imgView.image = [UIImage imageNamed:localization("imgTitle")];
self.changBtn.selected = [[[NSUserDefaults standardUserDefaults] objectForKey:@"btnState"] boolValue];
[self.changBtn setTitle:localization("change language") forState:UIControlStateNormal];
}
-(IBAction)changeBtnClick:(UIButton *)sender{
NSString *language = nil;
if (sender.selected) {
sender.selected = NO;
}else{
sender.selected = YES;
}
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
self.changBtn = sender;
if (sender.selected) { // 繁体中文
language = @"zh-Hant";
}else{ // 简体中文
language = @"zh-Hans";
}
[def setValue:language forKey:@"userLanguage"];
[def synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:nil]; // 发送切换了语言的通知
NSUserDefaults *btnDef = [NSUserDefaults standardUserDefaults];
[btnDef setBool:sender.selected forKey:@"btnState"];
[btnDef synchronize];
}@end
`AppDelegate.m`
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 监听语言的变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageChanged) name:@"changeLanguage" object:nil];
return YES;
}
App名称本地化
1.设置App名称
1>.Bundle display name,