1
//Viewcontroller.m code
NSLocalizedString(@"attributes",@"Attribute Name")

//Localizable.string code
"attributes"="attributes-french"; 

此方法非常适合 @"attributes" 的本地化

现在如果我想使用变量应该是什么代码

我在用

//Viewcontroller.m code
NSString *Value=@"attributes"
NSLocalizedString(Value,@"Attribute Name"); 

//Localizable.string code
"Value"="Value-french"; 

这是行不通的。有人能告诉我使用 NSLocalizdString 本地化变量(包含字符串)的正确方法吗?

4

2 回答 2

1

You cannot localize on a variable name. You only localize on the value held by the variable. So your Localizable.strings should contain,

"attributes"="attributes-french"

If anything, you can vary portions of the string using %@ as described here.

于 2011-06-06T20:46:18.107 回答
0

调用没有问题NSLocalizedString,而是Localizable.strings文件中的定义。

由于您将变量定义Value为“属性”,因此该函数将用作查找正确本地化字符串的键。

这应该可以正常工作:

//Viewcontroller.m code
NSString *Value=@"attributes"
NSLocalizedString(Value,@"Attribute Name"); 

//Localizable.string code
"attributes"="Value-french"; 

我在 Swift 中测试了类似的代码,然后看起来像这样:

//Viewcontroller.swift code
let Value="attributes"
NSLocalizedString(Value, comment:"Attribute Name")

//Localizable.string code
"attributes"="Value-french"; // <- don't forget the semicolon!
于 2016-02-09T09:50:47.413 回答