iOS:如何使用多个插值参数本地化字符串?

时间:2014-06-11 10:31:49

标签: ios objective-c localization internationalization nslocalizedstring

如何使用NSLocalizedString构建具有多个参数的字符串,同时让翻译控件在需要时更改顺序?

我的Localizable.string中的一个例子是:

"score_out_of"="Your score is %i out of %i";

将像

一样调用
[NSString stringWithFormat:NSLocalizedString(@"score_out_of", nil), correct, total];

但是在某些语言环境中,语法规则可能会指示总数在正确之前。在Objective C中,似乎插值顺序是硬编码的。

在其他语言中,这是通过命名参数来完成的,例如在ruby中它将被定义为:

out_of: "Your score is %{correct} out of %{total}"

并调用如:

I18n('out_of', {total: total, correct: correct})

在iOS / Objective C上完成相同操作的推荐方法是什么?

1 个答案:

答案 0 :(得分:4)

根据文件

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW2

请注意,您还可以使用“n $”位置说明符,例如%1 $ @%2 $ s

所以你可以简单地创建你的字符串

" score_out_of" ="你的分数是%1 $ i i%2 $ i"

而在其他语言中,它可能是

" score_out_of" ="在%2 $ i中,您的分数为%1 $ i"