Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 895
DNS Avatar asked Jun 05 '09 13:06

DNS


People also ask

What is locale setting?

The locale setting defines the language of your user interface and the display formats for information like time, date, and currency.

What is computer locale?

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.


2 Answers

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

like image 124
Schnouki Avatar answered Sep 28 '22 01:09

Schnouki


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, '') 
like image 44
u0b34a0f6ae Avatar answered Sep 28 '22 00:09

u0b34a0f6ae