I believe to use jinja2 with bottle one simply uses jinja2_template instead of template: e.g. bottle.jinja2_template("mytemplate", dict(name=value, name2=value2)) However if one needs the i18n jinja extension how is that best specified to also do
....install_gettext_translations(
? Is that done automatically with
bottle.jinja2_template("mytemplate", dict(name=value, name2=value2), template_lookup=['templates'],'template_settings'= {'extensions':['jinja2.ext.i18n'],'autoescape': True }))
? Thanks.
Upon further reflection, I think I may need to overide the prepare method in class Jinja2Template to add the env.install_gettext_translations( ???
More info ,if I were doing ....install_gettext_translations( manually, perhaps:
tenv = Environment(extensions=['jinja2.ext.i18n'])
tenv.install_gettext_translations(gettransobj())
import gettext
import locale
def gettransobj():
loc = locale.getlocale()
# change to reflect where your mo files are
mofilename = "res/messages_%s.mo" % locale.getdefaultlocale()[0][0:2]
try:
trans = gettext.GNUTranslations(open( mofilename, "rb" ) )
except IOError:
trans = gettext.NullTranslations()
return trans
OR for babel translations obj something like
.....install_gettext_translations(gettransobj(),newstyle=True)
import babel
import locale
def gettransobj():
loc = locale.getlocale()
mofilename = "res/messages_%s.mo" % locale.getdefaultlocale()[0][0:2]
trans = babel.support.Translations(open( mofilename, "rb" ) )
If this code is somewhat correct, not sure where to put it? Not very familiar with jinja2. Just once at top of program or per bottle.jinja2_template call.
On a different note, if someone needs to do extraction using babel, see jinja2.ext.babel_extract
Another approach is getting trans obj with something like:
return gettext.translation(domain, localedir=localedir,languages=languages, codeset='utf-8')