2014-04-25 86 views
3

我相信使用jinja2與瓶一個簡單地使用jinja2_template而不是模板: 例如, bottle.jinja2_template(「MyTemplate的」,字典(名稱=值,2 =值2)) 但是,如果一個需要國際化延伸神社是如何最規定也做如何使用jinja2 i18n與瓶(與巴貝爾)

....install_gettext_translations(

?是否自動完成

bottle.jinja2_template("mytemplate", dict(name=value, name2=value2), template_lookup=['templates'],'template_settings'= {'extensions':['jinja2.ext.i18n'],'autoescape': True })) 

?謝謝。

經過進一步反射,我想可能需要手動此改變在類Jinja2Template prepare方法來添加env.install_gettext_translations(???

更多信息,如果我在做.... install_gettext_translations(,也許是:

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爲巴貝爾翻譯OBJ像

..... 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")) 

如果這段代碼有點正確,不知道該把它放在哪裏?對jinja2不太熟悉。只需在程序頂端或bottle.jinja2_template調用一次。

在不同的音符,如果有人需要用巴貝爾做的提取,看到jinja2.ext.babel_extract

另一種方法就是設法讓反式OBJ的東西,如:

return gettext.translation(domain, localedir=localedir,languages=languages, codeset='utf-8') 

回答

2
from jinja2 import FileSystemBytecodeCache, Environment 
bcc = FileSystemBytecodeCache('/tmp', '%s.cache') 

template_settings = {'filters': { 
         'tojson': json_util.dumps 
        }, 
        'bytecode_cache': bcc, 
        'extensions': ['jinja2.ext.i18n'], 
        'languages': ['en_US'] 
        } 

import bottle 
from bottle import Jinja2Template 
from babel.support import Translations 

TEMPLATE_PATH = bottle.TEMPLATE_PATH 
DEBUG = bottle.DEBUG 
TEMPLATES = {} 

class Jinja2BabelTemplate(Jinja2Template): 
    def prepare(self, filters=None, tests=None, 
         languages=['en_US'], 
         globals={}, **kwargs): 
     from jinja2 import Environment, FunctionLoader 
     self.env = Environment(loader=FunctionLoader(self.loader), **kwargs) 
     #BABEL: this is where we load and install our translations from babel 
     translations = Translations.load('translations', languages) 
     self.env.install_gettext_translations(translations) 
     if filters: self.env.filters.update(filters) 
     if tests: self.env.tests.update(tests) 
     if globals: self.env.globals.update(globals) 
     if self.source: 
      self.tpl = self.env.from_string(self.source) 
     else: 
      self.tpl = self.env.get_template(self.filename) 


def jinja_template(*args, **kwargs): 
    tpl = args[0] if args else None 
    adapter = Jinja2BabelTemplate 
    lookup = kwargs.pop('template_lookup', TEMPLATE_PATH) 
    languages = kwargs.pop('languages', None) 
    tplid = (id(languages), tpl,) 
    if tplid not in TEMPLATES or DEBUG: 
     settings = kwargs.pop('template_settings', {}) 
     if languages: settings.update({'languages': languages})  
     if isinstance(tpl, adapter): 
      TEMPLATES[tplid] = tpl 
      if settings: TEMPLATES[tplid].prepare(**settings) 
     elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl: 
      TEMPLATES[tplid] = adapter(source=tpl, lookup=lookup, **settings) 
     else: 
      TEMPLATES[tplid] = adapter(name=tpl, lookup=lookup, **settings) 
    if not TEMPLATES[tplid]: 
     abort(500, 'Template (%s) not found' % tpl) 
    for dictarg in args[1:]: kwargs.update(dictarg) 
    return TEMPLATES[tplid].render(kwargs) 

template = functools.partial(jinja_template, 
          template_settings=template_settings) 


#and now you should be able to use the template function above with different languages 

@get('/home') 
def home(): 
    data = {} 
    return template('home.html', data, languages=['en_US']) 
+0

您需要在答案中指定你不知道的東西。粘貼代碼只是沒有任何意義。 – Runcorn

+0

謝謝兄弟!有效! – phedoreanu