为什么我在python中这样做时会得到以下错误:

>>> import locale
>>> print str( locale.getlocale() )
(None, None)
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/locale.py", line 531, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

这也适用于其他地区,如fr或nl。我用的是Ubuntu 11.04。

更新:做以下事情没有任何结果:

dpkg-reconfigure locales
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LANG = (unset)
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory

当前回答

执行此命令 Locale来获取所使用的Locale。如:

LANG = en_US。utf - 8 语言= en_US: en LC_CTYPE = zh_CN。utf - 8 LC_NUMERIC = " en_US。utf - 8” 而= " en_US。utf - 8” LC_COLLATE = " en_US。utf - 8” LC_MONETARY = " en_US。utf - 8” LC_MESSAGES = " en_US。utf - 8” LC_PAPER = " en_US。utf - 8” LC_NAME = " en_US。utf - 8” LC_ADDRESS = " en_US。utf - 8” LC_TELEPHONE = " en_US。utf - 8” LC_MEASUREMENT = " en_US。utf - 8” LC_IDENTIFICATION = " en_US。utf - 8” LC_ALL =

在/etc/locale-gen文件的第一步中搜索列出的地区列表。对使用过的邮件取消评论 运行locale-gen生成新添加的区域设置

其他回答

在Arch Linux上,我可以通过运行sudo locale-gen来解决这个问题

首先,通过以下步骤确保你已经安装了语言包:

sudo apt-get install language-pack-en-base


sudo dpkg-reconfigure locales

您可能没有任何de_DE区域可用。

您可以使用locale -a命令查看可用区域设置的列表。 例如,在我的机器上:

$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
it_CH.utf8
it_IT.utf8
POSIX

注意,如果你想将区域设置为it_IT,你还必须指定.utf8:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'it_IT')   # error!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/locale.py", line 539, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
>>> locale.setlocale(locale.LC_ALL, 'it_IT.utf8')
'it_IT.utf8'

要安装一个新的区域设置,请使用:

sudo apt-get install language-pack-id

其中id是语言代码(从这里取)

安装locale后,你应该按照Julien Palard的建议重新配置locale:

sudo dpkg-reconfigure locales

把它放在ENV上面的Dockerfile中。

# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8

ENV LANG en_US.UTF-8

python寻找.UFT-8,但你可能有.utf8 尝试使用sudo dpkg-reconfigure locale安装.UFT-8包