我想使用 NSLocalizedString 来更改 Mac 的语言而不是 IOS。这是我已经完成的步骤。但是,当我重新启动应用程序时,语言仍然没有改变。
- 添加新的 xib 文件以及 NSLocalizedString
- 创建一个 NSMenuItems。
然后我创建了一个 IBAction 并使用该功能与所有 NSMenuItems 连接。
-(IBAction)languageDidSelect: (id)sender { [[NSUserDefaults standardUserDefaults] setObject:[languages objectAtIndex: [sender tag]] forKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults] synchronize]; [sender setState: NSOnState]; [self relaunch]; } -(void)relaunch { isRelaunch = true; //boolean variable [[NSApplication sharedApplication]terminate:nil]; exit(0); } - (void)applicationWillTerminate:(NSNotification *)notification { //relaunch the app via NSTask if (isRelaunch) { NSString *appPath = [[NSBundle mainBundle] bundlePath]; NSTask *task = [NSTask new]; [task setLaunchPath:@"/usr/bin/open"]; [task setArguments:[NSArray arrayWithObjects:appPath, nil]]; [task launch]; //After finished relaunch, run the following function [self finishedRelaunch]; } } -(void)finishedRelaunch { NSArray* langs = [[NSUserDefaults standardUserDefaults] objectForKey: @"AppleLanguages"]; //setLanguage function is using to change the NSMenuItem state if ([langs count] > 0) [self setLanguage: [langs objectAtIndex: 0]]; else [self setLanguage: nil]; } //Use to set NSMenuItem state - (void)setLanguage:(NSString *)name { NSString* ident = [NSLocale canonicalLocaleIdentifierFromString: name]; [[[languageMenu submenu] itemWithTag: 0] setState: (nil == name) ? NSOnState : NSOffState]; [[[languageMenu submenu] itemWithTag: 1] setState: ([ident isEqual: @"en"]) ? NSOnState : NSOffState]; [[[languageMenu submenu] itemWithTag: 2] setState: ([ident isEqual: @"de"]) ? NSOnState : NSOffState]; ... }
- 结果是,当我检查 plist 时,“AppleLanguages”部分确实更改为新语言(从 jp -> de)。但是,xib 文件和字符串根本没有改变。你们能告诉我如何解决它吗?
谢谢!!!(我的开发目标是 10.5,我的基础 SDK 是 OS X 10.7)