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)
在 messages.po 文件中。
"Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" "POT-Creation-Date: 2013-01-19 19:26+0800\n" "PO-修订日期:2013-01-19 19:13+0800\n" "最后译者:全名 \n" "语言团队:en_US \n" "复数形式:nplurals=2;复数=(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"
#~ msgstr "Hello-World"
#~ msgstr "Hello World"
在处理程序中:
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")
在 html 文件中:
<div id="main">
{{ _("Hello-World") }}
</div>
当导航到网页时,它返回字符串“Hello-World”而不是“Hello World”。我不知道怎么了。任何人都可以帮忙吗?