NSLocalizedString只有一半的时间可以恢复所引用键的预期值.其他时候我回到NSLocalizedString中指定的密钥名称,并且在每次运行时都会一直发生.
目前,我目前只支持英语.
我打电话给:
NSString *someText = NSLocalizedString(@"mystring.keyname",nil);
en.lproj / Localizable.strings的内容:
"mystring.keyname" = "Hello there!";
当它无法正常工作时,someText的值为mystring.keyname.
以下是我测试可靠性的方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSLog(@"Strings file: %@",[bundle pathForResource:@"Localizable" ofType:@".strings"]); NSLog(@"Localizations: %@",[bundle localizations]); NSLog(@"Local Dict: %@",[bundle localizedInfoDictionary]); NSLog(@"localizedStringForKey: %@",[bundle localizedStringForKey:@"mystring.keyname"value:@"Wha happened?" table:nil]); NSLog(@"Localized String: %@",NSLocalizedString(@"mystring.keyname",@"Are you sure you want to start a new game?")); }
这会在每次运行时打印出预期值:
Run #1: Local Dict: (null) localizedStringForKey: Hello there! Localized String: Hello there! Run #2: Local Dict: (null) localizedStringForKey: Wha happened? Localized String: mystring.keyname
我的文件布局如下:
WORKSPACE_DIR/ my_workspace_name.xcworkspace PROJECT_DIR/ - my_project_name.xcodeproj RESOURCES_DIR/ - en.lproj/ Localizable.strings
XCode的项目级设置:
1) Build Phases has Localizable.strings listed in the "Copy Bundle Resources". 2) Build Settings: Convert Copied Files => YES Property List Output Encoding => binary Strings file Output Encoding => UTF-16 3) Info.plist's: CFBundleDevelopmentRegion => 'en'
Localizable.strings配置为UTF-16虽然我没有在文件检查器中看到选中了字符串文件的文本设置选项.但我已经通过在TextWrangler中打开文件来验证编码类型,该文件使用Unix LF将编码显示为Unicode(UTF-16).
关于如何让这个工作一直有效的任何建议?
解决方法
如果您的localisable文件中有任何额外的分号,它会构建但无法进行本地化.