1

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

  • / - > 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()

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

如果我缺少任何信息,请警告我。谢谢

4

2 回答 2

1

在我看来,你正在寻找实际上你应该寻找的时候'entity',给定你的路线。request.matchdict'page''/{page}'

于 2013-03-30T17:02:54.753 回答
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

谢谢

于 2013-03-30T18:41:33.753 回答