繁体   English   中英

jinja2.exceptions.UndefinedError: '_' 未定义

[英]jinja2.exceptions.UndefinedError: '_' is undefined

我正在使用 flask_babel 对 python 项目进行本地化。 从 {{ _('') }} 中的模板中提取可翻译字符串时,出现以下错误。 如果你们中的任何人都可以解决它,请在这里分享解决方案。 提前致谢。

Traceback (most recent call last):
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/app.py", line 2069, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/app.py", line 2054, in wsgi_app
    response = self.handle_exception(e)
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/app.py", line 2051, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/app.py", line 1501, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/app.py", line 1485, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/mnt/c/Users/Lungsang/Desktop/new-pyrrha/pyrrha/app/main/views/index.py", line 34, in index
    return render_template_with_nav_info('main/index.html')
  File "/mnt/c/Users/Lungsang/Desktop/new-pyrrha/pyrrha/app/main/views/utils.py", line 60, in render_template_with_nav_info
    return render_template(template, **kwargs)
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/templating.py", line 147, in render_template
    return _render(
  File "/home/lungsang/.local/lib/python3.8/site-packages/flask/templating.py", line 128, in _render
    rv = template.render(context)
  File "/home/lungsang/.local/lib/python3.8/site-packages/jinja2/environment.py", line 1289, in render
    self.environment.handle_exception()
  File "/home/lungsang/.local/lib/python3.8/site-packages/jinja2/environment.py", line 924, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/mnt/c/Users/Lungsang/Desktop/new-pyrrha/pyrrha/app/templates/main/index.html", line 1, in top-level template code
    {% extends 'layouts/base.html' %}
  File "/mnt/c/Users/Lungsang/Desktop/new-pyrrha/pyrrha/app/templates/layouts/base.html", line 36, in top-level template code
    {% block content %}{% endblock %}
  File "/mnt/c/Users/Lungsang/Desktop/new-pyrrha/pyrrha/app/templates/main/index.html", line 5, in block 'content'
    <h1>{{ _('Welcome to Pyrrha !') }}</h1>
  File "/home/lungsang/.local/lib/python3.8/site-packages/jinja2/utils.py", line 81, in from_obj
    if hasattr(obj, "jinja_pass_arg"):
jinja2.exceptions.UndefinedError: '_' is undefined

#主应用程序文件

from flask_login import current_user 
from flask import request , Flask 
from flask_babel import Babel 

from .utils import render_template_with_nav_info 
from .. import main 
from ...models import Corpus 
from ...utils.pagination import int_or 



app = Flask(__name__)

app.config['BABEL_DEFAULT_LOCALE'] = 'en'
babel = Babel(app)

@babel.localeselector 
def get_locale(): 
    #return request.accept_languages.best_match(['es', 'fr', 'en']) 
    return 'bo_CN' 

@main.route('/')
def index():
   
    if current_user.is_authenticated and not 
       current_user.is_anonymous:
        corpora = Corpus.for_user(current_user, _all=False).paginate(
            page=int_or(request.args.get("page"), 1),
            per_page=20  # ToDo: Should it be hardcoded ?
        )
        return render_template_with_nav_info("main/index_loggedin.html", corpora=corpora)

    return render_template_with_nav_info('main/index.html')

模板文件 (index.html)

{% extends 'layouts/base.html' %}

{% block content %}
    <div class="ui text container">
      <h1>{{ _('Welcome to Pyrrha !') }}</h1>
      <p>Pyrrha is a simple Python Flask WebApp to fasten the post-correction of lemmatized and morpho-syntactic tagged corpora.</p>
      <p>It is under active development at the <a href="http://www.chartes.psl.eu">École Nationale des Chartes</a> and can
      be found on <a href="https://github.com/hipster-philology/pyrrha">github</a>.</p>
        <p>To cite it in a paper, please use the information available on <a href="https://doi.org/10.5281/zenodo.2325427">Zenodo</a></p>
    </div>
{% endblock %}

init .py 模块中应用以下代码后,它工作正常。 谢谢

babel = Babel(app)
@babel.localeselector
def get_locale():
    return 'es'

删除模板文件中 h1 标签中的“_”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM