2

I am trying to pull a string from my language file and to include a variable during the execution. The issue is that depending on the language, the variable will be positioned differently. In the case of Hebrew, it seems impossible to put the variable at the end. I cannot use string interpolation due to the fact that I am pulling the string from the resource file. See video Any hints?

The code that I am working with is:

// the {0} should be positioned at the end of the Hebrew string
<data name="BeforeStartingYourMeeting" xml:space="preserve">
  <value>{0}פני התחלת הישיבה שלך</value>
</data>

I am trying to use the string later on in the code the following way:

string timeLeft = appointment.Start.Subtract(DateTime.Now).TotalMinutes + " min";
string timeBeforeMeetingLabel = string.Format(Properties.Resources.BeforeStartingYourMeeting, timeLeft);

I have read:

This case is exactly describing my issue:

The thought of the below workaround but I end up with the same issue:

<data name="BeforeStartingYourMeeting" xml:space="preserve">
  <value>פני התחלת הישיבה שלך {time}</value>
</data>

The code here shows clearly the issue. Different input strings but same output!!! It looks like it has to do with the display of the string which has to be in the specific RTL format. If I do a copy/past of the generated line into a textpad, the second line is different.

Salim
  • 495
  • 3
  • 20
  • " It seems impossible to put the variable at the end" why? – styx Nov 10 '19 at 07:59
  • Quite often the problem with Hebrew (and apparently all RTL languages) is that symbols like '{' are treated as LTR characters while Hebrew is RTL (right-to-left). So it moves symbols '{}' inside of a `string.Format` to some invalid positions (mostly at the end of the string) and later throws an exception. To understand your specific issue we need to know more about the issue, give a sample of the data that you're working with and code with string interpolation that doesn't work – Fabjan Nov 10 '19 at 08:05
  • @styx it move the variable elsewhere in the string. Most probably due to the reason indicated by Fabjan – Salim Nov 10 '19 at 08:35
  • @Fabjan thanks for your explanation which makes sense. I have updated the post with the code. – Salim Nov 10 '19 at 08:35
  • Your code should work, check it out for yourself: https://dotnetfiddle.net/8kPRvA Make sure you don't have hidden characters in your format string. Try copying your string to a tool that measures the string length and make sure you see the correct number – Fabjan Nov 10 '19 at 09:18
  • Thanks the issue is that the {0} has to be at the end of the Hebrew string and not at the beginning. As you can see in the video I simply can't put it at the end. You can reproduce the issue by trying to move the {0} at the end of the string of the code on your fiddle sample example. – Salim Nov 11 '19 at 05:30

1 Answers1

2

As stated by @Avi in his post:

"The unicode characters "RTL mark" (U+200F) and "LTR mark" (U+200E) were created precisely for this purpose."

Reference: - https://learn.microsoft.com/en-us/globalization/input/text-rendering#directionality-control-marks

The code can be updated as follow:

string heText1 = "פני התחלת הישיבה שלך time";
string timePattern1 = "time";
Console.WriteLine(heText1.Replace(timePattern1,"\u200e" +DateTime.Now.ToString("dd/MM/yyyy")));

Fiddle code.

N.B.: I am still unable to store a hebrew string with a {0} pattern in the resource file and had to accept using an hugly {time} pattern and to replace.

Salim
  • 495
  • 3
  • 20
  • Hey, I just tried this: string a = string.Format(" קמ\"ש \u200e{0}", 1); This provided the expected answer, with the number at the right. No need for the ugly hack – mike1952 Nov 23 '19 at 23:06
  • Although it seems like the resource file causes problems with reading the unicode. Will update if I find a solution to that. – mike1952 Nov 23 '19 at 23:14
  • So I'm using resharper's localisation, and langRes is my resource file reference. By using `"קמ\"ש \u200e{0}"` in the resource, I can get to something that works like this: `string.Format(langRes.Form1_Form1__0__Km_h.Replace("\\u200e", "\u200e"), speed);` The replace is a bit ugly, but still ok. – mike1952 Nov 23 '19 at 23:24