[i18n] ValueError: plural forms expression could be dangerous

在做djangocms的过程中,执行i18n之后,django就不能正常工作了,报错

Traceback (most recent call last):
  File "manage-eclipse.py", line 33, in <module>
    execute_from_command_line(sys.argv)
  File "env\Lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "env\Lib\site-packages\django\core\management\__init__.py", line 341, in execute
    django.setup()
  File "env\Lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "env\Lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "env\Lib\site-packages\django\apps\config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "D:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "env\Lib\site-packages\django\contrib\auth\models.py", line 101, in <module>
    class Group(models.Model):
  File "env\Lib\site-packages\django\db\models\base.py", line 157, in __new__
    new_class.add_to_class(obj_name, obj)
  File "env\Lib\site-packages\django\db\models\base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "env\Lib\site-packages\django\db\models\fields\related.py", line 1535, in contribute_to_class
    self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
  File "env\Lib\site-packages\django\db\models\fields\related.py", line 1049, in create_many_to_many_intermediary_model
    'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to},
  File "env\Lib\site-packages\django\utils\functional.py", line 159, in __mod__
    return six.text_type(self) % rhs
  File "env\Lib\site-packages\django\utils\functional.py", line 116, in __text_cast
    return func(*self.__args, **self.__kw)
  File "env\Lib\site-packages\django\utils\translation\__init__.py", line 85, in ugettext
    return _trans.ugettext(message)
  File "env\Lib\site-packages\django\utils\translation\trans_real.py", line 343, in ugettext
    return do_translate(message, 'ugettext')
  File "env\Lib\site-packages\django\utils\translation\trans_real.py", line 320, in do_translate
    _default = _default or translation(settings.LANGUAGE_CODE)
  File "env\Lib\site-packages\django\utils\translation\trans_real.py", line 227, in translation
    _translations[language] = DjangoTranslation(language)
  File "env\Lib\site-packages\django\utils\translation\trans_real.py", line 129, in __init__
    self._add_installed_apps_translations()
  File "env\Lib\site-packages\django\utils\translation\trans_real.py", line 176, in _add_installed_apps_translations
    translation = self._new_gnu_trans(localedir)
  File "env\Lib\site-packages\django\utils\translation\trans_real.py", line 156, in _new_gnu_trans
    fallback=use_null_fallback)
  File "D:\Python27\lib\gettext.py", line 481, in translation
    t = _translations.setdefault(key, class_(fp))
  File "D:\Python27\lib\gettext.py", line 182, in __init__
    self._parse(fp)
  File "D:\Python27\lib\gettext.py", line 318, in _parse
    self.plural = c2py(plural)
  File "D:\Python27\lib\gettext.py", line 95, in c2py
    raise ValueError, 'plural forms expression could be dangerous'
ValueError: plural forms expression could be dangerous

目前我的项目有一点需解决的问题,就是中文仍然用的zh而不是最新的zh_hans,这个后面需要解决。 就目前这个情况,大概搜了一下,主要的解决方案就是将django.po文件里的一句话删除掉

"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

参考文档 https://stackoverflow.com/questions/8793513/plural-forms-expression-could-be-dangerous

涉及到的文件有

aldryn_bootstrap3\locale\zh\lc_messages
django\contrib\admin\locale\zh\lc_messages
django\contrib\auth\locale\zh\lc_messages
django\contrib\humanize\locale\zh\lc_messages
django\contrib\postgres\locale\zh\lc_messages
tracking\locale\zh\lc_messages

基本改好前三个就能工作,首先删除django.mo,执行python manage-eclipse.py compilemessages -l zh就可以了

评论