I try to enable the trans
tag and I've made a test template i18n.html:
{% trans %}For sale{% endtrans %}--{{message}}--{{decimal_format}}
Here is my python code according to the manpages:
from webapp2_extras import i18n as multilingua
import jinja2
from webapp2_extras.i18n import lazy_gettext as gettext
from webapp2_extras.i18n import lazy_gettext as _
from jinja2 import Environment, FileSystemLoader
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.i18n'])
# The code below seems wrong since it is django but it was the only way I could make the page load
jinja_environment.install_gettext_translations(django.utils.translation)
class HelloWorldHandler(webapp2.RequestHandler):
def get(self):
# Set the requested locale.
locale = self.request.GET.get('locale', 'pt')
multilingua.get_i18n().set_locale(locale)
message = multilingua.gettext('For sale')
#django.utils.translation.activate('pt')
template = jinja_environment.get_template('templates/i18n.html')
decimal_format = multilingua.I18n(self.request).format_decimal(1000)
self.response.out.write(template.render(message=message, decimal_format=decimal_format))
I could not make it work without django and therefore I ask how to lose the django translation and staying with webapp2.i18n + jinja instead.
There was also a discussion in a thread where I'm not the only one saying that documentation is somewhat incomplete or hard to find. Could you please answer or comment which is the recommended way of making the trans tag work and why I must include jinja_environment.install_gettext_translations(django.utils.translation)
?
When I try to remove my use of django I also lose the functions of webapp2.i18n. My locale files are both in locale/... and conf/locale.. since the first is the default for webapp2 and the second is the default for django translations, so I could really use some guidelines for best practice here to get rid of the django dependecies and use webapp2 and jinja for rendering my localizations.
If to any help, I did receive an error message when trying to remove django:
self.response.out.write(template.render(message=message, decimal_format=decimal_format))
File "/media/Lexar/montao/montaoproject/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/media/Lexar/montao/montaoproject/templates/i18n.html", line 2, in top-level template code
{{ _('For sale') }}--{{message}}--{{decimal_format}}
UndefinedError: 'gettext' is undefined
Thank you
Import name: jinja2.ext.i18n. The i18n extension can be used in combination with gettext or Babel. When it's enabled, Jinja provides a trans statement that marks a block as translatable and calls gettext .
Jinja2 is a full-featured template engine for Python. It has full unicode support, an optional integrated sandboxed execution environment, widely used and BSD licensed.
Take a look at Jinja2's i18n Extension documentation. Calling install_gettext_translations
basically sets the object through which Jinja2 will call gettext, ngettext, etc, in order to translate strings when it encounters a {% trans %}
tag.
Since those functions are defined on webapp2.i18n
(see here), jinja2 will successfully call those functions to retrieve translations, dependent upon your call to set_locale
inside of the request. I don't have the code in front of me, but I'd guess that gettext
and company defined in webapp2.i18n
are merely proxies to call webapp.i18.get_i18n().gettext
, which is the magic that makes all of this work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With