LocalizationManager_RTL.cs 2.8 KB

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