【问题标题】:Is it possible to customize certain words in localizable strings?是否可以在可本地化的字符串中自定义某些单词?
【发布时间】:2020-08-13 12:42:50
【问题描述】:

我有一个美国英语的localizable string,上面写着

"message" = "The distance is 10 feet";

是否可以自定义单词"10 feet" 让我们说英国英语,使其变为“x 米”,而不是在设置菜单中将语言设置为 GB 时?

或者我需要为此创建一个单独的 localizable.string 文件吗?已阅读添加区域语言支持可能存在一些问题。

【问题讨论】:

  • 如果消息是整个句子(这通常是我们本地化时最好的做法),是的,你可以做到:你本地化整个句子。如果你翻译部分字符串,(例如10 然后feet),不,但这也很糟糕。在某些语言上,复数可能还需要对句子的前一部分进行其他更改。所以:应该没问题,或者你有本地化问题
  • 您也许可以通过developer.apple.com/documentation/foundation/… 找到您想要解决的具体距离问题。它应该摆脱复数/单数(在不同的语言中可能不同)和/或该字符串在句子中的位置。我建议有:"message" = "The distance is %@";.

标签: ios swift string localization


【解决方案1】:

是的,你可以。这是一个例子:

"message" = "The distance is %d feet";
let originMessage = NSLocalizedString("message", comment: "")
let yourCustomMessage = String(format: originMessage, yourNumber)

【讨论】:

  • 一英尺,多英尺,不是吗?你不处理那个案子。用户问,如果用户更喜欢米而不是英尺怎么办?
【解决方案2】:

您可以在 Localizable.strings 文件中设置

"message" = "The distance is %d %@";

然后检查当前iPhone语言是Englis US还是English UK,并据此设置值和单位

var localizedText = NSLocalizedString("message", comment"")

localizedText = localizedText.replaceingOccurences(of: "%d", with : String(10/* set value */))

localizedText = localizedText.replaceingOccurences(of: "%@", with : "feet" /*set unit*/)

您也可以使用字符串格式:

let localizedText = String(format: NSLocalizedString("message", comment""),value, unit )

【讨论】:

  • 为什么使用replaceingOccurences而不使用string(format:),因为它们已经是有效的“占位符”。
猜你喜欢
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2016-01-26
  • 1970-01-01
  • 2017-04-19
  • 2015-02-09
相关资源
最近更新 更多