カテゴリリストがありますが、ロケールごとに言語を変更できるように、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を別の構成ファイルに分離する必要があることです。この問題を解決する方法はありますか?ありがとう!