2
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),extensions=['jinja2.ext.i18n'], autoescape = True)
jinja_env.install_gettext_translations(i18n)

config['webapp2_extras.i18n'] = {
    'translations_path': 'locale',
    'template_path': 'views'
}

app = webapp2.WSGIApplication([
    ('/', MainController.MainPageHandler)
], config=config, debug=True)

In the messages.po file.

"Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" "POT-Creation-Date: 2013-01-19 19:26+0800\n" "PO-Revision-Date: 2013-01-19 19:13+0800\n" "Last-Translator: FULL NAME \n" "Language-Team: en_US \n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 0.9.6\n"

#~ msgid "Hello-World"

#~ msgstr "Hello World"

In the handler:

from webapp2_extras import i18n

from webapp2_extras.i18n import gettext as _

class MainPageHandler(Handler.Handler):
    def get(self):
        locale = self.request.GET.get('locale', 'en_US')
        i18n.get_i18n().set_locale(locale)
        logging.info(locale)
        message = _('Hello-World')
        logging.info(message)
        self.render("main.html")

In the html file:

<div id="main">

    {{ _("Hello-World") }}
</div>

When navigate to the webpage, it returns the string "Hello-World" instead of "Hello World". I don't know what's wrong. Anyone can help?

3 Answers 3

5

Couple of things that might be wrong, or might just be missing from the description...

the default 'domain' with webapp2 translation is 'messages', not 'message', so if your file is actually 'message.po' as you typed it, then that needs to change.

Secondly, the translation works off the compiled .mo file, not the .po, so if you haven't run the compile step (pybabel compile -f -d ./locale), you need to do that. You should have a file at locale/en_US/LC_MESSAGES/messages.mo

3
  • my bad, it is messages.po and i have the .mo file as well.
    – tipsywacky
    Jan 19, 2013 at 19:15
  • 1
    Not sure then...it looks like it should work. Is the output from your logging.info(message) showing the translation? Your render function is definitely using the same jinja env?
    – Greg
    Jan 19, 2013 at 19:22
  • Nope it's showing Hello-World. can't figure out what's wrong :(
    – tipsywacky
    Jan 19, 2013 at 19:34
3

Thanks, @tipsywacky, I was a little lost with jinja2, babel and GAE, and your code put me on the right path.

I want to share my code for other "stackers", in which you can appreciate a strange thing: don't know why, but I don't need to setup a config var to make all working.

import webapp2
import jinja2
import os
import logging
# Internacionalization functions
from webapp2_extras import i18n
from webapp2_extras.i18n import gettext as _
# Put here the path to the jinja templates, relative to the actual file
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "views"))),
        extensions=['jinja2.ext.i18n'],
        autoescape=False)
jinja_env.install_gettext_translations(i18n)


# This controller handles the generation of the front page.
class MainPage(webapp2.RequestHandler):
    def get(self):
        locale = self.request.get('locale', 'es_ES')
        i18n.get_i18n().set_locale(locale)
        logging.info(_('Hello-World'))
        template = jinja_environment.get_template('main.html')
        self.response.out.write(template.render())

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

With only this code and your HTML file (and the message.mo files compiled):

<div id="main">
    {{ _("Hello-World") }}
</div>

my app makes the translation of the logging text and of the HTML text.

That's wonderful, I spent the full day searching for a way to do localization and finally I got it.

The only thing I don't understand is your config var. Why I don't need it?

1
  • You might want to ask someone else. Though I don't think you need it. It was some codes that I got from an example.
    – tipsywacky
    May 25, 2013 at 11:08
2

Alright, figured out what's wrong.

In the messages.po file, I put down #: gettext_example.py:16 at the top of msgid "Hello-World". Then re-compile it, and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.