金字塔,按页面设置i18n

时间:2013-03-30 16:13:12

标签: python internationalization pyramid

我有多种语言的网页......就像这样:

  • / - > en,us,pt,es,默认为US
  • / foo - > pt,en和默认值为PT
  • / bar - > pt,默认为PT

在数据库上我有:

    |--URL--|-Languages-|-defLang- 
    |  foo  |   pt,us   |    pt  
    |  bar  |    pt     |    pt  
__init__.py上的

我设置如下:

tsf = TranslationStringFactory('myproject')


def add_renderer_globals(event):
    request = event['request']
    event['_'] = request.translate
    event['localizer'] = request.localizer


def add_localizer(event):
    request = event.request
    languages = ('en','us','es')
    deflang = 'pt'
    # CHECK current page
    if 'page' in request.matchdict:
        currentpage = DBSession.query(Pages).filter_by(url=request.matchdict['page']).first()
        #SET pages languages
        languages = currentpage.Languages.split(',')
        deflang = currentpage.defLang
    if not default_locale_negotiator(request):
        #set language
        request._LOCALE_ = request.accept_language.best_match(languages , deflang)
    localizer = get_localizer(request)
    request.lang = localizer.locale_name
    def auto_translate(string):
        return localizer.translate(self.tsf(string))
    request.localizer = localizer
    request.translate = auto_translate



def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    session_factory = session_factory_from_settings(settings)
    config = Configurator(settings=settings)
    config.include('pyramid_mailer')
    config.set_session_factory(session_factory)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_subscriber(add_renderer_globals, BeforeRender)
    config.add_subscriber(add_localizer, ContextFound)
    config.add_translation_dirs('myproject:locale/')
    config.add_route('home', '/')
    config.add_route('page', '/{page}')

    config.scan()
    return config.make_wsgi_app()

但这不起作用,只有家里工作正常

如果我错过任何信息,请警告我。 感谢

2 个答案:

答案 0 :(得分:1)

我认为您在[{1}}中搜索'entity',实际上您应该根据路线request.matchdict搜索'page'

答案 1 :(得分:1)

def add_localizer(event):
    request = event.request
    languages = ('en','us','es')
    deflang = 'pt'
    # CHECK current page
    if 'page' in request.matchdict:
        currentpage = DBSession.query(Pages).filter_by(url=request.matchdict['page']).first()
        #SET pages languages
        languages = currentpage.Languages.split(',')
        deflang = currentpage.defLang
    #-----  CHANGED   ----------------------
    dln = default_locale_negotiator(request)
    if not dln or dln not in languages:
        #set the default language 
        request._LOCALE_ = deflang
    localizer = get_localizer(request)
    request.lang = localizer.locale_name
    def auto_translate(string):
        return localizer.translate(self.tsf(string))
    request.localizer = localizer
    request.translate = auto_translate

感谢