Localization in Swift with string like \(foo) doesn't work

For example, I have code


let count = 10
let says = NSLocalizedString("It runs \(count) times", comment: "run times")


Then I got the translation file.


<trans-unit id="It runs (count) times">
<source>It runs (count) times</source>
<note>run times</note>


However, when I translated it, it didn't work.


<trans-unit id="It runs (count) times">
<source>It runs (count) times</source>
<target>aaa(count)aa</target>
<note>run times</note>
</trans-unit>


All other translations without \(foo) did work. Is this a known bug or I shouldn't use it this way?

Accepted Reply

I managed to solve this problem by using


let newSays = String.localizedStringWithFormat(NSLocalizedString("It runs %d times", comment: "new run times"), count)


I think this is more like C instead of Swift. Nevertheless, it works.

Replies

I managed to solve this problem by using


let newSays = String.localizedStringWithFormat(NSLocalizedString("It runs %d times", comment: "new run times"), count)


I think this is more like C instead of Swift. Nevertheless, it works.

This is the correct way of doing it. You have to use variable substitution (localizedStringWithFormat) for localization. The \(count) variables will never work.