我有多种语言的页面......像这样:
- / - > 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()
但这不起作用,只有家它工作正常
如果我缺少任何信息,请警告我。谢谢