#!/usr/bin/env python3 # -*- coding: utf-8 -*- """check-i18n.py — ตรวจความครบถ้วนของคำแปล (ui/locales/.json) ตรวจ 5 เรื่อง: 1. คีย์ที่ขาดในภาษาใดภาษาหนึ่ง → ERROR (โหมดนั้นจะถอยไปใช้ภาษาตั้งต้น = คำไทยโผล่ในหน้า EN) 2. คีย์ซ้ำภายในไฟล์ภาษาเดียวกัน → ERROR (JSON เก็บตัวหลังเงียบ ๆ — เคยทำให้ค่าอังกฤษทับค่าไทย) 3. ไฟล์ภาษาไม่ตรงกับ SUPPORTED ใน i18n.js → ERROR 4. ค่าที่ว่างเปล่า → WARN (อาจตั้งใจ เช่น home.provincePrefix ของ EN) 5. คีย์ที่ไม่พบที่ใช้ → WARN (อาจใช้แบบไดนามิกก็ได้) ใช้: python resource/tools/check-i18n.py python resource/tools/check-i18n.py --quiet (แสดงเฉพาะที่มีปัญหา) """ import json import re import sys from pathlib import Path R = Path(__file__).resolve().parents[2] LOCALES = R / "ui/locales" BASE = "th" # ภาษาตั้งต้น — ใช้เทียบว่าคีย์ครบไหม quiet = "--quiet" in sys.argv errors, warns = [], [] # ── 1) อ่านไฟล์ภาษาทั้งหมด (จับคีย์ซ้ำด้วย object_pairs_hook) ── files = sorted(LOCALES.glob("*.json")) if not files: print(f"!! ไม่พบไฟล์ภาษาใน {LOCALES.relative_to(R)}") sys.exit(1) dicts, dups = {}, {} for p in files: lg = p.stem found = [] def hook(pairs, _found=found): seen = {} for k, v in pairs: if k in seen: _found.append(k) seen[k] = v return seen try: dicts[lg] = json.loads(p.read_text(encoding="utf-8"), object_pairs_hook=hook) except Exception as e: errors.append((f"อ่านไฟล์ {p.name} ไม่ได้", [str(e)[:70]])) continue if found: dups[lg] = found langs = sorted(dicts) if BASE not in dicts: errors.append((f"ไม่พบภาษาตั้งต้น '{BASE}'", [str(l) for l in langs])) # ── 2) ไฟล์ภาษาต้องตรงกับ SUPPORTED ใน i18n.js ── src = (R / "ui/i18n.js").read_text(encoding="utf-8") m = re.search(r"var SUPPORTED\s*=\s*\[([^\]]*)\]", src) supported = re.findall(r"'([\w-]+)'", m.group(1)) if m else [] if supported and sorted(supported) != langs: errors.append(("SUPPORTED ใน i18n.js ไม่ตรงกับไฟล์ภาษา", [f"js={supported}", f"files={langs}"])) # ── 3) คีย์ขาด / คีย์ซ้ำ ── base_keys = list(dicts.get(BASE, {})) for lg in langs: if lg == BASE: continue missing = [k for k in base_keys if k not in dicts[lg]] if missing: errors.append((f"คีย์ขาดใน '{lg}' ({len(missing)})", missing)) extra = [k for k in dicts[lg] if k not in dicts[BASE]] if extra: warns.append((f"คีย์ที่มีใน '{lg}' แต่ไม่มีใน '{BASE}' ({len(extra)})", extra)) for lg in sorted(dups): errors.append((f"คีย์ซ้ำในไฟล์ '{lg}.json' ({len(set(dups[lg]))})", sorted(set(dups[lg])))) # ── 4) ค่าที่ว่างเปล่า ── empty = [f"{lg}:{k}" for lg in langs for k, v in dicts[lg].items() if not str(v).strip()] if empty: warns.append((f"ค่าที่ว่างเปล่า ({len(empty)}) — อาจตั้งใจ", empty)) # ── 5) คีย์ที่ไม่พบที่ใช้ ── # ตรงตัว: t('key') · data-i18n="key" · data-i18n-attr="attr:key" # ไดนามิก: literal ที่ลงท้ายด้วย '.' เป็นคำนำหน้าคีย์ (เช่น 'check.day' + type) used, all_lits = set(), set() for f in list((R / "ui").rglob("*.js")) + list((R / "ui").rglob("*.html")): if "vendor" in str(f) or f.name == "i18n.js": continue txt = f.read_text(encoding="utf-8", errors="ignore") used |= set(re.findall(r"""tr?\('([\w.]+)'""", txt)) used |= set(re.findall(r'data-i18n(?:-n)?="([\w.]+)"', txt)) used |= set(re.findall(r'data-i18n-attr="[^":]+:([\w.]+)"', txt)) all_lits |= set(re.findall(r"'([\w.]{2,80})'", txt)) prefixes = {x for x in all_lits if x.endswith(".")} unused = sorted(k for k in base_keys if k not in used and k not in all_lits and not any(k.startswith(p) for p in prefixes)) if unused: warns.append((f"ไม่พบที่ใช้ ({len(unused)}) — อาจใช้แบบไดนามิกก็ได้", unused)) # ── รายงาน ── if not quiet: print("i18n: ui/locales/ · " + " · ".join(f"{lg}={len(dicts[lg])}" for lg in langs) + f" · ภาษาตั้งต้น={BASE} · ไฟล์ที่ใช้จริง {LOCALES.relative_to(R)}") for title, items in errors: print(f"[ERROR] {title}: {', '.join(str(i) for i in items[:12])}" + (" …" if len(items) > 12 else "")) for title, items in warns: print(f"[WARN ] {title}: {', '.join(str(i) for i in items[:12])}" + (" …" if len(items) > 12 else "")) if errors: print(f"\nผลลัพธ์: ไม่ผ่าน — {len(errors)} เรื่องต้องแก้") sys.exit(1) print(f"\nผลลัพธ์: ผ่าน — ทุกคีย์มีครบทั้ง {len(langs)} ภาษา ไม่มีคีย์ซ้ำ" + (f" (มี {len(warns)} คำเตือน)" if warns else "")) sys.exit(0)