_('hogehoge')でTypeErrorが出た
GAE/pythonで国際化対応のためi18nを入れようとしたのだが動かなかった。
GAEはpython2なのである。。。。。という問題
from webapp2_extras.i18n import lazy_gettext as _
i18n.get_i18n().set_locale('en_US')
message = _("hogehoge")
さて、これを動かしたところ、
TypeError: 'list' object is not callable
リスト??
文字列がリスト評価されてしまっている可能性も考えて
message = _("a")
としたが、変わらず、
print type(_)
返ってきた型はこれ
<type 'list'>
そういえば、コードの中で、_にリスト代入していたかもしれない
_abc = [i for i, _ in enumerate(abc)]
みたいな。
python2
Python 2.7.10
>>> abc = ["a", "b", "c"]
>>> _abc = [i for i, _ in enumerate(abc)]
>>> type(_)
<type 'str'>
python3
Python 3.6.2
>>> abc = ["a", "b", "c"]
>>> _abc = [i for i, _ in enumerate(abc)]
>>> type(_)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
なるほど。
結論
- _にリスト入れるのがよくないね
- 名前空間汚してしまったのが問題でした。_に入れるのはやめよう!!