What is the correct way to set Python's locale on Windows?

DNS picture DNS · Jun 5, 2009 · Viewed 96k times · Source

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.

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

What am I doing wrong?

Answer

Schnouki picture Schnouki · Jun 5, 2009

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

On Windows, I think it would be something like:

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

MSDN has a list of language strings and of country/region strings