0
votes

I have a web site built with DevExpress controls including Reports. The main language used is Hebrew so the basic direction is RTL. However there is often a need to include English text, LTR, mixed in the Hebrew text. Their web controls support RTL and usually there isn't a problem with mixed texts.

The problem is with their reports that up until recently did not support RTL. Creating a report entirely in Hebrew was not to much of a problem. Trouble starts when we have Hebrew and English mixed, then the text becomes messed up.

I succeeded in fixing that with the following code:

private string FixBiDirectionalString(string textToFix)
    {
        try
        {
            char RLE = '\u202B';
            char PDF = '\u202C';
            char LRM = '\u200E';
            char RLM = '\u200F';

            StringBuilder sb = new StringBuilder(textToFix.Replace("\r", "").Replace("\n", string.Format("{0}", '\u000A')));

            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("[A-Za-z0-9-+ ]+");
            System.Text.RegularExpressions.MatchCollection mc = r.Matches(sb.ToString());
            foreach (System.Text.RegularExpressions.Match m in mc)
            {
                double tmp;
                if (m.Value == " ")
                    continue;
                if (double.TryParse(RemoveAcceptedChars(m.Value), out tmp))
                    continue;
                sb.Replace(m.Value, LRM + m.Value + RLM);
            }

            return RLE + sb.ToString() + PDF;
        }
        catch { return Text; }

    }

    private string RemoveAcceptedChars(string p)
    {
        return p.Replace("+", "").Replace("-", "").Replace("*", "").Replace("/", "");
    }

This code is based on code I found in this article XtraReports RTL: bidirectional text drawing in one of the comments.

However I still had a problem with spaces between Hebrew and English words disapearing or being misplaced.

How can that be fixed? (I'm still using an older version of reports that doesn't support RTL).

1

1 Answers

1
votes

I fixed it by first trimming the leading and trailing spaces in the string that matched the Regex of the English alphabet and then added spaces accordingly in relation to the unicode elements.

  string mTrim = m.Value.Trim();
  sb.Replace(m.Value, " " + LRM + mTrim + " " + RLM);

This problem is caused because the spaces are neutral or weakly directional, meaning their direction is dependent on the text they are in and here the text being mixed can cause the space to be misplaced. So this code forces one space to be part the general RTL direction and one to be part of the LTR segment. Then the words are displayed separated properly.