[ Django ]如何作i18n

在 .py 檔內請使用 _ 函數,例如 _(“some string”),在 template 內請先在開頭載入 {% load i18n %},然後使用 {% trans “some string” %}。 然後在每個 app 裡確定放置了 locale 資料夾及 babel.cfg 檔,內容為:

[django: templates/**.*]
[python: *.py]
[python: locale/*.txt]
[javascript: static/**.js]

要開始翻譯時,使用:

python manage.py i18n

即可。

這會在每個 locale 資料夾裡最下層產生不同語系的 .po 檔(一般為 django.po),請使用 ​poedit 來進行翻譯,並存檔即可(會產生 .mo 檔,即為結果) 如何製作javascript的i18n 請遵循以下步驟:

1.在 .js檔 要翻譯的字串加上 gettext(‘我是要翻譯的字串’) 2.確定有 babel.cfg ,內容有加入[javascript: static/**.js] 3.正常執行i18n翻譯:

python manage.py i18n

4.在 .po 檔輸入欲翻譯之內容 5.然後執行:

python manage.py geni18nstatic

6.之後會產生不同語系的js檔,請先載入 i18n.js 後再載入翻譯過的 .js 檔

Was this article helpful? Votes: 0
Article details:
Published date: 30/08/2017 6:05AM
Last updated: 30/08/2017 6:52AM (Olivia - olivia.wu@nuwainfo.com)
Share article: 
Author: Olivia (olivia.wu@nuwainfo.com)
Django | javascript |