{"id":20619735,"url":"https://github.com/twtrubiks/flask-babel-example","last_synced_at":"2025-07-27T15:36:26.673Z","repository":{"id":84518950,"uuid":"73820873","full_name":"twtrubiks/Flask-Babel-example","owner":"twtrubiks","description":"How Use Flask-Babel on Windows - Python Flask","archived":false,"fork":false,"pushed_at":"2017-02-04T10:06:14.000Z","size":9,"stargazers_count":26,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T19:53:43.894Z","etag":null,"topics":["flask-babel","localization","python-flask","tutorial"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twtrubiks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-15T14:22:55.000Z","updated_at":"2024-10-21T11:01:42.000Z","dependencies_parsed_at":"2023-03-02T04:30:27.445Z","dependency_job_id":null,"html_url":"https://github.com/twtrubiks/Flask-Babel-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FFlask-Babel-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FFlask-Babel-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FFlask-Babel-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FFlask-Babel-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/Flask-Babel-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067779,"owners_count":21207395,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flask-babel","localization","python-flask","tutorial"],"created_at":"2024-11-16T12:12:23.492Z","updated_at":"2025-04-15T12:01:54.302Z","avatar_url":"https://github.com/twtrubiks.png","language":"Python","readme":"# Flask-Babel(多國語言轉換) example\nHow Use Flask-Babel on Windows - Python Flask\n\n* [Demo](https://youtu.be/3mBodR0uWfo)  \n\n使用Python [Flask](http://flask.pocoo.org/) 搭配 [Flask-Babel](https://pythonhosted.org/Flask-Babel/) 快速實現多國語系轉換，希望這個簡單的範例可以幫助想要學習的朋友。\n\n## 特色\n* 透過 [Flask-Babel](https://pythonhosted.org/Flask-Babel/) 實現多國語系轉換。\n* 更多的文件可以參考官方文件 [Flask-Babel](https://pythonhosted.org/Flask-Babel/)\n\n## 安裝套件 Babel 以及 Flask-Babel\n請先確定電腦有安裝 [Python](https://www.python.org/)\n\n### Babel\n``` \npip install babel\n```\n安裝完後，使用 cmd(命令提示字元) 輸入\n``` \npybabel --version\n```\n如果看到版本號，如下圖，代表安裝且設定成功\u003cbr\u003e\n![alt tag](http://i.imgur.com/wKVo5mD.png)\n\n如果你出現 \u003cb\u003e 'pybabel' 不是內部或外部命令、可執行的程式或批次檔。 \u003c/b\u003e\n\n代表你需要設定環境變數或是安裝失敗。\n\n### Flask-Babel\n\n``` \npip install flask-babel\n```\n\n## 開始實做\n\n先建立好 flask 之後\n\n建立 \u003cb\u003e settings.cfg \u003c/b\u003e ，裡面請輸入，如下圖\n``` \nBABEL_DEFAULT_LOCALE=\"en\"\nBABEL_DEFAULT_TIMEZONE=\"UTC\"\n```\nBABEL_DEFAULT_LOCALE 代表你默認要顯示的語言\n\n![alt tag](http://i.imgur.com/MkYNAqA.png)\n\n程式碼( app.py )裡設定\n``` \napp.config.from_pyfile('settings.cfg')\n```\n\n建立 \u003cb\u003e babel.cfg \u003c/b\u003e ，裡面請輸入\n``` \n[python: **.py]\n[jinja2: **/templates/**.html]\nextensions=jinja2.ext.autoescape,jinja2.ext.with_\n```\n![alt tag](http://i.imgur.com/TbEC8CW.png)\n\n接下來請到 目錄下 使用 cmd(命令提示字元) 輸入下列指令，注意，最後那個 \u003cb\u003e.\u003c/b\u003e 是需要的 !!\n``` \npybabel extract -F babel.cfg -o messages.pot .\n```\n![alt tag](http://i.imgur.com/sv6Gd99.png)\n\n到專案底下會發現多出一個檔案\u003cb\u003e messages.pot \u003c/b\u003e ,\u003cb\u003e pot 檔只是翻譯模板，檔案內不包含任何翻譯 \u003c/b\u003e。\n![alt tag](http://i.imgur.com/NnW2Ues.png)\n\n先建立中文(zh)的翻譯，到 目錄下 使用 cmd(命令提示字元) 輸入下列指令\n``` \npybabel init -i messages.pot -d translations -l zh\n```\n![alt tag](http://i.imgur.com/MuabvLi.png)\n\n到專案下會發現多出一個資料夾，如下圖\n![alt tag](http://i.imgur.com/BCqNoz2.png)\n\n\u003cb\u003e messages.po \u003c/b\u003e 就是你必須要翻譯的地方!!\n\n以下是我在這裡輸入的翻譯\n\u003cbr\u003e\n![alt tag](http://i.imgur.com/u3elJwk.png)\n\n最後只需要到 目錄下 使用 cmd(命令提示字元) 輸入以下指令進行編譯即完成。\n``` \npybabel compile -d translations\n```\n![alt tag](http://i.imgur.com/PF8X9fc.png)\n\n編譯完後，資料夾會多出 \u003cb\u003e messages.mo \u003c/b\u003e\n\u003cbr\u003e\n![alt tag](http://i.imgur.com/XpIasBS.png)\n\n如要新增其他語言，以此類推。\n\n如果今天\u003cb\u003e messages.pot \u003c/b\u003e 有更新了，只需要執行以下指令\n``` \npybabel update -i messages.pot -d translations\n```\n如果你又有新增需要翻譯的文字，這時候你該怎麼辦呢 ?\n\n首先，先執行下方的指令，讓他重新掃一次整個目錄\n``` \npybabel extract -F babel.cfg -o messages.pot .\n```\n\n執行後你會發現 \u003cb\u003e messages.pot\u003c/b\u003e 裡面多了你剛剛的更新 (也就是新增加的文字)\n\n由於 \u003cb\u003e messages.pot\u003c/b\u003e 更新了，所以必須要再執行以下指令\n\n``` \npybabel update -i messages.pot -d translations\n```\n\n你會發現 en 資料夾(這裡舉例英文) 裡面的  \u003cb\u003emessages.po\u003c/b\u003e 多了你剛剛的更新內容\n\n這時候，再翻譯，翻譯完畢後，再用以下的指令編譯就完成了\n\n``` \npybabel compile -d translations\n```\n\n以上可能會有點小複雜，但多做幾次你就會了解了 :smile:\n\n接著使用下列指令即可運行\n\n``` \npython app.py\n```\n\nP.S\n\n通常翻譯我們用的語法是\n``` \ngettext(\"string\")\n```\n但要注意，這個 \"string\" 裡面的文字，如果有要用的 \"%\" 這個符號，必須用 \u003cb\u003e全形\u003c/b\u003e !! 如果你用 \u003cb\u003e半形\u003c/b\u003e 會出問題。\n\n接下來一個要注意的是只會發生在 \u003cb\u003e中文\u003c/b\u003e ，仔細看一下我的 app.py 裡的 38、39 行\n``` \ntest = gettext(\"\u003cb\u003e我會變亂碼\u003c/b\u003e\")\n```\n當我執行 compile ，也就是在 cmd 執行下方指令\n``` \npybabel compile -d translations\n```\n你會發現你的模板竟然變亂碼了，如下圖\n\n![alt tag](http://i.imgur.com/Wh4Lhie.jpg)\n\n該怎麼解決呢 ?\n\n其實這是編碼問題 !!  解決方法很簡單\n\n首先，在 \u003cb\u003eapp.py\u003c/b\u003e 的第一行輸入 \n``` \n#coding=utf-8\n```\n這樣可以解決掉編碼問題\n\n![alt tag](http://i.imgur.com/CbQALOJ.jpg)\n\n接著，在 \u003cb\u003eindex.html\u003c/b\u003e 裡面的第 14 行，必須這樣寫\n\n``` \n\u003cp\u003e{{ test |safe }}\u003c/p\u003e\n```\n\n要多加上一個 safe\n\n這樣才會顯示 \u003cb\u003e成功\u003c/b\u003e 加粗體，如下圖\n\n![alt tag](http://i.imgur.com/SBFNm1Z.jpg)\n\n不然他會顯示\n\n![alt tag](http://i.imgur.com/4pKd9wN.jpg)\n\n更多說明可以參考 [jinja working-with-automatic-escaping](http://jinja.pocoo.org/docs/2.9/templates/#working-with-automatic-escaping)\n\n\n\n\n## 執行畫面\n\n瀏覽器語系 - 英文\u003cbr\u003e\n![alt tag](http://i.imgur.com/Po0mZkl.png)\n\n瀏覽器語系 - 中文\u003cbr\u003e\n![alt tag](http://i.imgur.com/786Wdmn.png)\n\n\n## 執行環境\n* Python 3.5.2\n\n## License\nMIT license\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fflask-babel-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fflask-babel-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fflask-babel-example/lists"}