-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_translations.py
More file actions
102 lines (81 loc) · 3.26 KB
/
manage_translations.py
File metadata and controls
102 lines (81 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
manage_translations.py - Auto-Scanner fuer deutsche GUI-Strings
================================================================
Findet deutsche Strings in .py-Dateien und pflegt locales/translations.json.
Verwendung:
python manage_translations.py [--dir PROJEKTVERZEICHNIS]
"""
import json
import re
import os
import sys
TRANSLATION_FILE = "locales/translations.json"
STRING_PATTERNS = [
re.compile(r'text\s*=\s*"([^"]+)"'),
re.compile(r'setText\s*\(\s*["\']([^"\']+)["\']\s*\)'),
re.compile(r'setWindowTitle\s*\(\s*["\']([^"\']+)["\']\s*\)'),
re.compile(r'QLabel\s*\(\s*["\']([^"\']+)["\']\s*\)'),
re.compile(r'QPushButton\s*\(\s*["\']([^"\']+)["\']\s*\)'),
]
GERMAN_HINTS = [
"datei", "filter", "fehler", "laden", "speichern",
"ansicht", "optionen", "zurueck", "anzeigen", "export",
"import", "einstellungen", "abbrechen", "hilfe", "bearbeiten",
"oeffnen", "schliessen", "start", "aktualisieren",
]
def is_german(text):
if any(ch in text for ch in "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df"):
return True
text_lower = text.lower()
return any(w in text_lower for w in GERMAN_HINTS)
def find_german_strings(source_dir):
german_strings = set()
skip_dirs = {'build', 'dist', 'venv', '.venv', '__pycache__', 'releases'}
for root, dirs, files in os.walk(source_dir):
dirs[:] = [d for d in dirs if d not in skip_dirs]
for file in files:
if file.endswith(".py"):
path = os.path.join(root, file)
try:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
except Exception:
continue
for pattern in STRING_PATTERNS:
for match in pattern.findall(content):
if is_german(match):
german_strings.add(match.strip())
return german_strings
def manage_translations(source_dir="."):
trans_file = os.path.join(source_dir, TRANSLATION_FILE)
if os.path.exists(trans_file):
with open(trans_file, "r", encoding="utf-8") as f:
translations = json.load(f)
else:
translations = {}
found = find_german_strings(source_dir)
added = []
for s in sorted(found):
if s not in translations:
translations[s] = {"de": s, "en": ""}
added.append(s)
os.makedirs(os.path.dirname(trans_file), exist_ok=True)
with open(trans_file, "w", encoding="utf-8") as f:
json.dump(translations, f, indent=2, ensure_ascii=False)
if added:
print(f"[+] {len(added)} neue Eintraege hinzugefuegt:")
for s in added[:20]:
print(f" - {s}")
if len(added) > 20:
print(f" ... und {len(added) - 20} weitere")
else:
print("[i] Keine neuen deutschen Strings gefunden.")
missing = [k for k, v in translations.items() if not v.get("en")]
if missing:
print(f"\n[!] {len(missing)} fehlende englische Uebersetzungen")
else:
print("\n[ok] Alle Strings haben englische Uebersetzungen.")
print(f"\n[i] Gesamt: {len(translations)} Strings in {trans_file}")
if __name__ == "__main__":
target = sys.argv[1] if len(sys.argv) > 1 else "."
manage_translations(target)