-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_l10n_cc.py
executable file
·119 lines (90 loc) · 3.99 KB
/
generate_l10n_cc.py
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
#!/usr/bin/env python3
from pathlib import Path
from typing import Dict, List
import argparse
import re
from jinja2 import Environment, FileSystemLoader
def extract_language_from(file_path: Path) -> str:
pattern = "([a-z]{2})\\.po"
found = re.match(pattern, file_path.name)
if not found:
raise RuntimeError(f"Failed to extract language from {str(file_path)!r}")
return found.group(1)
def identify_supported_languages(po_files: Path) -> List[str]:
linguas_file = po_files / "LINGUAS"
with open(linguas_file) as fh:
return [lang.strip() for lang in fh.readlines()]
def load_translations(po_files: Path) -> Dict[str, Dict[str, str]]:
translations: Dict[str, Dict[str, str]] = {}
supported_languages = identify_supported_languages(po_files)
for po_file in po_files.glob("*.po"):
try:
language = extract_language_from(po_file)
except RuntimeError as error:
print(str(error))
continue
if language not in supported_languages:
print(f"Skipping translations for language {language!r}")
continue
print(f"Loading translations for language {language!r}")
language_translations: Dict[str, str] = {}
def store_translation(msgid_tokens: List[str],
msgstr_tokens: List[str]):
msgid = ("".join(msgid_tokens)
.replace('""', "")
.replace("\n", "")
.replace('"', "\""))
if len(msgid) == 0:
return
msgstr = ("".join(msgstr_tokens)
.replace('""', "")
.replace("\n", "")
.replace('"', "\""))
language_translations[msgid] = msgstr
with open(po_file, "r") as fh:
msgid_tokens: List[str] = []
msgstr_tokens: List[str] = []
defining: Optional[str] = None
for line in fh:
if line.startswith("#"):
continue
if line.startswith("msgid "):
defining = "msgid"
msgid_tokens.clear()
msgid_tokens.append(line[6:].strip())
elif line.startswith("msgstr "):
defining = "msgstr"
msgstr_tokens.clear()
msgstr_tokens.append(line[6:].strip())
elif not line.startswith('"'):
if defining is None:
continue
defining = None
store_translation(msgid_tokens, msgstr_tokens)
elif defining == "msgid":
msgid_tokens.append(line.strip())
elif defining == "msgstr":
msgstr_tokens.append(line.strip())
if defining is not None:
store_translation(msgid_tokens, msgstr_tokens)
translations[language] = language_translations
return translations
def generate_file(translations: Dict[str, Dict[str, str]], *,
output_path: Path, template_path: Path):
keys = sorted(list(set([
key for language in translations for key in translations[language]
])))
env = Environment(loader = FileSystemLoader(template_path.parent))
template = env.get_template(template_path.name)
content = template.render(keys=keys, translations=translations)
with open(output_path, "w") as fh:
fh.write(content)
print(f"Template for l10n sources rendered to {str(output_path)!r}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("po_files", type=Path, help="Path to PO files")
parser.add_argument("--output", type=Path, help="Path of file to generate")
parser.add_argument("--template", type=Path, help="Path to template file")
args = parser.parse_args()
translations = load_translations(args.po_files)
generate_file(translations, output_path=args.output, template_path=args.template)