在头文件中使用变量和常量定义进行本地化

时间:2012-02-11 00:08:43

标签: iphone objective-c ios xcode localization

当我有一个标题我定义了一些参数时,如何在这种情况下使用NSLocalizedString,例如:

#define appKey @"appKey1 is: %@"

我想我知道我的Localizable.strings应该是这样的:

"blabla" = "appKey1 is: %@"

但是如何使用NSLocalizedString?我读过我需要使用stringWithFormat,但不知道如何......

谢谢!

4 个答案:

答案 0 :(得分:3)

您可以将常量定义为:

#define appKey NSLocalizedString(@"appKey1 is: %@", @"appkey constant")

然后它应该以通常的方式被genstrings工具拿起。

在字符串文件中,它会像这样出现:

/* appkey constant */
"appKey1 is: %@" = "appKey1 is: %@";

你会翻译右手边。

答案 1 :(得分:2)

NSLocalizedStrings中可接受字符串文字。你需要做的是像

#define appKey NSLocalizedString(BlahBlah , comments);

"BlahBlah" = "appKey1 is: %@";

(一定要在Localizable.strings中用分号结束你的行,否则它最终会被破坏)。

答案 2 :(得分:1)

这就是你通常会这样做的方式,

NSString * myString = [NSString stringWithFormat:@"appKey1 is: %@",yourAppKeyString];

既然已经定义了它,你可以像这样使用它

NSString * myString = [NSString stringWithFormat:appKey,yourAppKeyString];

两种情况都会填充你的myString,如此

yourAppKeyString = @"keyString";
myString = @"appKey1 is: keyString";

答案 3 :(得分:0)

NSString * myString = [NSString stringWithFormat: NSLocalizedString(@"appKey", @""),yourAppKeyString];
相关问题