|
13 | 13 | from typing import Dict |
14 | 14 |
|
15 | 15 |
|
16 | | -# 需要扫描的语言 |
| 16 | +# 配置1:需要扫描的语言 |
17 | 17 | LANGUAGES = ['en_US', 'zh_CN', 'zh_Hant'] |
18 | 18 |
|
19 | | -# 是否保留无代码来源的项 |
20 | | -# 使用说明:如果想从 django.po 文件中清除它们,请将此属性设置为 False |
| 19 | +# 配置2:是否保留无代码来源的项? |
| 20 | +# 说明:如果想从 django.po 文件中清除它们,请将此属性设置为 False |
21 | 21 | KEEP_NO_SOURCE = True |
22 | 22 |
|
23 | | -# 是否备份原 django.po 文件。 |
| 23 | +# 配置3:是否保留翻译与原文一样的翻译内容? |
| 24 | +# 说明:默认:不保留,因为与原文一样没必要保留。 |
| 25 | +# 例:为False时: |
| 26 | +# ```text |
| 27 | +# msgid "xxx" |
| 28 | +# msgstr "xxx" |
| 29 | +# ``` |
| 30 | +# 会被简化为 |
| 31 | +# ```text |
| 32 | +# msgid "xxx" |
| 33 | +# msgstr "" |
| 34 | +# ``` |
| 35 | +KEEP_SAME_TRANSLATION = False |
| 36 | + |
| 37 | +# 配置4:是否备份原 django.po 文件。 |
24 | 38 | # 说明:因为有VCS,所以默认不备份。 |
25 | 39 | BACKUP_PO = False |
26 | 40 |
|
@@ -68,11 +82,13 @@ def parse_po_file(self, po_file_path: Path) -> Dict[str, str]: |
68 | 82 | # 跳过空字符串 |
69 | 83 | if msgid and msgid.strip(): |
70 | 84 | # 处理转义字符 |
71 | | - msgid = self._unescape_string(msgid) |
72 | | - msgstr = self._unescape_string(msgstr) |
| 85 | + msgid_unescaped = self._unescape_string(msgid) |
| 86 | + msgstr_unescaped = self._unescape_string(msgstr) |
73 | 87 |
|
74 | | - if msgstr and msgid != msgstr: # 只保存有翻译的内容 |
75 | | - translations[msgid] = msgstr |
| 88 | + if msgstr_unescaped: |
| 89 | + # 当 配置 KEEP_SAME_TRANSLATION 为 True 或 翻译内容与原文不一致,则保留 |
| 90 | + if KEEP_SAME_TRANSLATION or msgstr_unescaped != msgid_unescaped: |
| 91 | + translations[msgid_unescaped] = msgstr_unescaped |
76 | 92 |
|
77 | 93 | return translations |
78 | 94 |
|
@@ -115,30 +131,44 @@ def _extract_po_entries(self, content: str) -> Dict[str, str]: |
115 | 131 | # 如果遇到 msgstr,停止读取 msgid |
116 | 132 | elif next_line.startswith('msgstr '): |
117 | 133 | break |
| 134 | + # 跳过空行和注释,继续寻找 msgstr |
| 135 | + elif next_line == '' or next_line.startswith('#'): |
| 136 | + i += 1 |
| 137 | + continue |
118 | 138 | # 其他情况也停止 |
119 | 139 | else: |
120 | 140 | print(f"其他情况也停止: {next_line}") |
121 | 141 | break |
122 | 142 |
|
123 | | - # 现在查找 msgstr |
| 143 | + # 现在查找 msgstr(跳过中间的空行和注释) |
124 | 144 | msgstr_parts = [] |
125 | | - if i < len(lines) and lines[i].strip().startswith('msgstr '): |
126 | | - msgstr_line = lines[i].strip()[7:].strip() # 去掉 'msgstr ' |
127 | | - if msgstr_line: |
128 | | - msgstr_parts.append(msgstr_line) |
| 145 | + while i < len(lines): |
| 146 | + current_line = lines[i].strip() |
| 147 | + if current_line.startswith('msgstr '): |
| 148 | + msgstr_line = current_line[7:].strip() # 去掉 'msgstr ' |
| 149 | + if msgstr_line: |
| 150 | + msgstr_parts.append(msgstr_line) |
| 151 | + i += 1 |
| 152 | + break |
| 153 | + # 跳过空行和注释,继续寻找 msgstr |
| 154 | + elif current_line == '' or current_line.startswith('#'): |
| 155 | + i += 1 |
| 156 | + continue |
| 157 | + else: |
| 158 | + # 遇到其他内容,说明没有 msgstr |
| 159 | + i += 1 |
| 160 | + print(f"遇到其他内容,说明没有 msgstr: {current_line}") |
| 161 | + break |
129 | 162 |
|
130 | | - # 继续读取 msgstr 的后续行 |
131 | | - i += 1 |
| 163 | + # 如果找到了 msgstr,继续读取其后续行 |
| 164 | + if msgstr_parts: |
132 | 165 | while i < len(lines): |
133 | 166 | next_line = lines[i].strip() |
134 | 167 | if next_line.startswith('"'): |
135 | 168 | msgstr_parts.append(next_line) |
136 | 169 | i += 1 |
137 | 170 | else: |
138 | 171 | break |
139 | | - else: |
140 | | - i += 1 |
141 | | - continue |
142 | 172 |
|
143 | 173 | # 合并多行字符串 |
144 | 174 | if msgid_parts and msgstr_parts: |
|
0 commit comments