当前位置:   article > 正文

Flask-AppBuilder中用babel统一管理提示信息_writing po template file to messages.pot

writing po template file to messages.pot

不仅仅flask-appbuilder甚至是flask的开发过程中会在代码中写死很多提示信息如“输入错误”、该页面不存在等等,一开始对babel支持多语言的功能不太感兴趣,觉得不会做多语言版本,随着项目越做越大修改个信息很麻烦,希望有一个文件把所有提示信息统一管起来。

flask-appbuilder中babel目录结构

flask-appbulider中集成的是flask-babel,目录结构中包括babel目录和app\translations
在这里插入图片描述
babel目录下babel.cfg 是配置文件 messages.pot是生成提示信息文件
在这里插入图片描述
app\translations目录下放置不同语言版的信息,我这里没有其他语言
在这里插入图片描述

babel使用

  1. 配置babel
    初始化
[python: app/**.py]
[jinja2: app/templates/**.html]
encoding = utf-8
  • 1
  • 2
  • 3

用flask fab create-app 生成的是这样的,这个设置会搜索工程下所有的目录

python: **.py]
[jinja2: **/templates/**.html]
encoding = utf-8
  • 1
  • 2
  • 3

代码中使用
index.html
在这里插入图片描述
404.html
在这里插入图片描述

  1. 提取并所有消息
    使用flask-appbulider的提供内置命令
(venv) D:\demo>flask fab babel-extract
Starting Extractions config:./babel/babel.cfg input:. output:./babel/messages.pot keywords:('lazy_gettext', 'gettext', '_', '__')
Starting Update target:app/translations
Finish, you can start your translations

(venv) D:\demo>updating catalog app/translations\zh\LC_MESSAGES\messages.po based on ./babel/messages.pot
extracting messages from app\PyIndexView.py
extracting messages from app\__init__.py
extracting messages from app\models.py
extracting messages from app\views.py
extracting messages from app\templates\404.html (encoding="utf-8")
extracting messages from app\templates\index.html (encoding="utf-8")
writing PO template file to ./babel/messages.pot
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

生成内容如下

#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2022-03-19 00:25+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.9.1\n"

#: app/templates/404.html:4
msgid "Page not found"
msgstr ""

#: app/templates/index.html:4
msgid "Welcome info"
msgstr ""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  1. 制作翻译文件
    用初始化命令建立翻译文件,在flask-appbulider中,中文用zh
   "en": {"flag": "gb", "name": "English"},
    "pt": {"flag": "pt", "name": "Portuguese"},
    "pt_BR": {"flag": "br", "name": "Pt Brazil"},
    "es": {"flag": "es", "name": "Spanish"},
    "de": {"flag": "de", "name": "German"},
    "zh": {"flag": "cn", "name": "Chinese"},
    "ru": {"flag": "ru", "name": "Russian"},
    "pl": {"flag": "pl", "name": "Polish"},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
# 增加中文
(venv) D:\demo>pybabel init -i babel/messages.pot -d app/translations -l zh

creating catalog app/translations\zh\LC_MESSAGES\messages.po based on babel/messages.pot
  • 1
  • 2
  • 3
  • 4

信息如下:

# FIRST AUTHOR <EMAIL@ADDRESS>, 2022.
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2022-03-19 00:25+0800\n"
"PO-Revision-Date: 2022-03-19 00:33+0800\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: zh\n"
"Language-Team: zh <LL@li.org>\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 2.9.1\n"

#: app/templates/404.html:4
msgid "Page not found"
msgstr ""

#: app/templates/index.html:4
msgid "Welcome info"
msgstr ""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

如果初始化过只需要更新用如下命令:

# 更新翻译信息
(venv) D:\demo>pybabel update -i babel/messages.pot -d app/translations
  • 1
  • 2

修改翻译信息

#: app/templates/404.html:4
msgid "Page not found"
msgstr "找不到改页面"

#: app/templates/index.html:4
msgid "Welcome info"
msgstr "欢迎访问本系统"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

从项目的根目录编译翻译

# 编译所有翻译
(venv) D:\demo>flask fab babel-compile
Starting Compile target:app/translations

(venv) D:\demo>compiling catalog app/translations\zh\LC_MESSAGES\messages.po to app/translations\zh\LC_MESSAGES\messages.mo
  • 1
  • 2
  • 3
  • 4
  • 5

messages.mo 的内容
在这里插入图片描述
在config.py中做如下配置
在这里插入图片描述
跑起来看看:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/857245
推荐阅读
相关标签
  

闽ICP备14008679号