#!/usr/bin/env python# -*- coding:utf-8 -*-# for webapp2 to import babel and pytzimportsysimportossys.path.insert(0,os.path.join(os.path.dirname(__file__),'babel.zip'))sys.path.insert(0,os.path.join(os.path.dirname(__file__),'pytz.zip'))importwebapp2importjinja2fromwebapp2_extrasimporti18nJINJA_ENVIRONMENT=jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),extensions=['jinja2.ext.i18n','jinja2.ext.autoescape'],autoescape=True)JINJA_ENVIRONMENT.install_gettext_translations(i18n)classMainPage(webapp2.RequestHandler):defget(self):locale=self.request.GET.get('locale','en_US')i18n.get_i18n().set_locale(locale)template_values={}template=JINJA_ENVIRONMENT.get_template('index.html')self.response.write(template.render(template_values))app=webapp2.WSGIApplication([('/',MainPage),],debug=True)
index.html (HTML template): Wrap the string you want to tranlsate in
{{ _("string") }}. See [18] for more syntax you can use.
<!doctype html><html><head><metacharset="utf-8"><title>i18n of GAE Webapp</title></head><body>{{_("home")}}<br/>{{_("about")}}<br/>{{_("'link'")|safe}}<br/></body></html>
babel.cfg (config for Babel extraction): This babel.cfg tells babel to
extract all translations from all HTML files in your webapp and the encoding of
HTML files are utf-8. For more information of the config file, refer to [16]
and [17].
By default, webapp2 looks for pot and po files in locale directory under
your project root directory, so first create a directory named locale:
# in your project root directory:
$mkdirlocale
Then extract all translations (create pot file).
# in your project root directory:
$pybabelextract-F./babel.cfg-o./locale/messages.pot./
The pot file looks like:
# Translations template for PROJECT.
# Copyright (C) 2015 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2015.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2015-04-12 03:32+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 1.3\n"
#: index.html:8
msgid "home"
msgstr ""
#: index.html:10
msgid "about"
msgstr ""
#: index.html:12
msgid "'link'"
msgstr ""
Then initialize the directory for each locale that your webapp will support.
en_US and zh_TW are supported in our example. See [19] for table of
locales.
# in your project root directory:
$pybabelinit-len_US-d./locale-i./locale/messages.pot
$pybabelinit-lzh_TW-d./locale-i./locale/messages.pot
Two po files (locale/en_US/LC_MESSAGES/messages.po and
locale/zh_TW/LC_MESSAGES/messages.po) are created. You do not need to do
anything with the en_US po file because English is default language.
Translate only non-default-language po files. In our exmaple, the zh_TWpo
file after translation looks like:
# Chinese (Traditional, Taiwan) translations for PROJECT.
# Copyright (C) 2015 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2015.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2015-04-12 03:32+0800\n"
"PO-Revision-Date: 2015-04-12 03:35+0800\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: zh_Hant_TW <LL@li.org>\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 1.3\n"
#: index.html:8
msgid "home"
msgstr "首頁"
#: index.html:10
msgid "about"
msgstr "關於"
#: index.html:12
msgid "'link'"
msgstr "'連結'"
After all translations done, compile po file with the following command:
# in your project root directory:
$pybabelcompile-f-d./locale
Now we can run this GAE Python webapp, and then open the browser with URL: