-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho-update-po
executable file
Β·159 lines (127 loc) Β· 6.06 KB
/
o-update-po
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
#!/usr/bin/env python3
"""Tool to update translation files in an Odoo codebase"""
import argparse
import glob
import os
import subprocess
import sys
from pathlib import Path
from utils.terminal_logger import TerminalLogger, log
def update_translation_files(modules, lang_codes, addons_path, base_i18n_path, tl):
log(f"π¬ {tl.WHITE}{tl.B}Update Translations{tl.NOFORMAT}\n")
log(f"Updating translations for modules {tl.B}{f'{tl.B_END}, {tl.B}'.join(modules)}{tl.B_END} ...")
log("β")
success = failure =False
for i, module in enumerate(modules, start=1):
last_module = i == len(modules)
tree_prepend = "βββ" if last_module else "βββ"
log(f"{tree_prepend} {tl.B}{module}{tl.B_END}")
i18n_path = os.path.join(addons_path, module, "i18n")
if module == "base":
i18n_path = base_i18n_path
if not os.path.exists(i18n_path):
tree_prepend = " βββ" if last_module else "β βββ"
log(f"{tree_prepend} {tl.BR_BLACK}No translations found!{tl.NOFORMAT}")
continue
po_files = sorted(filter(lambda f: f.endswith(".po"), os.listdir(i18n_path)))
if lang_codes:
po_files = filter(lambda po: po[:-2] in lang_codes, po_files)
for j, po_file in enumerate(po_files, start=1):
last_po_file = j == len(po_files)
if last_module and last_po_file:
tree_prepend = " βββ"
elif last_po_file:
tree_prepend = "β βββ"
elif last_module:
tree_prepend = " βββ"
else:
tree_prepend = "β βββ"
po_file_path = os.path.join(i18n_path, po_file)
pot_file_path = os.path.join(i18n_path, f"{module}.pot")
result = subprocess.run(
[
"msgmerge",
"--no-fuzzy-matching",
"-q",
po_file_path,
pot_file_path,
],
capture_output=True,
)
if result.returncode:
failure = True
log(f"{tree_prepend} {tl.RED}{tl.B}!{tl.B_END} Updating {tl.U}{po_file_path}{tl.U_END} failed during {tl.I}msgmerge{tl.I_END}!{tl.NOFORMAT}")
log(f"\n{result.stderr.decode()}\n")
continue
result = subprocess.run(
[
"msgattrib",
"--no-fuzzy",
"--no-obsolete",
"-o",
po_file_path,
],
input=result.stdout,
)
if result.returncode:
failure = True
log(f"{tree_prepend} {tl.RED}{tl.B}!{tl.B_END} Updating {tl.U}{po_file_path}{tl.U_END} failed during {tl.I}msgattrib{tl.I_END}!{tl.NOFORMAT}")
log(f"\n{result.stderr.decode()}\n")
else:
success = True
log(f"{tree_prepend} Updated {tl.U}{po_file_path}{tl.U_END} βοΈ")
if not last_module:
log("β")
if not success and failure:
log(f"\n{tl.RED}{tl.B}!{tl.B_END} All translation files failed to update ...{tl.NOFORMAT}")
elif success and failure:
log(f"\n{tl.YELLOW}{tl.B}!{tl.B_END} Some translation files were updated correctly, while others failed ...{tl.NOFORMAT}")
elif success and not failure:
log(f"\n{tl.GREEN}All translation files were updated correctly!{tl.NOFORMAT} πͺ")
def update_modules_translations(modules_per_path, lang_codes, base_i18n_path, tl):
for modules_list, modules_path in modules_per_path:
if not modules_list:
continue
update_translation_files(modules_list, lang_codes, modules_path, base_i18n_path, tl)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Update Odoo translation files (.po) according to a new version of its .pot file."
)
parser.add_argument("-m", "--modules", help="The list of modules to update (comma-separated)")
parser.add_argument("-l", "--languages", help="The language codes to export (e.g. en,fr_BE) (default: all languages)")
parser.add_argument("-c", "--community-path", default="odoo", help="The relative path to your Odoo Community repo (default: odoo)")
parser.add_argument("-e", "--enterprise-path", default="enterprise", help="The relative path to your Odoo Enterprise repo (default: enterprise)")
parser.add_argument("--no-color", action="store_true", help="Disable colors in the terminal")
args = parser.parse_args()
tl = TerminalLogger(args.no_color)
log(f"π£ {tl.MAGENTA}{tl.B}Odoo PO Update{tl.NOFORMAT}\n")
base_i18n_path = os.path.join(Path(args.community_path).resolve(), "odoo/addons/base/i18n")
community_modules_path = os.path.join(Path(args.community_path).resolve(), "addons")
enterprise_modules_path = Path(args.enterprise_path).resolve()
community_modules = {
os.path.basename(os.path.dirname(f))
for f in glob.glob(os.path.join(community_modules_path, "*/__manifest__.py"))
}
community_modules.add("base")
enterprise_modules = {
os.path.basename(os.path.dirname(f))
for f in glob.glob(os.path.join(enterprise_modules_path, "*/__manifest__.py"))
}
if args.modules == "community":
modules = community_modules
elif args.modules == "enterprise":
modules = enterprise_modules
elif args.modules:
modules = set(args.modules.split(","))
else:
log(f"{tl.RED}{tl.B}!{tl.B_END} No modules provided to update! Terminating the process ...{tl.NOFORMAT}")
sys.exit(1)
modules_per_path = [
(sorted(listed_modules & modules), path)
for listed_modules, path in [
(community_modules, community_modules_path),
(enterprise_modules, enterprise_modules_path),
]
]
lang_codes = set(args.languages.split(",")) if args.languages else None
update_modules_translations(modules_per_path, lang_codes, base_i18n_path, tl)