Cocoa多语言翻译步骤:
1.创建一个strings文件,命令:“Command+N”,命名:Localizable.strings.
2.选中新建的Localizable.strings文件,在窗口右边点击Localize...,弹出的窗口点击"Localize"按钮确认。
3.如下图所示,添加多语言Chinese。
4.在Localizable.strings.文件里面进行编辑。如下所示:
English Chinese
5.对字符串进行翻译设置显示。
#define AppLanguage @"TestLanguage"
#define CustomLocalizedString(key, comment)
[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:AppLanguage]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:nil]
6.具体实现示例:
-(void) doTranslation
{
NSString *currentLanguage = [[NSUserDefaults standardUserDefaults]objectForKey:AppLanguage];
NSLog(@"%@",currentLanguage);
[engField setStringValue:CustomLocalizedString(@"English", nil)];
[chinaField setStringValue:CustomLocalizedString(@"Chinese", nil)];
}
-(IBAction)onEnglish:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:AppLanguage];
[self doTranslation];
}
-(IBAction)onChinese:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:AppLanguage];
[self doTranslation];
}