Skip to content

Commit 43fbbf6

Browse files
perf: 国际化优化,支持po脚本生成
1 parent e9afb2f commit 43fbbf6

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# This file is put in the public domain.
3+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4+
#
5+
#, fuzzy
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: v0.1\n"
9+
"Report-Msgid-Bugs-To: [email protected]\n"
10+
"POT-Creation-Date: 2023-10-06 14:00+0800\n"
11+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
12+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13+
"Language-Team: LANGUAGE <[email protected]>\n"
14+
"Language: \n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=CHARSET\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
19+
msgid "Server error"
20+
msgstr "服务器错误"

scripts/i18n/i18n.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import tempfile
77

8+
from pathlib import Path
89
from typing import Optional
910

1011

@@ -67,27 +68,41 @@ def generate_i18n(self) -> None:
6768
writer.flush()
6869
domain = "behemoth"
6970
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')
7272
x_gettext_commands = [
7373
'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")}',
7575
'--from-code=UTF-8', '--add-comments=Translators', '--package-name=v0.1',
7676
'[email protected]', '--no-location',
7777
'--copyright-holder='
7878
]
7979
x_gettext_commands.extend(['--files-from', writer.name])
8080
self._execute_commands(x_gettext_commands)
8181

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)
91106

92107
def compile_i18n(self) -> None:
93108
# TODO command -> msgfmt messages.po -o messages.mo

0 commit comments

Comments
 (0)