forked from OWASP/cornucopia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_translations.py
More file actions
238 lines (188 loc) · 8.09 KB
/
check_translations.py
File metadata and controls
238 lines (188 loc) · 8.09 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
"""
Translation Tag Checker for OWASP Cornucopia
This script checks that translation files have the same T0xxx tags as the English version.
It detects:
- Missing tags in translations
- Untranslated tags (text identical to English)
- Empty tag values
"""
import sys
import yaml
from pathlib import Path
from typing import Dict, List, Tuple, Any
from collections import defaultdict
class TranslationChecker:
"""Check translations for missing, untranslated, or empty tags."""
def __init__(self, source_dir: Path):
self.source_dir = source_dir
self.results: Dict[str, Dict[str, Any]] = defaultdict(lambda: defaultdict(dict))
def extract_tags(self, yaml_file: Path) -> Dict[str, str]:
"""Extract T0xxx tags and their text from a YAML file."""
tags = {}
try:
with open(yaml_file, "r", encoding="utf-8") as f:
data = yaml.safe_load(f)
# Extract tags from paragraphs.sentences
if data and "paragraphs" in data:
for paragraph in data["paragraphs"]:
if "sentences" in paragraph:
for sentence in paragraph["sentences"]:
tag_id = sentence.get("id", "")
if tag_id.startswith("T0"):
tags[tag_id] = sentence.get("text", "")
except Exception as e:
print(f"Error reading {yaml_file}: {e}", file=sys.stderr)
return tags
def get_file_groups(self) -> Dict[str, List[Path]]:
"""Group YAML files by their base name (e.g., webapp-cards-2.2)."""
file_groups = defaultdict(list)
for yaml_file in self.source_dir.glob("*-*.yaml"):
# Skip archived files
if "archive" in str(yaml_file):
continue
# Extract base name and language
# Format: {edition}-{component}-{version}-{lang}.yaml
parts = yaml_file.stem.split("-")
if len(parts) >= 3:
# Find language code (usually last part or second to last)
lang = parts[-1]
base_name = "-".join(parts[:-1])
# Only process card files with language codes
if "cards" in base_name and len(lang) == 2:
file_groups[base_name].append(yaml_file)
return file_groups
def _separate_english_and_translations(self, files: List[Path]) -> Tuple[Path | None, List[Path]]:
"""Separate English reference file from translation files."""
english_file = None
translation_files = []
for f in files:
lang = f.stem.split("-")[-1]
if lang == "en":
english_file = f
else:
translation_files.append(f)
return english_file, translation_files
def _check_translation_tags(self, english_tags: Dict[str, str], trans_tags: Dict[str, str]) -> Dict[str, Any]:
"""Check translation tags against English reference."""
missing = []
untranslated = []
empty = []
for tag_id, eng_text in english_tags.items():
if tag_id not in trans_tags:
missing.append(tag_id)
elif not trans_tags[tag_id]:
empty.append(tag_id)
elif trans_tags[tag_id] == eng_text:
untranslated.append(tag_id)
return {
"missing": sorted(missing),
"untranslated": sorted(untranslated),
"empty": sorted(empty),
}
def check_translations(self) -> Dict[str, Dict[str, Any]]:
"""
Check all translation files against English versions.
Returns:
Dict with structure:
{
'base_name': {
'language': {
'missing': ['T00145', ...],
'untranslated': ['T00100', ...],
'empty': ['T00200', ...]
}
}
}
"""
file_groups = self.get_file_groups()
for base_name, files in file_groups.items():
english_file, translation_files = self._separate_english_and_translations(files)
if not english_file:
print(f"Warning: No English file found for {base_name}", file=sys.stderr)
continue
english_tags = self.extract_tags(english_file)
if not english_tags:
continue
for trans_file in translation_files:
lang = trans_file.stem.split("-")[-1]
trans_tags = self.extract_tags(trans_file)
issues = self._check_translation_tags(english_tags, trans_tags)
if any(issues.values()):
issues["file"] = str(trans_file.name)
self.results[base_name][lang] = issues
return dict(self.results)
def generate_markdown_report(self) -> str:
"""Generate a Markdown report of translation issues."""
report_lines = []
if not self.results:
report_lines.append("# Translation Check Report\n")
report_lines.append("✅ All existing translations have been completed.\n")
return "\n".join(report_lines)
report_lines.append("# Translation Check Report\n")
report_lines.append("The following sentences/tags have issues in the translations:\n")
# Language name mapping
lang_names = {
"es": "Spanish",
"fr": "French",
"hu": "Hungarian",
"it": "Italian",
"nl": "Dutch",
"no-nb": "Norwegian",
"pt-br": "Portuguese (Brazil)",
"pt-pt": "Portuguese (Portugal)",
"ru": "Russian",
}
for base_name in sorted(self.results.keys()):
languages = self.results[base_name]
for lang in sorted(languages.keys()):
lang_name = lang_names.get(lang, lang)
issues = languages[lang]
filename = issues.get("file", "")
report_lines.append(f"\n## {lang_name}\n")
report_lines.append(f"**File:** `{filename}`\n")
if issues["missing"]:
report_lines.append("### Missing Tags\n")
report_lines.append(
"The following tags are present in the English version but missing in this translation:\n"
)
tags_str = ", ".join(issues["missing"])
report_lines.append(f"{tags_str}\n")
if issues["untranslated"]:
report_lines.append("### Untranslated Tags\n")
report_lines.append("The following tags have identical text to English (not translated):\n")
tags_str = ", ".join(issues["untranslated"])
report_lines.append(f"{tags_str}\n")
if issues["empty"]:
report_lines.append("### Empty Tags\n")
report_lines.append("The following tags are empty:\n")
tags_str = ", ".join(issues["empty"])
report_lines.append(f"{tags_str}\n")
return "\n".join(report_lines)
def main() -> None:
"""Main entry point for the translation checker."""
# Determine source directory
script_dir = Path(__file__).parent
base_dir = script_dir.parent
source_dir = base_dir / "source"
if not source_dir.exists():
print(f"Error: Source directory not found: {source_dir}", file=sys.stderr)
sys.exit(1)
# Run checker
checker = TranslationChecker(source_dir)
results = checker.check_translations()
# Generate report
report = checker.generate_markdown_report()
# Output report
print(report)
# Write to file
output_file = base_dir / "translation_check_report.md"
with open(output_file, "w", encoding="utf-8") as f:
f.write(report)
print(f"\n---\nReport written to: {output_file}", file=sys.stderr)
# Exit with error code if issues found
if results:
sys.exit(1)
else:
sys.exit(0)
if __name__ == "__main__":
main()