|
5 | 5 | import subprocess
|
6 | 6 | import tempfile
|
7 | 7 |
|
| 8 | +from pathlib import Path |
8 | 9 | from typing import Optional
|
9 | 10 |
|
10 | 11 |
|
@@ -67,27 +68,41 @@ def generate_i18n(self) -> None:
|
67 | 68 | writer.flush()
|
68 | 69 | domain = "behemoth"
|
69 | 70 | for language in self.languages:
|
70 |
| - output_dir = os.path.join(self.app_dir, 'locales', language, 'LC_MESSAGES') |
71 |
| - os.makedirs(output_dir, exist_ok=True) |
| 71 | + output_dir = os.path.join(self.app_dir, 'locales') |
72 | 72 | x_gettext_commands = [
|
73 | 73 | 'xgettext', '-d', domain, '--language=Python', '--keyword=gettext',
|
74 |
| - '--keyword=_', f'--output={os.path.join(output_dir, f"{domain}.po")}', |
| 74 | + '--keyword=_', f'--output={os.path.join(output_dir, f"{domain}.pot")}', |
75 | 75 | '--from-code=UTF-8', '--add-comments=Translators', '--package-name=v0.1',
|
76 | 76 | '[email protected]', '--no-location',
|
77 | 77 | '--copyright-holder='
|
78 | 78 | ]
|
79 | 79 | x_gettext_commands.extend(['--files-from', writer.name])
|
80 | 80 | self._execute_commands(x_gettext_commands)
|
81 | 81 |
|
82 |
| - msg_uniq_commands = [ |
83 |
| - 'msguniq', '--to-code=utf-8', |
84 |
| - '/Users/jiangweidong/resources/jumpserver_pr/jumpserver/apps/locale/django.pot' |
85 |
| - ] |
86 |
| - msg_merge_commands = [ |
87 |
| - 'msgmerge', '-q', '--backup=none', '--previous', '--update', |
88 |
| - '/Users/jiangweidong/resources/jumpserver_pr/jumpserver/apps/locale/zh/LC_MESSAGES/django.po', |
89 |
| - '/Users/jiangweidong/resources/jumpserver_pr/jumpserver/apps/locale/django.pot' |
90 |
| - ] |
| 82 | + msg_uniq_commands = [ |
| 83 | + 'msguniq', '--to-code=utf-8', f'{os.path.join(output_dir, f"{domain}.pot")}' |
| 84 | + ] |
| 85 | + self._execute_commands(msg_uniq_commands) |
| 86 | + |
| 87 | + i18n_dir = os.path.join(output_dir, language, 'LC_MESSAGES') |
| 88 | + os.makedirs(i18n_dir, exist_ok=True) |
| 89 | + po_file = f'{os.path.join(i18n_dir, f"{domain}.po")}' |
| 90 | + pot_file = f'{os.path.join(output_dir, f"{domain}.pot")}' |
| 91 | + if os.path.exists(po_file): |
| 92 | + msg_merge_commands = [ |
| 93 | + 'msgmerge', '-q', '--backup=none', '--previous', |
| 94 | + '--update', po_file, pot_file |
| 95 | + ] |
| 96 | + self._execute_commands(msg_merge_commands) |
| 97 | + messages = Path(po_file).read_text(encoding="utf-8") |
| 98 | + else: |
| 99 | + with open(pot_file, encoding="utf-8") as fp: |
| 100 | + messages = fp.read() |
| 101 | + with open(po_file, "w", encoding="utf-8") as fp: |
| 102 | + fp.write(messages) |
| 103 | + |
| 104 | + if os.path.exists(pot_file): |
| 105 | + os.unlink(pot_file) |
91 | 106 |
|
92 | 107 | def compile_i18n(self) -> None:
|
93 | 108 | # TODO command -> msgfmt messages.po -o messages.mo
|
|
0 commit comments