https://github.com/twtrubiks/flask-babel-example
How Use Flask-Babel on Windows - Python Flask
https://github.com/twtrubiks/flask-babel-example
flask-babel localization python-flask tutorial
Last synced: 5 months ago
JSON representation
How Use Flask-Babel on Windows - Python Flask
- Host: GitHub
- URL: https://github.com/twtrubiks/flask-babel-example
- Owner: twtrubiks
- Created: 2016-11-15T14:22:55.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-04T10:06:14.000Z (almost 9 years ago)
- Last Synced: 2025-03-28T19:53:43.894Z (9 months ago)
- Topics: flask-babel, localization, python-flask, tutorial
- Language: Python
- Size: 8.79 KB
- Stars: 26
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Flask-Babel(多國語言轉換) example
How Use Flask-Babel on Windows - Python Flask
* [Demo](https://youtu.be/3mBodR0uWfo)
使用Python [Flask](http://flask.pocoo.org/) 搭配 [Flask-Babel](https://pythonhosted.org/Flask-Babel/) 快速實現多國語系轉換,希望這個簡單的範例可以幫助想要學習的朋友。
## 特色
* 透過 [Flask-Babel](https://pythonhosted.org/Flask-Babel/) 實現多國語系轉換。
* 更多的文件可以參考官方文件 [Flask-Babel](https://pythonhosted.org/Flask-Babel/)
## 安裝套件 Babel 以及 Flask-Babel
請先確定電腦有安裝 [Python](https://www.python.org/)
### Babel
```
pip install babel
```
安裝完後,使用 cmd(命令提示字元) 輸入
```
pybabel --version
```
如果看到版本號,如下圖,代表安裝且設定成功

如果你出現 'pybabel' 不是內部或外部命令、可執行的程式或批次檔。
代表你需要設定環境變數或是安裝失敗。
### Flask-Babel
```
pip install flask-babel
```
## 開始實做
先建立好 flask 之後
建立 settings.cfg ,裡面請輸入,如下圖
```
BABEL_DEFAULT_LOCALE="en"
BABEL_DEFAULT_TIMEZONE="UTC"
```
BABEL_DEFAULT_LOCALE 代表你默認要顯示的語言

程式碼( app.py )裡設定
```
app.config.from_pyfile('settings.cfg')
```
建立 babel.cfg ,裡面請輸入
```
[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
```

接下來請到 目錄下 使用 cmd(命令提示字元) 輸入下列指令,注意,最後那個 . 是需要的 !!
```
pybabel extract -F babel.cfg -o messages.pot .
```

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

先建立中文(zh)的翻譯,到 目錄下 使用 cmd(命令提示字元) 輸入下列指令
```
pybabel init -i messages.pot -d translations -l zh
```

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

messages.po 就是你必須要翻譯的地方!!
以下是我在這裡輸入的翻譯

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

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

如要新增其他語言,以此類推。
如果今天 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
```
以上可能會有點小複雜,但多做幾次你就會了解了 :smile:
接著使用下列指令即可運行
```
python app.py
```
P.S
通常翻譯我們用的語法是
```
gettext("string")
```
但要注意,這個 "string" 裡面的文字,如果有要用的 "%" 這個符號,必須用 全形 !! 如果你用 半形 會出問題。
接下來一個要注意的是只會發生在 中文 ,仔細看一下我的 app.py 裡的 38、39 行
```
test = gettext("我會變亂碼")
```
當我執行 compile ,也就是在 cmd 執行下方指令
```
pybabel compile -d translations
```
你會發現你的模板竟然變亂碼了,如下圖

該怎麼解決呢 ?
其實這是編碼問題 !! 解決方法很簡單
首先,在 app.py 的第一行輸入
```
#coding=utf-8
```
這樣可以解決掉編碼問題

接著,在 index.html 裡面的第 14 行,必須這樣寫
```
{{ test |safe }}
```
要多加上一個 safe
這樣才會顯示 成功 加粗體,如下圖

不然他會顯示

更多說明可以參考 [jinja working-with-automatic-escaping](http://jinja.pocoo.org/docs/2.9/templates/#working-with-automatic-escaping)
## 執行畫面
瀏覽器語系 - 英文

瀏覽器語系 - 中文

## 執行環境
* Python 3.5.2
## License
MIT license