flask_babelex 使用

# flask_babelex 使用

# https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xiii-i18n-and-l10n
# 请在rqlicense-server 目录下操作    
# 生成翻译文件模版    
pybabel extract -F babel.cfg -k _l -o messages.pot .    
# 生成翻译文件 一般只需要 init 一次    
pybabel init -i messages.pot -d rqlicense/translations -l en    
# 更新翻译文件    
pybabel update -i messages.pot -d rqlicense/translations -l en    
# 编译    
pybabel compile -d rqlicense/translations    
1
2
3
4
5
6
7
8
9
10
  • 附赠 messages.po 谷歌翻译脚本
#!/usr/bin/python3    
# encoding: utf-8     
# @Time    : 2019/12/13 15:25    
# @author  : zza    
# @Email   : 740713651@qq.com    
# @File    : 翻译messages.po文件.py    
import re    
from tqdm import tqdm    
from googletrans import Translator    
    
proxies = {"http": 'http://localhost:9999',    
           "https": 'https://localhost:9999'}    
translate = Translator(proxies=proxies)    
    
    
def service(messages_po_path):    
    with open(messages_po_path, "r", encoding="utf8") as f:    
        messages_body = f.read()    
    messages_lines = messages_body.split("\n")    
    result_lines = []    
    msgid = ""    
    msgstr = ""    
    for line in tqdm(messages_lines):    
        if line.startswith("msgid"):    
            msgid = line    
        elif line.startswith("msgstr"):    
            msgstr = line    
            if msgid == 'msgid ""':    
                pass    
            elif msgstr == 'msgstr ""':    
                translate_str = re.findall(r"msgid \"(.*)\"", msgid)[0]    
                en_str = translate.translate(translate_str).text    
                msgstr = msgstr.replace('""', '"{}"'.format(en_str))    
            result_lines.append(msgid)    
            result_lines.append(msgstr)    
        else:    
            result_lines.append(line)    
    result_body = "\n".join(result_lines)    
    messages_po_to_path = messages_po_path.replace(".po", "bak.po")    
    with open(messages_po_to_path, "w", encoding="utf8") as f:    
        f.write(result_body)    
    
    
if __name__ == '__main__':    
    messages_po_path = r"D:\PycharmProjects\rqlicense\rqlicense-server\rqlicense\translations\en\LC_MESSAGES\messages.po"    
    service(messages_po_path)    
    
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
上次更新: 2024-01-10 16:01:00
最近更新
01
自建 RSS 订阅
01-05
02
搭建私人 docker hub
01-02
03
关于 SSH 攻击
10-12
更多文章>