占位符和NSLocalizedString

And*_*rea 6 cocoa objective-c nslocalizedstring

我正在尝试翻译我的应用程序,当有半个占位符时,我发现很难翻译.我需要找到以下代码:

[textView1 insertText:[NSString stringWithFormat:@"%@ è il %i/%i/%i. Il giorno delle settimana è %@. La Festa è compresa nel calcolo. \n", nomeFesta, giornoFesta, meseFesta, annoFestaModificato, ggSett]];
Run Code Online (Sandbox Code Playgroud)

我把文件localizable.string(英文):

"festaCompresa" = @"%@ is the %i/%i/%i. the day of the week is %@. The holidays is included in the calculation. \n";
Run Code Online (Sandbox Code Playgroud)

然后我编辑了这段代码:

[textView1 insertText:[NSString stringWithFormat:NSLocalizedString(@"festaCompresaW, @""), nomeFesta, giornoFesta, meseFesta, annoFestaModificato, ggSett]];
Run Code Online (Sandbox Code Playgroud)

这是行不通的.

CRD*_*CRD 6

你的字符串文件有一个小错误,你已经包含了一个@字符串作为NSString常量 - 文件格式使用引号中的字符串:

"festaCompresa" = "%@ is the %i/%i/%i. the day of the week is %@. The holidays is included in the calculation. \n";
Run Code Online (Sandbox Code Playgroud)

BTW:在为本地化创建格式字符串时,您可能需要使用位置格式,其中每个格式规范都包含参数的编号.例如:

"festaCompresa" = "%1$@ is the %2$i/%3$i/%4$i. the day of the week is %@. The holidays is included in the calculation. \n";
Run Code Online (Sandbox Code Playgroud)

在上面的字符串中显然不需要这样,因为参数包含在它们提供的顺序中.但是在某些语言中,它们可能需要采用不同的顺序,这就是它的完成方式.


Nov*_*arg 5

您是否复制粘贴了代码?还是您重新输入了?因为如果您复制粘贴了它,问题就出在这里:

[textView1 insertText:[NSString stringWithFormat:NSLocalizedString(@"festaCompresaW, @""), nomeFesta, giornoFesta, meseFesta, annoFestaModificato, ggSett]];
Run Code Online (Sandbox Code Playgroud)

我想应该是

[textView1 insertText:[NSString stringWithFormat:NSLocalizedString(@"festaCompresa", @""), nomeFesta, giornoFesta, meseFesta, annoFestaModificato, ggSett]];
Run Code Online (Sandbox Code Playgroud)

所以基本上是一个"代替W

另外,在Localizable.strings中,您不要放在@引号前面,因此:

"festaCompresa" = @"%@ is the %i/%i/%i. the day of the week is %@. The holidays is included in the calculation. \n";
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

"festaCompresa" = "%@ is the %i/%i/%i. the day of the week is %@. The holidays is included in the calculation. \n";
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你