gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:58 25 4
gpt4 key购买 nike

我如何使用 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 上完成同样的事情的推荐方法是什么?

最佳答案

根据文档

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

Note that you can also use the “n$” positional specifiers such as %1$@ %2$s

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

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

换句话说,它可能是

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

关于iOS:如何使用多个内插参数本地化字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24160675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com