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?
The locale setting defines the language of your user interface and the display formats for information like time, date, and currency.
A locale consists of a number of categories for which country-dependent formatting or other specifications exist. A program's locale defines its code sets, date and time formatting conventions, monetary conventions, decimal formatting conventions, and collation (sort) order.
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
You should not pass an explicit locale to setlocale, it is wrong. Let it find out from the environment. You have to pass it an empty string
import locale locale.setlocale(locale.LC_ALL, '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With