Persian-ASR-Curation / patch_argilla_ui_labels.py
Reza Sayar
Localize Argilla action buttons narrowly
c525984
Raw
History Blame Contribute Delete
2.5 kB
#!/usr/bin/env python3
from __future__ import annotations
import os
import re
from pathlib import Path
QUESTIONS_FORM_PATCHES = {
'discard:"Discard"': 'discard:"بی‌خیال"',
'submit:"Submit"': 'submit:"ثبت"',
'draft:"Save as draft"': 'draft:"ذخیره پیش‌نویس"',
}
EXTENSIONS = {".js", ".mjs", ".cjs"}
ROOTS = [
"/opt",
"/usr/local/lib",
"/usr/local/share",
"/app",
"/home/argilla",
]
def should_scan(path: Path) -> bool:
if path.suffix not in EXTENSIONS:
return False
return "_nuxt" in str(path) or "argilla" in str(path).lower()
def patch_file(path: Path) -> int:
try:
data = path.read_text(encoding="utf-8")
except UnicodeDecodeError:
return 0
except OSError:
return 0
if "questions_form:" not in data:
return 0
patched = data
hits = 0
# Argilla v2.8 ships the review toolbar labels in the compiled locale
# object. Keep this intentionally narrow: only patch that object, avoiding
# broad bundle rewrites that can alter unrelated code/highlighting strings.
pattern = re.compile(r"questions_form:\{[^{}]*\}")
def repl(match: re.Match[str]) -> str:
nonlocal hits
chunk = match.group(0)
new_chunk = chunk
for src, dst in QUESTIONS_FORM_PATCHES.items():
count = new_chunk.count(src)
if count:
hits += count
new_chunk = new_chunk.replace(src, dst)
return new_chunk
patched = pattern.sub(repl, patched)
if patched != data:
path.write_text(patched, encoding="utf-8")
return hits
def main() -> None:
total_hits = 0
touched: list[str] = []
for root in ROOTS:
base = Path(root)
if not base.exists():
continue
for dirpath, dirnames, filenames in os.walk(base):
dirnames[:] = [
d
for d in dirnames
if d not in {".git", "__pycache__", "node_modules", ".cache"}
]
for filename in filenames:
path = Path(dirpath) / filename
if not should_scan(path):
continue
hits = patch_file(path)
if hits:
total_hits += hits
touched.append(str(path))
print({"argilla_questions_form_label_patch_hits": total_hits, "files": touched[:20]}, flush=True)
if __name__ == "__main__":
main()