Question

Folks,

Parsing {% trans %}{{employee.title}}{% endtrans %} throws an error

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/john/src/employee/frontend/views.py", line 38, in get
    tpl = jinja2_env.get_template('index.html')
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2-2.6/jinja2/loaders.py", line 127, in load
    code = environment.compile(source, name, filename)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 493, in compile
    self.handle_exception(exc_info, source_hint=source)
  File "/Users/john/src/employee/frontend/templates/index.html", line 26, in template
    {% trans %}{{employee.title}}{% endtrans %}
TemplateSyntaxError: expected token 'end of print statement', got '.'

Parsing {% trans %}Software Engineer{% endtrans %} works well, any thoughts?

Update: Now it's not throwing an error, but it's not translating, this is the code snippet:

{% for employee in employees %} <li class="employee"> <div> {% trans title=employee.title %}{{title}}{% endtrans %} </div> </li> {% endfor %}

Was it helpful?

Solution 2

This has 2 prongs. If you need help internationalizing your webpage to get it ready to translate information it you can use rules like this:

If you have static information on the webpage use {% trans %} Title {% endtrans %}.

If you would rather, you can translate dynamic text before it is put in the template using the gettext library.

the commmon usage is something like this:

from webapp2_extras.i18n import gettext as _
params['title']=_("My Translated Title Method")
self.render_template('my.html',**params)

and render the text using the {{title}} syntax in your template. docs: https://docs.python.org/2/library/gettext.html

the 2nd prong is Localization and that has to do with running babel your text files you set up in order to translate the local language (english I'm assuming) to the foreign language. check out http://babel.edgewall.org/wiki/Documentation/0.9/intro.html for more.

OTHER TIPS

You need to bind the property to a variable to use it inside trans:

{% trans title=employee.title %}{{ title }}{% endtrans %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top