LoginSignup
0
0

More than 5 years have passed since last update.

python2でi18n.lazy_gettext省略記法が使えないerror

Posted at

_('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

なるほど。

 結論

  • _にリスト入れるのがよくないね
  • 名前空間汚してしまったのが問題でした。_に入れるのはやめよう!!
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0