NSLocalizedString 仅在我取回所引用键的预期值的一半时间内起作用。其他时候,我取回 NSLocalizedString 中指定的键名,并且在每次运行时始终如一地发生。
目前,我目前只支持英语。
我打电话给:
NSString *someText = NSLocalizedString(@"mystring.keyname", nil);
en.lproj/Localizable.strings 的内容:
"mystring.keyname" = "Hello there!";
当它不能正常工作时,someText's
值为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)。
关于如何让它一直工作的任何建议?