-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathcheck-translations.py
More file actions
executable file
·286 lines (230 loc) · 9.59 KB
/
Copy pathcheck-translations.py
File metadata and controls
executable file
·286 lines (230 loc) · 9.59 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
#
##############################################################################
# Copyright (c) 2026
#
# Author(s):
# ChatGPT
# ann0see
# JaminShanti
# Gemini
# The Jamulus Development Team
#
##############################################################################
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
##############################################################################
"""
Qt TS translation checker.
This tool validates Qt `.ts` translation files according to Qt Linguist
semantics.
Warnings are reported with best-effort line numbers. In strict mode, the
presence of any warning results in a non-zero exit code to allow CI failure.
"""
import argparse
import re
import sys
import xml.etree.ElementTree as ET
from collections import defaultdict, Counter
from dataclasses import dataclass
from enum import IntEnum
from pathlib import Path
# Regex helpers
PLACEHOLDER_RE = re.compile(r"%\d+")
HTML_TAG_RE = re.compile(r"<[^>]+>")
# ANSI escape codes
BOLD = "\033[1m"
CYAN = "\033[36m"
YELLOW = "\033[33m"
RED = "\033[31m"
RESET = "\033[0m"
# Severity Enum
class Severity(IntEnum):
WARNING = 1
SEVERE = 2
# Data structures
@dataclass(frozen=True)
class MessageContext:
ts_file: Path
line: int
lang: str
source: str
translation: str
tr_type: str
excerpt: str
@dataclass(frozen=True)
class WarningItem:
ts_file: Path
line: int
lang: str
message: str
severity: Severity
# Helpers
def approximate_message_lines(text: str):
"""Yield approximate line numbers for <message> elements."""
lines = text.splitlines()
cursor = 0
for _ in range(text.count("<message")):
for i in range(cursor, len(lines)):
if "<message" in lines[i]:
cursor = i + 1
yield i + 1
break
else:
yield 0
# Checks
def check_language_header(ts_file: Path, root, file_lang: str):
header_lang = root.attrib.get("language", "")
if header_lang != file_lang:
return [WarningItem(ts_file, 0, file_lang,
f"Language header mismatch '{header_lang}' != '{file_lang}'",
Severity.WARNING)]
return []
def check_empty_translation(ctx: MessageContext):
if not ctx.translation.strip() and ctx.tr_type != "unfinished":
return [WarningItem(ctx.ts_file, ctx.line, ctx.lang,
f"Empty translation for '{ctx.excerpt}'",
Severity.SEVERE)]
return []
def check_placeholders(ctx: MessageContext):
if ctx.tr_type != "unfinished" and Counter(PLACEHOLDER_RE.findall(ctx.source)) != Counter(
PLACEHOLDER_RE.findall(ctx.translation)):
msg = (f"Placeholder mismatch for '{ctx.excerpt}'\n"
f"Source: {ctx.source}\n"
f"Trans: {ctx.translation}")
return [WarningItem(ctx.ts_file, ctx.line, ctx.lang, msg, Severity.WARNING)]
return []
def check_html(ctx: MessageContext):
if HTML_TAG_RE.search(ctx.source) and not HTML_TAG_RE.search(
ctx.translation) and ctx.tr_type != "unfinished":
msg = (f"HTML missing for '{ctx.excerpt}'\n"
f"Source: {ctx.source}\n"
f"Trans: {ctx.translation}")
return [WarningItem(ctx.ts_file, ctx.line, ctx.lang, msg, Severity.WARNING)]
return []
def check_whitespace(ctx: MessageContext):
if not ctx.translation or ctx.tr_type == "unfinished":
return []
# Check if leading/trailing whitespace presence matches between source and translation
src_lead = ctx.source != ctx.source.lstrip()
src_trail = ctx.source != ctx.source.rstrip()
tr_lead = ctx.translation != ctx.translation.lstrip()
tr_trail = ctx.translation != ctx.translation.rstrip()
if src_lead != tr_lead or src_trail != tr_trail:
return [WarningItem(ctx.ts_file, ctx.line, ctx.lang,
f"Leading/trailing whitespace mismatch for '{ctx.excerpt}'",
Severity.WARNING)]
return []
def check_newline_consistency(ctx: MessageContext):
if ctx.source.endswith("\n") != ctx.translation.endswith("\n"):
return [WarningItem(ctx.ts_file, ctx.line, ctx.lang,
f"Newline mismatch for '{ctx.excerpt}'",
Severity.WARNING)]
return []
# Detect warnings
def detect_warnings(ts_file: Path, file_lang: str):
try:
text = ts_file.read_text(encoding="utf-8")
root = ET.fromstring(text)
except (OSError, ET.ParseError) as exc:
return [WarningItem(ts_file, 0, file_lang,
f"Error reading or parsing XML: {exc}",
Severity.SEVERE)]
warnings = []
warnings.extend(check_language_header(ts_file, root, file_lang))
message_lines = approximate_message_lines(text)
for context in root.findall("context"):
for message, line in zip(context.findall("message"), message_lines):
# Safely extract source text
source_elem = message.find("source")
source = "".join(source_elem.itertext()) if source_elem is not None else ""
# Safely extract translation text (handling Qt plural <numerusform> elements)
tr_elem = message.find("translation")
translation = ""
tr_type = ""
if tr_elem is not None:
tr_type = tr_elem.attrib.get("type", "")
numerus_forms = tr_elem.findall("numerusform")
if numerus_forms:
translation = " ".join("".join(n.itertext()) for n in numerus_forms)
else:
translation = "".join(tr_elem.itertext())
# Format a clean excerpt without blindly adding '...' to tiny strings
source_clean = source.strip().replace("\n", " ")
excerpt = source_clean[:30] + ("..." if len(source_clean) > 30 else "")
ctx = MessageContext(ts_file, line, file_lang, source, translation, tr_type, excerpt)
# All checks
warnings.extend(check_empty_translation(ctx))
warnings.extend(check_placeholders(ctx))
warnings.extend(check_html(ctx))
warnings.extend(check_whitespace(ctx))
warnings.extend(check_newline_consistency(ctx))
return warnings
# CLI
def main():
parser = argparse.ArgumentParser(description="Qt TS translation checker with extended rules")
parser.add_argument("--ts-dir", type=Path, default=Path("../src/translation"),
help="Directory containing translation_*.ts files")
parser.add_argument("--strict", action="store_true",
help="Exit non-zero if any warning is found")
args = parser.parse_args()
if not args.ts_dir.exists():
print(f"Directory not found: {args.ts_dir}", file=sys.stderr)
return 2
ts_files = sorted(args.ts_dir.glob("translation_*.ts"))
if not ts_files:
print(f"No TS files found in {args.ts_dir}", file=sys.stderr)
return 2
all_warnings = []
failures_by_language = defaultdict(lambda: {"severe": 0, "warning": 0})
for ts_file in ts_files:
lang = ts_file.stem.replace("translation_", "")
failures_by_language[lang] # Initializes default counters
all_warnings.extend(detect_warnings(ts_file, lang))
# Group output by file
grouped = defaultdict(list)
for w in all_warnings:
grouped[w.ts_file].append(w)
if w.severity == Severity.SEVERE:
failures_by_language[w.lang]["severe"] += 1
else:
failures_by_language[w.lang]["warning"] += 1
# Detailed clean column output
for file in sorted(grouped.keys()):
messages = grouped[file]
print(f"\n{BOLD}File: {file.name}{RESET}")
for w in sorted(messages, key=lambda x: x.line):
color = RED if w.severity == Severity.SEVERE else YELLOW
sev_text = "SEVERE " if w.severity == Severity.SEVERE else "WARNING"
msg_lines = w.message.split("\n")
# Print the primary warning line
print(f" {CYAN}Line {w.line:<4}{RESET} | {color}{sev_text}{RESET} | {msg_lines[0]}")
# Print any secondary data (like HTML/Placeholder source & trans contexts) properly aligned
for extra_line in msg_lines[1:]:
print(f" | | {extra_line}")
# Test summary
print("\n== Test Summary ==")
for lang in sorted(failures_by_language.keys()):
counts = failures_by_language[lang]
print(f"{BOLD}[{lang}]{RESET} Severe: {counts['severe']}, Warnings: {counts['warning']}")
total_severe = sum(f["severe"] for f in failures_by_language.values())
total_warning = sum(f["warning"] for f in failures_by_language.values())
print(f"\nTotal Severe: {total_severe}, Total Warnings: {total_warning}")
if total_severe > 0 or (args.strict and total_warning > 0):
return 1
return 0
if __name__ == "__main__":
sys.exit(main())