初始化文件
from flask import Flask
from flask_babel import Babel
app = Flask(__name__)
app.config['BABEL_DEFAULT_LOCALE'] = 'es'
babel = Babel(app)
from flask import Flask
from flask_babel import Babel
app = Flask(__name__)
app.config['BABEL_DEFAULT_LOCALE'] = 'es'
babel = Babel(app)
@babel.localeselector
def get_locale():
return 'es'
if __name__ == '__main__':
app.run()
用于生成翻译后文本的命令
pybabel extract -F babel.cfg -o messages.pot .
pybabel init -i messages.pot -d translations -l es
compile -d translations
html模板示例index.html
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}{{ _("Gallery") }}{% endblock %}</h1>
{% if 'user_id' in session %}
<h1>{{ _("hello and welcome") }}</h1>
{% endif %}
{% endblock %}
(...)
目录:
appfolder
|_____init.py
translations
|_____es
|_____LC_MESSAGES
|_________messages.mo
|_________messages.po
templates
|_____index.html</code>
我只是想翻译模板上的文本,但我尝试了所有操作,但我不明白它不会如何翻译,当我生成.pot文件时,它将重新整理所有翻译并将它们构建在.po文件上并填充翻译内容,将其编译为.mo,然后运行该网站以es语言显示,并且仅显示原始文本。
P.S:i catched the variables beign used by babel if this has some help