为什么我在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
您可能没有任何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
你的错误清楚地说,你正在尝试使用地区,一些东西不存在。
>>> 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 581, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
语言环境。错误:不支持的区域设置
要检查可用的设置,请使用locale -a
deb@deb-Latitude-E7470:/ambot$ 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
POSIX
所以你可以用其中一个,
>>> locale.setlocale(locale.LC_ALL, 'en_AG.utf8')
'en_AG.utf8'
>>>
对于de_DE
这个文件既可以手动调整,也可以使用update-locale工具更新。
update-locale LANG=de_DE.UTF-8