如何根据cookies/headers/session在webapp2中决定语言?
我想利用webapp2的新功能来实现本地化,这样可以根据不同地区的习惯来格式化时间和货币。
Django有一个很好的功能叫做get_language_from_request,我在完全转向webapp2之前就用过。现在我用webapp2的i18n功能,能够在我用gettext写的本地化内容之间切换,并把它们编译成名为messages.mo的文件,供我的应用读取和显示。我已经找到了几种获取用户语言的优先方法:
1. HTTP GET请求,比如hl=pt-br表示巴西葡萄牙语
2. HTTP SESSION变量,我称之为i18n_language
3. Cookie,我应该设置和获取,但我不太清楚具体怎么做
4. HTTP Header,我也不太清楚怎么获取,我在研究Django是怎么做的,Django有一个方便的get_language_from_request
功能,我以前用过,但现在我不再导入Django了,我希望在我的webapp2代码中也能实现这个功能。
def get_language_from_request(self, request):
"""
Analyzes the request to find what language the user wants the system to
show. If the user requests a sublanguage where we have a main language, we send
out the main language.
"""
if self.request.get('hl'):
self.session['i18n_language'] = self.request.get('hl')
return self.request.get('hl')
if self.session:
lang_code = self.session.get('i18n_language', None)
if lang_code:
logging.info('language found in session')
return lang_code
lang_code = Cookies(self).get(LANGUAGE_COOKIE_NAME)
if lang_code:
logging.info('language found in cookies')
return lang_code
accept = os.environ.get('HTTP_ACCEPT_LANGUAGE', '')
for accept_lang, unused in self.parse_accept_lang_header(accept):
logging.info('accept_lang:'+accept_lang)
lang_code = accept_lang
return lang_code
我看到Django的代码是可以用的,但我不太清楚webapp2的i18n功能能做多少,比如我是否需要处理语言的回退问题,比如如果没有pt-br的本地化文件,pt-br应该回退到pt,其他方言也是类似的情况。
实际上设置语言我可以用
i18n.get_i18n().set_locale(language)
我希望你能帮我确定获取用户语言的不同方法的优先级,我也想知道你对实现的想法。你觉得我只用会话变量就可以,不需要那么全面的解决方案吗?因为我主要是针对某个地区的使用,目前我只用巴西葡萄牙语和英语的翻译,但我希望能为西班牙语、俄语和其他语言做好准备,因此我希望能够切换到用户的语言,至少能把它保存到webapp2的会话中。我也想知道你对使用cookie和header获取用户语言的看法。
我之前用的原始代码是来自Django,像这样,但我现在不能再用了,因为它锁定在Django的mo文件上,特定于Django。
def get_language_from_request(request):
"""
Analyzes the request to find what language the user wants the system to
show. Only languages listed in settings.LANGUAGES are taken into account.
If the user requests a sublanguage where we have a main language, we send
out the main language.
"""
global _accepted
from django.conf import settings
globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale')
supported = dict(settings.LANGUAGES)
if hasattr(request, 'session'):
lang_code = request.session.get('django_language', None)
if lang_code in supported and lang_code is not None and check_for_language(lang_code):
return lang_code
lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
if lang_code and lang_code not in supported:
lang_code = lang_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr
if lang_code and lang_code in supported and check_for_language(lang_code):
return lang_code
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
for accept_lang, unused in parse_accept_lang_header(accept):
if accept_lang == '*':
break
# We have a very restricted form for our language files (no encoding
# specifier, since they all must be UTF-8 and only one possible
# language each time. So we avoid the overhead of gettext.find() and
# work out the MO file manually.
# 'normalized' is the root name of the locale in POSIX format (which is
# the format used for the directories holding the MO files).
normalized = locale.locale_alias.get(to_locale(accept_lang, True))
if not normalized:
continue
# Remove the default encoding from locale_alias.
normalized = normalized.split('.')[0]
if normalized in _accepted:
# We've seen this locale before and have an MO file for it, so no
# need to check again.
return _accepted[normalized]
for lang, dirname in ((accept_lang, normalized),
(accept_lang.split('-')[0], normalized.split('_')[0])):
if lang.lower() not in supported:
continue
langfile = os.path.join(globalpath, dirname, 'LC_MESSAGES',
'django.mo')
if os.path.exists(langfile):
_accepted[normalized] = lang
return lang
return settings.LANGUAGE_CODE
这样做每次请求都可以吗?我觉得我还应该把语言设置到header中self.response.headers['Content-Language'] = language
根据我的理解,如果我选择使用HTTP头,我可以直接从Django中提取一些功能,但我不太明白它的作用,所以也许你可以为我解释一下Django的这段代码:
def parse_accept_lang_header(lang_string):
"""
Parses the lang_string, which is the body of an HTTP Accept-Language
header, and returns a list of (lang, q-value), ordered by 'q' values.
Any format errors in lang_string results in an empty list being returned.
"""
result = []
pieces = accept_language_re.split(lang_string)
if pieces[-1]:
return []
for i in range(0, len(pieces) - 1, 3):
first, lang, priority = pieces[i : i + 3]
if first:
return []
priority = priority and float(priority) or 1.0
result.append((lang, priority))
result.sort(lambda x, y: -cmp(x[1], y[1]))
return result
谢谢你
更新
我发现我不能在请求处理程序的初始化函数中使用会话,可能是因为会话对象还没有创建。所以我把从会话中获取语言的代码放在BaseHandler的render函数中,似乎可以正常工作。考虑一下header或cookie的值也是不错的。
2 个回答
这段内容主要是讲一个程序是如何处理语言设置的,下面是我做的改进和设计变化:
"""
Use this handler instead of webapp2.RequestHandler to support localization.
Fill the AVAILABLE_LOCALES tuple with the acceptable locales.
"""
__author__ = 'Cristian Perez <http://cpr.name>'
import webapp2
from webapp2_extras import i18n
AVAILABLE_LOCALES = ('en_US', 'es_ES', 'en', 'es')
class LocalizedHandler(webapp2.RequestHandler):
def set_locale_from_param(self):
locale = self.request.get('locale')
if locale in AVAILABLE_LOCALES:
i18n.get_i18n().set_locale(locale)
# Save locale to cookie for future use
self.save_locale_to_cookie(locale)
return True
return False
def set_locale_from_cookie(self):
locale = self.request.cookies.get('locale')
if locale in AVAILABLE_LOCALES:
i18n.get_i18n().set_locale(locale)
return True
return False
def set_locale_from_header(self):
locale_header = self.request.headers.get('Accept-Language') # e.g. 'es,en-US;q=0.8,en;q=0.6'
if locale_header:
locale_header = locale_header.replace(' ', '')
# Extract all locales and their preference (q)
locales = [] # e.g. [('es', 1.0), ('en-US', 0.8), ('en', 0.6)]
for locale_str in locale_header.split(','):
locale_parts = locale_str.split(';q=')
locale = locale_parts[0]
if len(locale_parts) > 1:
locale_q = float(locale_parts[1])
else:
locale_q = 1.0
locales.append((locale, locale_q))
# Sort locales according to preference
locales.sort(key=lambda locale_tuple: locale_tuple[1], reverse=True)
# Find first exact match
for locale in locales:
for available_locale in AVAILABLE_LOCALES:
if locale[0].replace('-', '_').lower() == available_locale.lower():
i18n.get_i18n().set_locale(available_locale)
return True
# Find first language match (prefix e.g. 'en' for 'en-GB')
for locale in locales:
for available_locale in AVAILABLE_LOCALES:
if locale[0].split('-')[0].lower() == available_locale.lower():
i18n.get_i18n().set_locale(available_locale)
return True
# There was no match
return False
def set_locale_default(self):
i18n.get_i18n().set_locale(AVAILABLE_LOCALES[0])
def save_locale_to_cookie(self, locale):
self.response.set_cookie('locale', locale)
def __init__(self, request, response):
"""
Override __init__ in order to set the locale
Based on: http://stackoverflow.com/a/8522855/423171
"""
# Must call self.initialze when overriding __init__
# http://webapp-improved.appspot.com/guide/handlers.html#overriding-init
self.initialize(request, response)
# First, try to set locale from GET parameter (will save it to cookie)
if not self.set_locale_from_param():
# Second, try to set locale from cookie
if not self.set_locale_from_cookie():
# Third, try to set locale from Accept-Language header
if not self.set_locale_from_header():
# Fourth, set locale to first available option
self.set_locale_default()
首先,它会检查网址里有没有
locale
这个参数。如果有的话,它会设置一个cookie来保存这个语言设置,以便以后使用。这样,你可以在任何地方通过这个locale
参数来改变语言,但在后续的请求中就不需要再带这个参数了。如果网址里没有这个参数,它会检查是否有
locale
的cookie。如果连cookie都没有,它会查看
Accept-Language
这个请求头。这里面有个很重要的点,就是它会考虑到这个请求头里的q
优先级因子,并且还会做一些小处理:它会接受语言前缀。例如,如果浏览器指定的是en-GB
,但在AVAILABLE_LOCALES
这个列表里找不到它,就会选择en
,如果en
存在的话。这样的话,默认情况下会用en_US
,如果en
的其他设置不存在的话。它还会处理大小写和格式问题(用-
或_
作为分隔符)。
我做的事情是这样的——我有一个基础的请求处理器,所有的请求处理器都从这个基础处理器继承。在这里,我定义了一个常量,里面包含了可用的语言。然后我重写了初始化方法,在每次请求时设置语言:
import webapp2
from webapp2_extras import i18n
AVAILABLE_LOCALES = ['en_GB', 'es_ES']
class BaseHandler(webapp2.RequestHandler):
def __init__(self, request, response):
""" Override the initialiser in order to set the language.
"""
self.initialize(request, response)
# first, try and set locale from cookie
locale = request.cookies.get('locale')
if locale in AVAILABLE_LOCALES:
i18n.get_i18n().set_locale(locale)
else:
# if that failed, try and set locale from accept language header
header = request.headers.get('Accept-Language', '') # e.g. en-gb,en;q=0.8,es-es;q=0.5,eu;q=0.3
locales = [locale.split(';')[0] for locale in header.split(',')]
for locale in locales:
if locale in AVAILABLE_LOCALES:
i18n.get_i18n().set_locale(locale)
break
else:
# if still no locale set, use the first available one
i18n.get_i18n().set_locale(AVAILABLE_LOCALES[0])
首先,我会检查浏览器的cookie,然后检查请求头,最后如果没有找到有效的语言,就默认使用第一个可用的语言。
为了设置cookie,我有一个单独的控制器,大致长这样:
import base
class Index(base.BaseHandler):
""" Set the language cookie (if locale is valid), then redirect back to referrer
"""
def get(self, locale):
if locale in self.available_locales:
self.response.set_cookie('locale', locale, max_age = 15724800) # 26 weeks' worth of seconds
# redirect to referrer or root
url = self.request.headers.get('Referer', '/')
self.redirect(url)
所以像www.example.com/locale/en_GB这样的URL会把语言设置为en_GB,同时设置cookie并返回到之前的页面(这样做的好处是可以在任何页面切换语言,并且保持在同一页面上)。
这种方法没有考虑请求头中局部匹配的情况,比如“en”而不是“en_GB”,不过因为我在应用中启用的语言列表是固定的(而且语言切换的URL在页面底部是硬编码的),所以我对此并不太担心。
希望这对你有帮助。