【问题标题】:Django Internationalization---compilemessages error:AttributeError: module 'locale' has no attribute 'normalize'Django Internationalization---compilemessages error:AttributeError: module 'locale' has no attribute 'normalize'
【发布时间】:2018-01-14 18:58:06
【问题描述】:

我就在终点线前,感觉我还没有完成!我创建并编译了所有消息,以便拥有一个具有 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

这是我的语言环境文件夹的样子:

这是 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

稍后编辑:我已经删除了初始化文件,现在出现了这个错误:

  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

【问题讨论】:

  • 可能是错误的,但请尝试删除您的语言环境文件夹和任何子文件夹中的 init.py 文件 - 语言环境文件夹或其中的任何内容并不意味着是 python模块
  • @at14 谢谢!我已经删除了初始化文件,现在我得到了这个丑陋的错误:(
  • 您在尝试运行 compilemessages 时遇到了新错误?或尝试访问某些视图或功能时
  • 尝试运行服务器时出现错误“无法编码/解码的字符串是:erien���� d”
  • 您是否明确将某些内容投射到 str ?

标签: python django internationalization locale


【解决方案1】:

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

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

这是因为它正在寻找一个 Python 模块“locale”,但它被您的 Django 目录“locale”所掩盖。

要么删除__init__.py 和/或重命名您的目录“区域设置”。我两个都做了……正如我们在荷兰所说的那样,“缝合两次就是把它缝合好”。

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

【讨论】:

猜你喜欢
  • 2022-12-01
  • 2022-10-19
  • 2022-08-04
  • 2022-12-01
  • 2015-09-26
  • 2020-11-23
  • 2022-08-24
  • 2021-05-28
  • 2012-08-08
相关资源
最近更新 更多