我有一个类别列表,我想将它与 i18n 一起使用,以便我可以按区域设置更改语言。
我的python代码如下:
from webapp2_extras import i18n
category_list = {}
category_list['bikes'] = {'value': i18n.gettext('CATEGORY_BIKES')}
class CategoriesHandler(BaseHandler):
"""List categories"""
def get(self, **kwargs):
"""List all categories"""
self.response.write(self.json_output(category_list))
它会导致错误:
File "/Users/user/Developer/GAE/project/CategoriesHandler.py", line 11, in <module>
category_list['bikes'] = {'value': i18n.gettext('CATEGORY_BIKES')}
File "/Users/user/Developer/GAE/project/webapp2_extras/i18n.py", line 713, in gettext
return get_i18n().gettext(string, **variables)
File "/Users/user/Developer/GAE/project/webapp2_extras/i18n.py", line 894, in get_i18n
request = request or webapp2.get_request()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1720, in get_request
assert getattr(_local, 'request', None) is not None, _get_request_error
AssertionError: Request global variable is not set.
但是,如果我将 category_list 移动到类 get 方法中,一切都很好。
class CategoriesHandler(BaseHandler):
"""List categories"""
def get(self, **kwargs):
"""List all categories"""
category_list = {}
category_list['bikes'] = {'value': i18n.gettext('CATEGORY_BIKES')}
self.response.write(self.json_output(category_list))
pass
问题是我需要将 category_list 分离到另一个配置文件中,以便我可以轻松地维护我的代码。有没有办法解决这个问题?谢谢!