我想为我的项目进行国际化。我按照官方文档中的描述进行了操作,但是本地化仍然不起作用。这是我尝试获取用户语言环境的方法:
def get_locale_name(request):
""" Return the :term:`locale name` associated with the current
request (possibly cached)."""
locale_name = getattr(request, 'locale_name', None)
if locale_name is None:
locale_name = negotiate_locale_name(request)
request.locale_name = locale_name
return locale_name
但是request
没有属性“local_name”,但它有“Accept-Language”,所以当函数get_local_name
在请求中找不到“local_name”时,它会调用另一个函数:
def negotiate_locale_name(request):
""" Negotiate and return the :term:`locale name` associated with
the current request (never cached)."""
try:
registry = request.registry
except AttributeError:
registry = get_current_registry()
negotiator = registry.queryUtility(ILocaleNegotiator,
default=default_locale_negotiator)
locale_name = negotiator(request)
if locale_name is None:
settings = registry.settings or {}
locale_name = settings.get('default_locale_name', 'en')
return locale_name
我怎么能看到negotiator
尝试从全局环境中获取本地,但如果它不能从配置中执行它的设置值。而且我不明白为什么 Pyramid 不能直接从请求的“Accept-Language”字段中获取语言环境?
而且,我怎样才能正确确定语言环境?