簡介
Superset的國際化是通過Flask-Babel中的lazy_gettext()和gettext()來實現的。有關的詳細的介紹可以參考國際化和本地化,本文將不再贅述。在superset中,lazy_gettext()和gettext()分別用和_來替代,如下圖所示
from flask_babelimport gettextas __
from flask_babelimport lazy_gettextas _
import pandasas pd
import sqlalchemyas sqla
from sqlalchemyimport create_engine
from sqlalchemy.engine.urlimport make_url
from unidecodeimport unidecode
from werkzeug.routingimport BaseConverter
from werkzeug.utilsimport secure_filename
1、修改配置文件實現支持中文
$SUPERSET_HOME/config.py #筆者練習時所用SUPERT_HOME=~/pythonProjects/incubator-superset/superset
LANGUAGES = {
'en': {'flag':'us', 'name':'English'},
'it': {'flag':'it', 'name':'Italian'},
'fr': {'flag':'fr', 'name':'French'},
'zh': {'flag':'cn', 'name':'Chinese'},
'ja': {'flag':'jp', 'name':'Japanese'},
'de': {'flag':'de', 'name':'German'},
}
2、提取待翻譯文本到messages.pot,并使用message.pot跟新messages.po
cd $SUPERSET_HOME
pybabel extract -F ./translations/babel.cfg -k _ -k __ -k t -k tn -k tct -o ./translations/messages.pot .
pybabel update -i ./translation/messages.pot -d ./translations/ -l zh
# 第一次生成messages.po時使用如下命令
#pybabel update -i ./translation/messages.pot -d ./translations/ -l zh
在之前的版本中babel.cfg存在的路徑是$SUPERSET_HOME/babel
3、翻譯新提取出的待翻譯文本
如下,新生成的po或po中因為更新添加的新詞條的msgstr,即翻譯文本是空白,需添加翻譯文本
msgid "Add Druid Cluster"
msgstr ""
msgid "Add Druid Datasource"
msgstr ""
msgid "Edit Druid Datasource"
msgstr ""
msgid "Show Druid Datasource"
msgstr ""
如下所示方式,添加需要漢化的文本
msgid "Add Druid Cluster"
msgstr "新增Druid集群"
msgid "Add Druid Datasource"
msgstr ""
msgid "Edit Druid Datasource"
msgstr ""
msgid "Show Druid Datasource"
msgstr ""
4、編譯messages.po文件為messages.mo
$SUPERSET_HOME目錄下,執行如下命令
pybabel compile -d translations
注意:要對translations目錄及其子目錄有寫入權限
5、轉messages.po文件為messages.json
npm install po2json -g
po2json -d superset -f jed1.x superset/translations/zh/LC_MESSAGES/messages.po superset/translations/zh/LC_MESSAGES/messages.json