Home > front end >  Localization stings when differ order of numbers
Localization stings when differ order of numbers

Time:03-28

I'm doing language localization of a Swift iOS app to Chinese. How can I translate a "%d of %d last weeks" which for English is "1 out of the 2 last weeks" but becomes "最近2周有1周" in Chinese.

How can I do a localization where "..A..B.." should become "..B..A.." in another language?

CodePudding user response:

You should use positional index of parameters, so instead of using %@ as the placeholder, use %1$@ instead. Positional placeholders allow translators to change the order of placeholders or repeat them if necessary.

For example use "%1$d of %2$d last weeks" as a key so in Chinese you can have in the translation: "%1$d of %2$d last weeks" = "最近%2$d周有%1$d周". And in code:

let template = NSLocalizedString("%1$d of %2$d last weeks", comment: "")
let str = String.localizeStringWithFormat(template, 1, 2)
  • Related