import gettext en_trans = gettext.install('messages', locale='i18n_path', languages=['default_language']) zh_trans = gettext.install('messages', locale'i18n_path', languages=['zh_CH']) print(_('hello world')) zh_trans.install() print(_('hello world'))
def render_string(self, template_name, **kwargs): # If no template_path is specified, use the path of the calling file ... ... args = dict( handler=self, request=self.request, current_user=self.current_user, locale=self.locale, _=self.locale.translate, static_url=self.static_url, xsrf_form_html=self.xsrf_form_html, reverse_url=self.application.reverse_url ) args.update(self.ui) args.update(kwargs) return t.generate(**args)
<html> <head> <title>${_('title')}</title> </head> <body> <a href=''>${_('body')}</a> </body> </html>
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR ORGANIZATION # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\\n" "POT-Creation-Date: %(time)s\\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" "Language-Team: LANGUAGE <[email protected]>\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=CHARSET\\n" "Content-Transfer-Encoding: ENCODING\\n" "Generated-By: pygettext.py %(version)s\\n" #: demo.html:3 msgid "title" msgstr "" #: demo.html:6 msgid "body" msgstr ""
>>> import gettext >>> en_trans = gettext.translation('messages', 'e:/python/locale', languages=['en_US']) >>> zh_trans = gettext.translation('messages', 'e:/python/locale', languages=['zh_CN']) >>> en_trans.gettext('title'), en_trans.gettext('body') ('title', 'body') >>> zh_trans.gettext('title'), zh_trans.gettext('body') ('HTML Head 头', 'HTML Body 测试') >>> en_trans.install() >>> _('title'), _('body'), _('hello') ('title', 'body', 'hello') >>> zh_trans.install() >>> _('title'), _('body'), _('hello') ('HTML Head 头', 'HTML Body 测试', 'hello')
上例使用的python解析器是python3.2.2 2.*版本有些method参数稍有不同,请注意!