我有多種語言的網頁,設置國際化......是這樣的:金字塔,由
- /- >恩,我們,PT,ES,默認是美國
- /富 - > PT,EN和默認爲PT
- /酒吧 - > 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()
但不工作,僅在主它的正常工作
,如果我錯過任何信息,請提醒我。 感謝
請加上你的答案作爲一個實際的答案,並接受它。不要只是將它添加爲您的問題的一部分。 –