| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- # -*- coding: utf-8 -*-
- # 把 .cs 中 MessageBox.Show / BaseUtil.ShowXxx 调用里的中文字符串字面量包上
- # UAS_MES_NEW.I18n.LanguageManager.T(...),使其走多语言字典(缺词回退原文)。
- import os, re, io
- ROOT = r"C:\Users\HUAWE1\source\repos\mes-client\UAS_MES_JWS"
- EXCLUDE_DIRS = {"obj", "bin", ".workbuddy", ".git", "I18n"}
- TQ = "UAS_MES_NEW.I18n.LanguageManager.T"
- han = re.compile(r'[\u4e00-\u9fff]')
- changed_files = 0
- changed_calls = 0
- def wrap_call(src, start_idx):
- """start_idx 指向调用名前位置;返回 (new_text, 是否修改, 下一次扫描起点)"""
- # 找到左括号
- lp = src.find("(", start_idx)
- if lp == -1:
- return src, False, lp if lp != -1 else len(src)
- # 平衡括号扫描(跳过字符串字面量与注释)
- i = lp
- depth = 0
- n = len(src)
- while i < n:
- c = src[i]
- if c == '"':
- i += 1
- while i < n and src[i] != '"':
- if src[i] == '\\':
- i += 1
- i += 1
- i += 1
- continue
- if c == "'":
- i += 1
- while i < n and src[i] != "'":
- if src[i] == '\\':
- i += 1
- i += 1
- i += 1
- continue
- if c == '/' and i + 1 < n and src[i+1] == '/':
- j = src.find('\n', i)
- i = n if j == -1 else j
- continue
- if c == '(':
- depth += 1
- elif c == ')':
- depth -= 1
- if depth == 0:
- break
- i += 1
- if depth != 0:
- return src, False, n
- call_end = i # 指向 ')'
- body = src[lp+1:call_end]
- if "LanguageManager.T(" in body or "LanguageManager.Show" in body:
- return src, False, call_end + 1
- if not han.search(body):
- return src, False, call_end + 1
- # 逐个字面量包裹(跳过无中文的)
- out = []
- k = 0
- m = 0
- while k < len(body):
- c = body[k]
- if c == '"':
- j = k + 1
- while j < len(body) and body[j] != '"':
- if body[j] == '\\':
- j += 1
- j += 1
- lit = body[k:j+1]
- inner = lit[1:-1] if len(lit) >= 2 else ""
- if han.search(inner):
- out.append(body[m:k])
- out.append(TQ + "(" + lit + ")")
- m = j + 1
- k = j + 1
- else:
- k += 1
- out.append(body[m:])
- new_body = "".join(out)
- if new_body == body:
- return src, False, call_end + 1
- return src[:lp+1] + new_body + src[call_end:], True, lp + 1 + len(new_body) + 1
- call_re = re.compile(r'\b(?:MessageBox\.Show|BaseUtil\.Show(?:Error|Warning|Info|Question))\s*\(')
- for dp, dn, fn in os.walk(ROOT):
- dn[:] = [d for d in dn if d not in EXCLUDE_DIRS]
- for f in fn:
- if not f.endswith(".cs") or f.endswith(".Designer.cs"):
- continue
- p = os.path.join(dp, f)
- try:
- src = io.open(p, encoding="utf-8-sig", errors="ignore").read()
- except Exception:
- continue
- pos = 0
- modified = False
- while True:
- mm = call_re.search(src, pos)
- if not mm:
- break
- src2, did, npos = wrap_call(src, mm.start())
- if did:
- src = src2
- modified = True
- changed_calls += 1
- pos = npos
- else:
- pos = max(npos, mm.end())
- if modified:
- # 保留 BOM 与否:统一无 BOM UTF-8(VS 兼容)
- io.open(p, "w", encoding="utf-8").write(src)
- changed_files += 1
- print("files changed:", changed_files)
- print("calls wrapped:", changed_calls)
|