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

I am trying to sort a list of strings according to language. I used the Babel library for other i18n related tasks, but it does not support sorting. The Python locale module provides the strcoll function, but requires the process locale to be set to the one I want to work with. Kind of pain, but I can live with it.

The problem is that I cannot set the locale. The documentation for the locale module shows an example:

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

When I run this, I get the following:

 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?

+71
python windows internationalization localization
Jun 05 '09 at 13:56
source share
6 answers

You seem to be using Windows. The locale lines are different there. Take a look at the document:

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

On Windows, I think it will be something like:

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

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

+92
Jun 05 '09 at 14:13
source share

You must not pass an explicit locale to setlocale, this is wrong. Let him learn from the environment. You must pass it an empty string

 import locale locale.setlocale(locale.LC_ALL, '') 
+11
Sep 08 '09 at 18:19
source share

This is the only way to do this on Windows (German example):

 import locale locale.setlocale(category=locale.LC_ALL, locale="German") # Not locale="de_DE" 
+8
Mar 27 '17 at 0:37
source share

Ubuntu

On Ubuntu, you may have this problem because you do not have this local installation on your system.

From the shell, try a:

 $> locale -a 

and check if the language of interest is found. Otherwise, you must install it:

 $> sudo apt-get install language-pack-XXX 

where XXX is your language (in my case "xxx = it", Italian) Then run dpkg-reconfigure :

 $> sudo dpkg-reconfigure locales 

After that, try again in the python shell:

 >>> import locale >>> locale.setlocale(locale.LC_ALL,'it_IT.UTF-8') 

(this is for the Italian locale, which was what I needed)

+6
Jun 30 '14 at 7:59
source share

I know this was asked a few years ago, but I thought that I would try to add what I learned using Python 3.6 for Windows:

 import locale for x in locale.windows_locale.values(): print(x.replace('_','-')) 

I tried some, and it also seems like a way to find out what is available on Windows.

Good to know: this is for some reason incompatible with strptime () in the current stable version of Python

And then you just set the language:

locale.setlocale(locale.LC_ALL, any_item_of_the_printed_strings)

+4
Feb 18 '18 at 3:35
source share

From locale.setlocale docs:

 locale.setlocale(category, locale=None): """ Set the locale for the given category. The locale can be a string, an iterable of two strings (language code and encoding), or None. """" 

On Linux (especially Ubuntu) you can use

 locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') 

or

 locale.setlocale(locale.LC_ALL, ('de', 'utf-8')) 

You will get the same error if the locale is not installed on the system. So make sure your system is set to locale :

 $ locale -a # to list the currently installed locales $ (sudo) locale-gen de_DE.UTF-8 # to install new locale 
+3
Apr 25 '14 at 23:16
source share



All Articles