1


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

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
Jpicerno
  • 21
  • 3
  • Just as a point, you could always do something like `[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"The trick belongs to", nil), user]` - so long as the 'user' will always be at the end of the string. – Doc Jan 08 '14 at 21:04
  • @Doc - Doesn't work in Yoda. – Hot Licks Jan 08 '14 at 21:05
  • You need to be sure that the "%" and "@" characters occur in that order in the character order of the NSString (as fetched by `characterAtIndex`), not in the order that they appear in right-to-left text. And you need to be careful to assure that the values are not garbled in any character set conversion you might do. (I suspect that part of your problem may be that you didn't correctly rebuild the strings file after each of the above trial edits.) – Hot Licks Jan 08 '14 at 21:11
  • @HotLicks That's why I said it's fine so long as the user variable will always be in the same location. That said, I'm somewhat surprised that the last sample tried didn't work - unless of course OP didn't rebuild the localized strings file like you suggested. – Doc Jan 08 '14 at 21:13
  • Probably the 4th line above is correct. – Hot Licks Jan 08 '14 at 21:15
  • (It's worth noting that if the `%@` were not recognized at all then the string would have been printed with garbage characters there, with no substitution. This is why one suspects that the file was not rebuilt between tests.) – Hot Licks Jan 08 '14 at 21:17
  • Try adding RLE char (0x202B) at the beginning of the string. – VahidN Jan 08 '14 at 21:40
  • It didn't work for me, the work around was to put something before the %@ and after that, it works. I think, the problem is that xcode(or i don't know what!) when we write %@ at the start of the frase it undertand it is left to write, and try to write where it think to fit best. _بازیکن %@ برنده کارت ها شد_ It was the way i finally could do it. – Jpicerno Jan 09 '14 at 18:56

2 Answers2

5

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%@ برنده کارت ها شد";
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I used this and it shows "u200f FNF نمبر کا اضافہ کریں " correct formate is "نمبر کا اضافہ کریں FNF" – Abdul Rehman May 15 '17 at 11:43
  • any suggestion? complete localized string ("addFNFNumber_Str" = "\u200f FNF نمبر کا اضافہ کریں ";) – Abdul Rehman May 15 '17 at 11:44
  • \u200f shouldn't be necessary if `%@` isn't involved. But what is "FNF" here? (I'm sorry, I'm not familiar with this alphabet. It's not quite Arabic or Farsi. What is it?) How did you type this string? It looks like you typed "\u200f FNF" and then ن. If ن is the first letter of the string, it should be the first letter you type and FNF should be last, but I'm not clear what FNF is. – Rob Napier May 18 '17 at 22:05
  • 1
    I was able to get it to work in my Localizable.strings file by using "\U200F%@". Apparently the strings file uses a different unicode format than in code. – duncanc4 Nov 14 '19 at 10:49
0

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.

James Richard
  • 1,525
  • 12
  • 18
  • Thanks James,but what i try to say with this example that i use this 5 different formats (not all together) and i get the same result. – Jpicerno Jan 08 '14 at 20:53
  • That's where the limitations of the localization system come in. It can only know one value per key per localization. You'll probably have to make each their own key and in your application you'll have to decide which of those to use. If they are different dialiacs you could put each one in a different localized file (for example, english U.S. might be different than english UK) – James Richard Jan 08 '14 at 20:58
  • If there's only one replacement token in the string, using the above numbering scheme should be unnecessary. It is useful when there is more than one token, since it allows tokens to be matched up with their replacement values even if translation changes the order. – Hot Licks Jan 08 '14 at 21:13