How do I use NSLocalizedString to build a string with multiple parameters while giving the translator control to change the order if they wish?
An example in my Localizable.string is:
"score_out_of"="Your score is %i out of %i";
And would be invoked like
[NSString stringWithFormat:NSLocalizedString(@"score_out_of", nil), correct, total];
But on some locales the grammar rules might dictate that total goes before correct. In Objective C it seems the interpolation order is hard coded.
In other languages this is accomplished by naming the parameters, for example in ruby it would be defined like:
out_of: "Your score is %{correct} out of %{total}"
And invoked like:
I18n('out_of', {total: total, correct: correct})
What is the recommended way to accomplish the same thing on iOS / Objective C?
According to the documentation
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
So you can simply create your string as
"score_out_of"="Your score is %1$i out of %2$i"
And in other language, it could be
"score_out_of"="Out of %2$i, your score is %1$i"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With