NSLocalizedString with parameters in right to left languages (like farsi)

1.7k views Asked by At


I write this post because this problem is driving me crazy.
I had same problems try to print same text(right to left like Persian) in UILabel using NSLocalizedString with parameters. My code looks like this:

label.text = [NSString stringWithFormat:NSLocalizedString(@"The trick belongs to %@",nil),user];

In my string file in farsi I try to use this

"The trick belongs to %@" = " %@ میز را ترک کرد";
"The trick belongs to %@" = "%@ برنده کارت ها شد";
"The trick belongs to %@" = "@% برنده کارت ها شد";
"The trick belongs to %@" = "برنده کارت ها شد @%";
"The trick belongs to %@" = "برنده کارت ها شد %@";

And all this type of form always print:

"Maria برنده کارت ها شد " but has to be shown like "برنده کارت ها شد Maria"

Many thanks in advance

2

There are 2 answers

3
James Richard On

Your variable in the right hand side should be %1$@, and make sure it's the only entry for that key. In your example you have 5 strings, so it appears to be ignoring the other ones.

The reason we use %1$@ is because if we want a different order for the variables we can use 1,2,3, etc.

More on it here:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html#//apple_ref/doc/uid/10000051i-CH6

Scroll down to the Formatting Strings Resources section.

4
Rob Napier On

You need to let iOS know that this is a RTL string, even though it encounters an M at the front of it (which indicates it's a LTR string). You do this with the Unicode Implicit Direction Mark RIGHT-TO-LEFT MARK U+200F.

For example:

self.label.text = [NSString stringWithFormat:@"\u200f%@ برنده کارت ها شد", @"Maria"];

You can put \u200f in your Localized.strings file; you don't need any special code to handle this.

"The trick belongs to %@" = @"\u200f%@ برنده کارت ها شد";