Django 国际化---compilemessages 错误:AttributeError: 模块'locale' 没有属性'normalize'

Coh*_*hen 2 python django locale internationalization

我就在终点线之前,感觉我还没有完成!我创建并编译了所有消息,以便拥有一个具有 2 种语言的站点,并且在运行服务器时收到此错误:AttributeError: module 'locale' has no attribute 'normalize'。

有人可以帮帮我吗?

Traceback (most recent call last):
  File "/Users/ionutcohen/Dropbox/PycharmProjects/chn/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/ionutcohen/Dropbox/PycharmProjects/chn/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/ionutcohen/Dropbox/PycharmProjects/chn/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 306, in execute
    parser = CommandParser(None, usage="%(prog)s subcommand [options] [args]", add_help=False)
  File "/Users/ionutcohen/Dropbox/PycharmProjects/chn/venv/lib/python3.6/site-packages/django/core/management/base.py", line 47, in __init__
    super().__init__(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/argparse.py", line 1633, in __init__
    self._positionals = add_group(_('positional arguments'))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gettext.py", line 606, in gettext
    return dgettext(_current_domain, message)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gettext.py", line 570, in dgettext
    codeset=_localecodesets.get(domain))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gettext.py", line 505, in translation
    mofiles = find(domain, localedir, languages, all=True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gettext.py", line 477, in find
    for nelang in _expand_lang(lang):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gettext.py", line 206, in _expand_lang
    loc = locale.normalize(loc)
AttributeError: module 'locale' has no attribute 'normalize'

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

这是我的语言环境文件夹的样子: 在此处输入图片说明

这是 gettext.py 中引用错误的函数。似乎第一行有错误:

def _expand_lang(loc):
    loc = locale.normalize(loc)
    COMPONENT_CODESET   = 1 << 0
    COMPONENT_TERRITORY = 1 << 1
    COMPONENT_MODIFIER  = 1 << 2
    # split up the locale into its base components
    mask = 0
    pos = loc.find('@')
    if pos >= 0:
        modifier = loc[pos:]
        loc = loc[:pos]
        mask |= COMPONENT_MODIFIER
    else:
        modifier = ''
    pos = loc.find('.')
    if pos >= 0:
        codeset = loc[pos:]
        loc = loc[:pos]
        mask |= COMPONENT_CODESET
    else:
        codeset = ''
    pos = loc.find('_')
    if pos >= 0:
        territory = loc[pos:]
        loc = loc[:pos]
        mask |= COMPONENT_TERRITORY
    else:
        territory = ''
    language = loc
    ret = []
    for i in range(mask+1):
        if not (i & ~mask):  # if all components for this combo exist ...
            val = language
            if i & COMPONENT_TERRITORY: val += territory
            if i & COMPONENT_CODESET:   val += codeset
            if i & COMPONENT_MODIFIER:  val += modifier
            ret.append(val)
    ret.reverse()
    return ret
Run Code Online (Sandbox Code Playgroud)

稍后编辑:我删除了 init 文件,现在出现此错误:

  File "/Users/ionutcohen/Dropbox/PycharmProjects/chn/manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
  File "/Users/ionutcohen/Dropbox/PycharmProjects/chn/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 12, in <module>
    from django.core.management.base import (
  File "/Users/ionutcohen/Dropbox/PycharmProjects/chn/venv/lib/python3.6/site-packages/django/core/management/base.py", line 7, in <module>
    from argparse import ArgumentParser
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/argparse.py", line 93, in <module>
    from gettext import gettext as _, ngettext
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gettext.py", line 409
    advance to next entry in the seek tables
             ^
SyntaxError: invalid syntax

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

MZA*_*MZA 10

我有同样的。在 PyCharm 中,我__init__.py为我的国际化创建了一个目录“locale”(无缘无故)并收到以下消息:

AttributeError: module 'locale' has no attribute 'normalize'

这是因为它正在寻找一个 Python 模块“locale”,但它被你的 Django 目录“locale”屏蔽了。

删除__init__.py和/或重命名您的目录“语言环境”。我两者都做了......正如我们在荷兰所说的那样,“缝两次就是把它缝好”。

似乎网络上到处都是人们使用“语言环境”作为他们国际化的目录。嗯.. 现在在我看来有点像创建一个名为“Class”的类或一个名为“Table”的 SQL 表。我已经看到完成,有时它有效,但我永远不会推荐它。