如何处理包含多个变量和其中一些需要处理复数的字符串本地化。

huangapple go评论98阅读模式
英文:

How should I handle localization of string which contains multiple variables and some of them should take care of plural

问题

以下是要翻译的内容:

For example:

  1. let count: Int = ...
  2. let unit: Calendar.Component = ... //.year, .month, .day
  3. let str = "\(count) \(unit) later";

So str could be one of these:

  1. //In English
  2. 1 year later
  3. 3 months later
  4. //In Chinese(Note: The spaces between the words are being removed.)
  5. 1年后
  6. 3个月后

What's the best way to localize this string? If the unit part is removed, I know how to do that with stringsdict file, but now I am confused.

英文:

For example:

  1. let count: Int = ...
  2. let unit: Calendar.Component = ... //.year, .month, .day
  3. let str = "\(count) \(unit) later"

So str could be one of these:

  1. //In English
  2. 1 year later
  3. 3 months later
  4. //In Chinese(Note: The spaces between the words are being removed.)
  5. 1年后
  6. 3个月后

What's the best way to localize this string? If the unit part is removed, I know how to do that with stringsdict file, but now I am confused.

答案1

得分: 1

代码部分不提供翻译。

英文:

The use of stringsdict works just fine for this case. You just need an entry for each possible unit.

The English setup would include something like:

  1. <key>years later</key>
  2. <dict>
  3. <key>NSStringLocalizedFormatKey</key>
  4. <string>%#@years@</string>
  5. <key>years</key>
  6. <dict>
  7. <key>NSStringFormatSpecTypeKey</key>
  8. <string>NSStringPluralRuleType</string>
  9. <key>NSStringFormatValueTypeKey</key>
  10. <string>d</string>
  11. <key>one</key>
  12. <string>%d year later</string>
  13. <key>other</key>
  14. <string>%d years later</string>
  15. </dict>
  16. </dict>
  17. <key>months later</key>
  18. <dict>
  19. <key>NSStringLocalizedFormatKey</key>
  20. <string>%#@months@</string>
  21. <key>months</key>
  22. <dict>
  23. <key>NSStringFormatSpecTypeKey</key>
  24. <string>NSStringPluralRuleType</string>
  25. <key>NSStringFormatValueTypeKey</key>
  26. <string>d</string>
  27. <key>one</key>
  28. <string>%d month later</string>
  29. <key>other</key>
  30. <string>%d months later</string>
  31. </dict>
  32. </dict>
  33. <key>days later</key>
  34. <dict>
  35. <key>NSStringLocalizedFormatKey</key>
  36. <string>%#@days@</string>
  37. <key>days</key>
  38. <dict>
  39. <key>NSStringFormatSpecTypeKey</key>
  40. <string>NSStringPluralRuleType</string>
  41. <key>NSStringFormatValueTypeKey</key>
  42. <string>d</string>
  43. <key>one</key>
  44. <string>%d day later</string>
  45. <key>other</key>
  46. <string>%d days later</string>
  47. </dict>
  48. </dict>

Then your code becomes something like:

  1. let count: Int = ...
  2. let unit: Calendar.Component = ... //.year, .month, .day
  3. var key: String?
  4. switch unit {
  5. case .year: key = "years later"
  6. case .month: key = "months later"
  7. case .day: key = "days later"
  8. default: break
  9. }
  10. if let key {
  11. let fmt = NSLocalizedString(key)
  12. let str = String(format: fmt, count)
  13. }

If you might have a bunch of these, it might help to add an extension to Calendar.Component to create a string from a unit so you don't need to scatter the clumsy switch statements everywhere.

huangapple
  • 本文由 发表于 2023年4月13日 23:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定