在Objective-C中读取.strings文件?

Zeu*_*eus 9 objective-c

我正在创建一个Mac应用程序,我想本地化我的标签.我认为.strings文件是更好的选择.但是我.strings在Objective-C中读取文件时遇到了麻烦.我正在寻找一种更简单的方法.

这是我的.string文件内容:

"LABEL_001" = "Start MyApp";
"LABEL_002" = "Stop MyApp";
"LABEL_003" = "My AppFolder";
...
Run Code Online (Sandbox Code Playgroud)

我已经查看了http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/LoadingResources/Strings/Strings.html.

这是我的代码:

NSBundle *bundle = [NSBundle mainBundle];
NSString *strFilePath = [[NSBundle mainBundle] pathForResource:@"Labels" ofType:@"strings"];
NSString *tt =NSLocalizedStringFromTableInBundle(@"LABEL_001",strFilePath,bundle, nil);
NSLog(@"STRING ::: %@",tt);
Run Code Online (Sandbox Code Playgroud)

但字符串tt给出了"LABEL_001",我想要"Start MyApp"

我究竟做错了什么?

小智 26

一.您必须在应用程序包Localizable.strings<LANGUAGENAME>.lproj目录中命名您的文件.

二.使用NSLocalizedString宏:

NSString *loc = NSLocalizedString(@"LABEL_001", nil);
Run Code Online (Sandbox Code Playgroud)

三.如果没有任何效果,您可以使用字符串文件初始化NSDictionary,因为字符串文件是一种特殊类型的plist:

NSString *fname = [[NSBundle mainBundle] pathForResource:@"whatever" ofType:@"strings"];
NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:fname];
NSString *loc = [d objectForKey:@"LABEL_001"];
Run Code Online (Sandbox Code Playgroud)