首页 > 为什么 flask-babel 可以从 .py 文件正常导出待翻译字段,但无法从 HTML 文件中导出?

为什么 flask-babel 可以从 .py 文件正常导出待翻译字段,但无法从 HTML 文件中导出?

Hi,大家好。我在学习 flask 的时候遇到这样一个问题,我可以使用 python-flask 从 views.py 中导出需要翻译的字段,但是不能从 .html 文件中导出,具体:

我的 bebel.cfg 文件配置:

[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_

一个 HTML 的内容:

...
    <div class="container-fluid">
        <div>Microblog:
            <a href="{{ url_for('index') }}">{{ gettext('Home') }}</a>
            {% if g.user.is_authenticated %}
                | <a href="{{ url_for('user', nickname=g.user.nickname) }}">{{ gettext('Your Profile') }}</a>
                | <form style="display: inline;" action="{{ url_for('search') }}" method="post" name="search">
                    {{ g.search_form.hidden_tag() }}
                    {{ g.search_form.search(size=20) }}
                    <input type="submit" value="Search">
                </form>
                | <a href="{{ url_for('log_out') }}">{{ gettext('Logout') }}</a>
            {% endif %}
        </div>
        <hr>
...

views.py 中的部分内容如下:

@app.route('/user/<nickname>')
@app.route('/user/<nickname>/<int:page>')
@login_required
def user(nickname, page=1):
    user = User.query.filter_by(nickname=nickname).first()
    if user == None:
        flash(gettext('User %s not found.' % nickname))
        return redirect(url_for('index'))

    posts = user.posts.paginate(page, POSTS_PER_PAGE, False)

    return render_template('user.html',
                           user=user,
                           posts=posts)

最后得到的 .po 文件中有 User %s not found,但是 HTML 中的 gettext 一个都没有导出。

更新 .po 文件的脚本 tr_update.py 如下:

import os
import sys

pybabel = 'flask/bin/pybabel'
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot app')
os.system(pybabel + ' update -i messages.pot -d app/translations')
os.unlink('messages.pot')

这是什么原因导致的?需要更多信息请评论。

【热门文章】
【热门文章】