在 Objective-C 中读取 .strings 文件? [英] Read .strings file in Objective-C?

查看:51
本文介绍了在 Objective-C 中读取 .strings 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 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";
...

我已经看过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);

但是字符串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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆