带有gettext的Django问题无法读取.mo文件

时间:2018-08-02 10:00:33

标签: django python-2.7

为了提供一些背景信息,我正在实习将Django应用程序从1.4.22升级到1.11,将python 2升级到3。

该项目的分解不充分,当我尝试对其进行重构时,我得到了一个奇怪的堆栈:

Traceback (most recent call last):
  File "/home/antonin/Developpement/Projet/djangoproject2/djangoproject/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/antonin/Developpement/Projet/djangoproject2/djangoproject/instance/models.py", line 54, in <module>
    YEARS += [(str(yr), _(u"n+%(year)d") % {'year': yr}) for yr in range(1, 20)]
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/functional.py", line 179, in __mod__
    return six.text_type(self) % rhs
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/functional.py", line 144, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 83, in ugettext
    return _trans.ugettext(message)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 325, in ugettext
    return do_translate(message, 'ugettext')
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 306, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 209, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 195, in _fetch
    res = _merge(apppath)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 177, in _merge
    t = _translation(path)
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 159, in _translation
    t = gettext_module.translation('django', path, [loc], DjangoTranslation)
  File "/usr/lib64/python2.7/gettext.py", line 554, in translation
    t = _translations.setdefault(key, class_(fp))
  File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 107, in __init__
    gettext_module.GNUTranslations.__init__(self, *args, **kw)
  File "/usr/lib64/python2.7/gettext.py", line 255, in __init__
    self._parse(fp)
  File "/usr/lib64/python2.7/gettext.py", line 348, in _parse
    magic = unpack('<I', buf[:4])[0]
struct.error: unpack requires a string argument of length 4

当我尝试使用逐步模式进行调试时,我发现问题出在

File "/home/antonin/Developpement/Projet/djangoproject2/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 107, in __init__
    gettext_module.GNUTranslations.__init__(self, *args, **kw)

其中fp.read()接收u'path / to / file.po'(正确的路径)并返回空缓冲区。但我看不出有什么原因。

编辑:谢谢,哈肯,确实是.mo文件损坏了。问题解决了。

0 个答案:

没有答案