在 Objective-C 中读取 .strings 文件? [英] Read .strings file in Objective-C?
问题描述
我正在创建一个 Mac 应用程序,我想本地化我的标签.我认为 .strings
文件会是更好的选择.但是我无法在 Objective-C 中读取 .strings
文件.我正在寻找一种更简单的方法.
I'm creating a Mac app and I want to localize my Labels. I thought a .strings
file would be a better choice. But I have trouble reading .strings
file in Objective-C. I'm looking for a simpler method.
这是我的.string
文件内容:
"LABEL_001" = "Start MyApp";
"LABEL_002" = "Stop MyApp";
"LABEL_003" = "My AppFolder";
...
这是我的代码:
NSBundle *bundle = [NSBundle mainBundle];
NSString *strFilePath = [[NSBundle mainBundle] pathForResource:@"Labels" ofType:@"strings"];
NSString *tt =NSLocalizedStringFromTableInBundle(@"LABEL_001",strFilePath,bundle, nil);
NSLog(@"STRING ::: %@",tt);
但是字符串tt给出了"LABEL_001"
,我想要"Start MyApp"
But the string tt gives "LABEL_001"
, I want "Start MyApp"
我做错了什么?
推荐答案
一个.您必须在应用程序包的 <LANGUAGENAME>.lproj
目录中将您的文件命名为 Localizable.strings
.
One. You have to name your file Localizable.strings
in the <LANGUAGENAME>.lproj
directory in the app bundle.
两个.使用 NSLocalizedString
宏:
NSString *loc = NSLocalizedString(@"LABEL_001", nil);
三个.如果没有任何效果,您可以使用字符串文件初始化 NSDictionary,因为字符串文件是一种特殊类型的 plist:
Three. If nothing works, you can initialize an NSDictionary using a strings file, as a strings file is a special type of plist:
NSString *fname = [[NSBundle mainBundle] pathForResource:@"whatever" ofType:@"strings"];
NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:fname];
NSString *loc = [d objectForKey:@"LABEL_001"];
这篇关于在 Objective-C 中读取 .strings 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!