LocalizationManager_RTL.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. namespace I2.Loc
  4. {
  5. public static partial class LocalizationManager
  6. {
  7. static string[] LanguagesRTL = {"ar-DZ", "ar","ar-BH","ar-EG","ar-IQ","ar-JO","ar-KW","ar-LB","ar-LY","ar-MA","ar-OM","ar-QA","ar-SA","ar-SY","ar-TN","ar-AE","ar-YE",
  8. "fa", "he","ur","ji"};
  9. public static string ApplyRTLfix(string line) { return ApplyRTLfix(line, 0, true); }
  10. public static string ApplyRTLfix(string line, int maxCharacters, bool ignoreNumbers)
  11. {
  12. if (string.IsNullOrEmpty(line))
  13. return line;
  14. // Fix !, ? and . signs not set correctly
  15. char firstC = line[0];
  16. if (firstC == '!' || firstC == '.' || firstC == '?')
  17. line = line.Substring(1) + firstC;
  18. int tagStart = -1, tagEnd = 0;
  19. // Find all Tags (and Numbers if ignoreNumbers is true)
  20. int tagBase = 40000;
  21. tagEnd = 0;
  22. var tags = new List<string>();
  23. while (I2Utils.FindNextTag(line, tagEnd, out tagStart, out tagEnd))
  24. {
  25. string tag = "@@" + (char)(tagBase + tags.Count) + "@@";
  26. tags.Add(line.Substring(tagStart, tagEnd - tagStart + 1));
  27. line = line.Substring(0, tagStart) + tag + line.Substring(tagEnd + 1);
  28. tagEnd = tagStart + 5;
  29. }
  30. // Split into lines and fix each line
  31. line = line.Replace("\r\n", "\n");
  32. line = I2Utils.SplitLine(line, maxCharacters);
  33. line = RTLFixer.Fix(line, true, !ignoreNumbers);
  34. // Restore all tags
  35. for (int i = 0; i < tags.Count; i++)
  36. {
  37. var len = line.Length;
  38. for (int j = 0; j < len-4; ++j)
  39. {
  40. if (line[j] == '@' && line[j + 1] == '@' && line[j + 2] >= tagBase && line[j + 3] == '@' && line[j + 4] == '@')
  41. {
  42. int idx = line[j + 2] - tagBase;
  43. if (idx % 2 == 0) idx++;
  44. else idx--;
  45. if (idx >= tags.Count) idx = tags.Count - 1;
  46. line = line.Substring(0, j) + tags[idx] + line.Substring(j + 5);
  47. break;
  48. }
  49. }
  50. }
  51. return line;
  52. }
  53. public static string FixRTL_IfNeeded(string text, int maxCharacters = 0, bool ignoreNumber=false)
  54. {
  55. if (IsRight2Left)
  56. return ApplyRTLfix(text, maxCharacters, ignoreNumber);
  57. return text;
  58. }
  59. public static bool IsRTL(string Code)
  60. {
  61. return Array.IndexOf(LanguagesRTL, Code)>=0;
  62. }
  63. }
  64. }