Internationalization with python gae, babel and i1

2019-04-13 00:32发布

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条回答
\"骚年 ilove
2楼-- · 2019-04-13 01:02

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楼-- · 2019-04-13 01:10

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?

查看更多
forever°为你锁心
4楼-- · 2019-04-13 01:12

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.

查看更多
登录 后发表回答