在Windows上设置Python语言环境的正确方法是什么?

问题描述:

我正在尝试以一种可识别语言环境的方式对字符串列表进行排序.我已经将Babel库用于其他与i18n相关的任务,但是它不支持排序. Python的locale模块提供了strcoll函数,但是需要将进程的语言环境设置为我要使用的语言环境.有点痛苦,但我可以忍受.

I'm attempting to sort a list of strings in a locale-aware manner. I've used the Babel library for other i18n-related tasks, but it doesn't support sorting. Python's locale module provides a strcoll function, but requires the locale of the process to be set to the one I want to work with. Kind of a pain, but I can live with it.

问题是我似乎无法实际设置语言环境. locale模块的文档给出了以下示例:

The problem is that I can't seem to actually set the locale. The documentation for the locale module gives this example:

import locale
locale.setlocale(locale.LC_ALL, 'de_DE')

当我运行它时,我得到了:

When I run that, I get this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\Lib\locale.py", line 494, in setlocale
locale.Error: unsupported locale setting

我在做什么错了?

似乎您正在使用Windows.区域设置字符串在此有所不同.仔细看一下文档:

It seems you're using Windows. The locale strings are different there. Take a more precise look at the doc:

locale.setlocale(locale.LC_ALL, 'de_DE') # use German locale; name might vary with platform

在Windows上,我认为应该是这样的:

On Windows, I think it would be something like:

locale.setlocale(locale.LC_ALL, 'deu_deu')

MSDN列出了语言字符串国家/地区字符串