Code Monkey home page Code Monkey logo

flask-babel-example's Introduction

Flask-Babel(多國語言轉換) example

How Use Flask-Babel on Windows - Python Flask

使用Python Flask 搭配 Flask-Babel 快速實現多國語系轉換,希望這個簡單的範例可以幫助想要學習的朋友。

特色

安裝套件 Babel 以及 Flask-Babel

請先確定電腦有安裝 Python

Babel

pip install babel

安裝完後,使用 cmd(命令提示字元) 輸入

pybabel --version

如果看到版本號,如下圖,代表安裝且設定成功
alt tag

如果你出現 'pybabel' 不是內部或外部命令、可執行的程式或批次檔。

代表你需要設定環境變數或是安裝失敗。

Flask-Babel

pip install flask-babel

開始實做

先建立好 flask 之後

建立 settings.cfg ,裡面請輸入,如下圖

BABEL_DEFAULT_LOCALE="en"
BABEL_DEFAULT_TIMEZONE="UTC"

BABEL_DEFAULT_LOCALE 代表你默認要顯示的語言

alt tag

程式碼( app.py )裡設定

app.config.from_pyfile('settings.cfg')

建立 babel.cfg ,裡面請輸入

[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_

alt tag

接下來請到 目錄下 使用 cmd(命令提示字元) 輸入下列指令,注意,最後那個 . 是需要的 !!

pybabel extract -F babel.cfg -o messages.pot .

alt tag

到專案底下會發現多出一個檔案 messages.pot , pot 檔只是翻譯模板,檔案內不包含任何翻譯 alt tag

先建立中文(zh)的翻譯,到 目錄下 使用 cmd(命令提示字元) 輸入下列指令

pybabel init -i messages.pot -d translations -l zh

alt tag

到專案下會發現多出一個資料夾,如下圖 alt tag

messages.po 就是你必須要翻譯的地方!!

以下是我在這裡輸入的翻譯
alt tag

最後只需要到 目錄下 使用 cmd(命令提示字元) 輸入以下指令進行編譯即完成。

pybabel compile -d translations

alt tag

編譯完後,資料夾會多出 messages.mo
alt tag

如要新增其他語言,以此類推。

如果今天 messages.pot 有更新了,只需要執行以下指令

pybabel update -i messages.pot -d translations

如果你又有新增需要翻譯的文字,這時候你該怎麼辦呢 ?

首先,先執行下方的指令,讓他重新掃一次整個目錄

pybabel extract -F babel.cfg -o messages.pot .

執行後你會發現 messages.pot 裡面多了你剛剛的更新 (也就是新增加的文字)

由於 messages.pot 更新了,所以必須要再執行以下指令

pybabel update -i messages.pot -d translations

你會發現 en 資料夾(這裡舉例英文) 裡面的 messages.po 多了你剛剛的更新內容

這時候,再翻譯,翻譯完畢後,再用以下的指令編譯就完成了

pybabel compile -d translations

以上可能會有點小複雜,但多做幾次你就會了解了 😄

接著使用下列指令即可運行

python app.py

P.S

通常翻譯我們用的語法是

gettext("string")

但要注意,這個 "string" 裡面的文字,如果有要用的 "%" 這個符號,必須用 全形 !! 如果你用 半形 會出問題。

接下來一個要注意的是只會發生在 中文 ,仔細看一下我的 app.py 裡的 38、39 行

test = gettext("<b>我會變亂碼</b>")

當我執行 compile ,也就是在 cmd 執行下方指令

pybabel compile -d translations

你會發現你的模板竟然變亂碼了,如下圖

alt tag

該怎麼解決呢 ?

其實這是編碼問題 !! 解決方法很簡單

首先,在 app.py 的第一行輸入

#coding=utf-8

這樣可以解決掉編碼問題

alt tag

接著,在 index.html 裡面的第 14 行,必須這樣寫

<p>{{ test |safe }}</p>

要多加上一個 safe

這樣才會顯示 成功 加粗體,如下圖

alt tag

不然他會顯示

alt tag

更多說明可以參考 jinja working-with-automatic-escaping

執行畫面

瀏覽器語系 - 英文
alt tag

瀏覽器語系 - 中文
alt tag

執行環境

  • Python 3.5.2

License

MIT license

flask-babel-example's People

Contributors

twtrubiks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sjl421

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.