i18n_wrap_msg.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. # 把 .cs 中 MessageBox.Show / BaseUtil.ShowXxx 调用里的中文字符串字面量包上
  3. # UAS_MES_NEW.I18n.LanguageManager.T(...),使其走多语言字典(缺词回退原文)。
  4. import os, re, io
  5. ROOT = r"C:\Users\HUAWE1\source\repos\mes-client\UAS_MES_JWS"
  6. EXCLUDE_DIRS = {"obj", "bin", ".workbuddy", ".git", "I18n"}
  7. TQ = "UAS_MES_NEW.I18n.LanguageManager.T"
  8. han = re.compile(r'[\u4e00-\u9fff]')
  9. changed_files = 0
  10. changed_calls = 0
  11. def wrap_call(src, start_idx):
  12. """start_idx 指向调用名前位置;返回 (new_text, 是否修改, 下一次扫描起点)"""
  13. # 找到左括号
  14. lp = src.find("(", start_idx)
  15. if lp == -1:
  16. return src, False, lp if lp != -1 else len(src)
  17. # 平衡括号扫描(跳过字符串字面量与注释)
  18. i = lp
  19. depth = 0
  20. n = len(src)
  21. while i < n:
  22. c = src[i]
  23. if c == '"':
  24. i += 1
  25. while i < n and src[i] != '"':
  26. if src[i] == '\\':
  27. i += 1
  28. i += 1
  29. i += 1
  30. continue
  31. if c == "'":
  32. i += 1
  33. while i < n and src[i] != "'":
  34. if src[i] == '\\':
  35. i += 1
  36. i += 1
  37. i += 1
  38. continue
  39. if c == '/' and i + 1 < n and src[i+1] == '/':
  40. j = src.find('\n', i)
  41. i = n if j == -1 else j
  42. continue
  43. if c == '(':
  44. depth += 1
  45. elif c == ')':
  46. depth -= 1
  47. if depth == 0:
  48. break
  49. i += 1
  50. if depth != 0:
  51. return src, False, n
  52. call_end = i # 指向 ')'
  53. body = src[lp+1:call_end]
  54. if "LanguageManager.T(" in body or "LanguageManager.Show" in body:
  55. return src, False, call_end + 1
  56. if not han.search(body):
  57. return src, False, call_end + 1
  58. # 逐个字面量包裹(跳过无中文的)
  59. out = []
  60. k = 0
  61. m = 0
  62. while k < len(body):
  63. c = body[k]
  64. if c == '"':
  65. j = k + 1
  66. while j < len(body) and body[j] != '"':
  67. if body[j] == '\\':
  68. j += 1
  69. j += 1
  70. lit = body[k:j+1]
  71. inner = lit[1:-1] if len(lit) >= 2 else ""
  72. if han.search(inner):
  73. out.append(body[m:k])
  74. out.append(TQ + "(" + lit + ")")
  75. m = j + 1
  76. k = j + 1
  77. else:
  78. k += 1
  79. out.append(body[m:])
  80. new_body = "".join(out)
  81. if new_body == body:
  82. return src, False, call_end + 1
  83. return src[:lp+1] + new_body + src[call_end:], True, lp + 1 + len(new_body) + 1
  84. call_re = re.compile(r'\b(?:MessageBox\.Show|BaseUtil\.Show(?:Error|Warning|Info|Question))\s*\(')
  85. for dp, dn, fn in os.walk(ROOT):
  86. dn[:] = [d for d in dn if d not in EXCLUDE_DIRS]
  87. for f in fn:
  88. if not f.endswith(".cs") or f.endswith(".Designer.cs"):
  89. continue
  90. p = os.path.join(dp, f)
  91. try:
  92. src = io.open(p, encoding="utf-8-sig", errors="ignore").read()
  93. except Exception:
  94. continue
  95. pos = 0
  96. modified = False
  97. while True:
  98. mm = call_re.search(src, pos)
  99. if not mm:
  100. break
  101. src2, did, npos = wrap_call(src, mm.start())
  102. if did:
  103. src = src2
  104. modified = True
  105. changed_calls += 1
  106. pos = npos
  107. else:
  108. pos = max(npos, mm.end())
  109. if modified:
  110. # 保留 BOM 与否:统一无 BOM UTF-8(VS 兼容)
  111. io.open(p, "w", encoding="utf-8").write(src)
  112. changed_files += 1
  113. print("files changed:", changed_files)
  114. print("calls wrapped:", changed_calls)