-1

I am using flask_babel for localization of a python project. When extracting translatable strings from templates, which is put in {{ _('') }} , i am getting the following error. If anyone of you can solve it, please share the solution here. thanks in advance.

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

#MAIN APP FILE

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')

TEMPLATE FILE (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 %}
lungsang
  • 133
  • 13
  • i have updated the main app file.. please have a look – lungsang Feb 08 '22 at 10:39
  • Welcome to StackOverflow, it is helpful for the community if you provide some more details of the attempts you've made so far. Please review the guidelines here: https://stackoverflow.com/help/how-to-ask – William Baker Morrison Feb 16 '22 at 17:10

2 Answers2

0

After applying the following codes in init.py module, its working fine. Thank you

babel = Babel(app)
@babel.localeselector
def get_locale():
    return 'es'
lungsang
  • 133
  • 13
-1

remove the "_" in the h1 tag in the template's file

Henry
  • 1