我有一个烧瓶应用程序,我想向它添加多种语言。所以我在 Flask_babel 上使用这个演示来做到这一点。 烧瓶标签演示
配置文件
DEBUG = True
LANGUAGES = ['en', 'de', 'fr']
这是app.py
from flask import Flask, request, g
from flask_babel import Babel
from config import Config
# set up application
app = Flask(__name__)
app.config.from_object(Config)
# set up babel
babel = Babel(app)
@babel.localeselector
def get_locale():
if not g.get('lang_code', None):
g.lang_code = request.accept_languages.best_match(app.config['LANGUAGES'])
return g.lang_code
babel.cfg
[python: app/**.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape, jinja2.ext.with_
要开始这个过程,我首先使用了这个命令
pybabel extract -F babel.cfg -o messages.pot .
然后这个
pybabel init -i messages.pot -d app/translations -l de
它生成messages.po文件
# German translations for PROJECT.
# Copyright (C) 2021 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2021.
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2021-05-28 19:07+0530\n"
"PO-Revision-Date: 2021-05-28 19:08+0530\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: de\n"
"Language-Team: de <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\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/blueprints/multilingual/routes.py:38
msgid "Beautiful day in Portland!"
msgstr ""
#: app/blueprints/multilingual/routes.py:42
msgid "The Avengers movie was so cool!"
msgstr ""
#: app/blueprints/multilingual/routes.py:45 app/templates/base.html:15
msgid "Home"
msgstr ""
#: app/blueprints/multilingual/routes.py:51
msgid "The Cake is a Lie"
msgstr ""
#: app/blueprints/multilingual/templates/multilingual/cake.html:4
msgid "Hi, User!"
msgstr ""
#: app/blueprints/multilingual/templates/multilingual/cake.html:6
msgid "I promise you that there will be "
msgstr ""
#: app/blueprints/multilingual/templates/multilingual/cake.html:6
msgid "cake"
msgstr ""
#: app/blueprints/multilingual/templates/multilingual/cake.html:6
msgid " at the end of this article, so keep on reading!"
msgstr ""
#: app/blueprints/multilingual/templates/multilingual/index.html:4
msgid "Hi, "
msgstr ""
#: app/blueprints/multilingual/templates/multilingual/index.html:6
msgid " says: "
msgstr ""
#: app/templates/base.html:6
msgid "Welcome to Microblog"
msgstr ""
#: app/templates/base.html:16
msgid "Cake"
msgstr ""
我如何翻译每一个msgid。演示说要手动执行此操作,但这是一个演示,它仅包含 10 行,但我的项目将包含 10 种语言的数百个翻译,那么我该怎么做呢?如何通过库或程序将未翻译的 .po 文件转换为已翻译的 .po 文件?