最近用flask-bable翻譯一個項目,在網站上查找到有一個示例文檔,地址:http://translations.readthedocs.io/en/latest/flask-babel.html#html
不過有些地方顯示的不對,特寫此文章進行更改,同時以備本身後期查看使用python
安裝 Flask-Babelflask
Flask-Babel 是 Flask 的翻譯擴展工具。安裝命令以下:瀏覽器
pip install flask-babelbabel
安裝它的時候會順便安裝 Babel、pytz、speaklater 這三個包,其中 Babel 是 Python 的一個國際化工具包。pytz 是處理時區的工具包,speaklater 至關因而 Babel 的一個輔助工具,app
咱們這裏集中在翻譯流程上,這幾個工具就供之後進一步瞭解吧。函數
Hello, World工具
接下來咱們作一個簡單的 Hello World 程序,新建一個叫 hello 的文件夾,在其中建立一個叫 hello.py 的文件,內容以下:網站
注意:須要先安裝flask: pip install flaskspa
#hello.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello():
day = "Saturday"
return render_template('index.html', day=day)
if __name__ == '__main__':
app.run(debug=True)
而後在 hello.py 的同一級目錄下建立一個叫 templates 的文件夾,在其中寫一個 index.html,內容以下:
<p>Hello, world!</p>
<p>It's {{ day }} today.</p>
很簡單的 Hello World 程序,接下來咱們要作的是讓這個站變成中文站。
更新程序和模板
再接下來就是翻譯了。翻譯須要用到 flask-babel 這個 flask 擴展。首先咱們將這個 app 「國際化」,爲模板和 .py 文件中的每個字符串添加一個 gettext 函數,因爲 gettext 函數被引用的次數太多了,爲了方便手寫,就將其 import 爲 「_」:
from flask import Flask, render_template
from flaskext.babel import Babel, gettext as _
app = Flask(__name__)
app.config['BABEL_DEFAULT_LOCALE'] = 'zh'
babel = Babel(app)
@app.route('/')
def hello():
day = _("Saturday")
return render_template('index.html', day=day)
if __name__ == '__main__':
app.run(debug=True)
而後修改模板:
<p>{{ _("Hello, world!") }}</p>
<p>{{ _("It's %(day)s today", day=day) }}</p>
你能夠注意到咱們對 app 的 locale 作了配置,而後用 babel 擴展將 app 再次初始化,而且將 .py 和 .html 中的字符串作了配置,讓它們都使用 gettext 這個函數。其中值得注意的是 gettext 的格式化字符串的參數。
若是直接用相似 "It's %s today" % day 是不行的。
這麼一來,app 的語言實際上是被寫死成中文了。其實你能夠在 flask 程序中讓用戶選擇本身喜愛的語言,或者依據瀏覽器設置用戶優先顯示的語言,詳細作法能夠參考官方文檔中提到 localeselector 的部分。
設置 Babel
接下來咱們要作的是 babel 的配置。在 hello.py 的同級目錄建立一個叫 babel.cfg 的文件,內容以下:
[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
生成翻譯模板
這樣 babel 就知道要從哪些位置搜索要翻譯的字符串了。而後咱們用 pybabel 生成要翻譯的 PO 模板文件,這個命令是 babel 這個工具包帶來的,生成翻譯模板命令以下:
$ pybabel extract -F babel.cfg -o messages.pot .
注意結尾的點「.」,這個點表示當前目錄,目錄是 pybabel 必須的參數,因此命令是沒法執行成功的。messages.pot 就是咱們生成的翻譯模板文件,內容大體以下:
# Translations template for PROJECT.
# Copyright (C) 2011 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR , 2011.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2011-07-26 15:39+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME \n"
"Language-Team: LANGUAGE \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9.6\n"</code>
#: hello.py:11
msgid "Saturday"
msgstr ""
#: templates/index.html:1
msgid "Hello, world!"
msgstr ""
#: templates/index.html:2
#, python-format
msgid "It's %(day)s today"
msgstr ""
你能夠修改裏邊頭文件的信息,把我的和項目相關的資料加進去。
翻譯
接下來咱們建立中文翻譯:
$ pybabel init -i messages.pot -d translations -l zh
這句命令會在 hello 文件夾中生成一個 translations 文件夾,要確保 flask 能找到翻譯內容,translations文件夾要和 templates 文件夾在同一個目錄中。接下來咱們就能夠進行翻譯了,修改 translations/zh/LC_MESSAGES/messages.po 文件,將其中的內容翻譯過來:
# Chinese (China) translations for PROJECT.
# Copyright (C) 2011 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR , 2011.
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2011-07-26 15:39+0800\n"
"PO-Revision-Date: 2011-07-26 09:07+0800\n"
"Last-Translator: FULL NAME \n"
"Language-Team: zh_CN \n"
"Plural-Forms: nplurals=1; plural=0\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9.6\n"</code>
#: hello.py:11
msgid "Saturday"
msgstr "星期六"
#: templates/index.html:1
msgid "Hello, world!"
msgstr "哈羅,世界!"
#: templates/index.html:2
#, fuzzy, python-format
msgid "It's %(day)s today"
msgstr "今天是%(day)s"
PO文件的翻譯其實能夠用專門的工具來編輯,好比 Poedit,不太小文件直接手譯就能夠了。
編譯翻譯結果
翻譯完後執行下面的命令,爲其編譯出 message.mo 文件:
$ pybabel compile -d translations
若是上述命令沒法生成 messages.mo 文件,那你須要將 message.po 中的 #, fuzzy 刪除。
而後就算基本完成了。這時執行 python hello.py 就會看到翻譯的中文頁面了。
更新翻譯
有時咱們須要對程序和模板作修改,翻譯也要隨之更新。更新後須要用前面的命令從新生成 messages.pot 文件,而後使用下面的命令將更新的內容 merge 到原來的翻譯中:
$ pybabel update -i messages.pot -d translations
最後再到對應 locale 的文件夾下更新翻譯並 compile 便可。
整個目錄結構圖下所示:
代碼彙總:
一、新建babel.cfg:[python: **.py][jinja2: **/templates/**.html]extensions=jinja2.ext.autoescape,jinja2.ext.with_二、生成編譯模板pybabel extract -F babel.cfg -o messages.pot .三、翻譯pybabel init -i messages.pot -d translations -l zh_Hans-CN四、手動輸入中文messages.mo五、編譯翻譯結果pybabel compile -d translations六、更新翻譯pybabel update -i messages.pot -d translations