[Cocoa]_[多语言翻译]

Cocoa内置多语言翻译,相对其它平台要方便得多。

1.首先创建一个strings文件,命名为Localizable.strings.

2.选中新建的Localizable.strings文件,在窗口右边点击Localize...,弹出的窗口点击"Localize"按钮确认。

如下图:


3.在如下位置添加其它语言,我们这里只添加简体中文演示


4.两个string文件要像如下编辑


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(@"%@",<span style="font-family: Menlo;">currentLanguage</span>);
    [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];
   
}


你可能感兴趣的:([Cocoa]_[多语言翻译])