92

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?

kenorb
  • 155,785
  • 88
  • 678
  • 743
DNS
  • 37,249
  • 18
  • 95
  • 132

6 Answers6

121

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

zezollo
  • 4,606
  • 5
  • 28
  • 59
Schnouki
  • 7,527
  • 3
  • 33
  • 38
  • 5
    FWIW, I had the problem under `Ubuntu 13.04`, `Linux 3.8.0-19`, with python `2.7.4` when trying to set the locale to `fr_FR`. Setting it to `fr_FR.UTF-8` worked for me. – Zoneur Sep 11 '13 at 12:38
  • For Python 3.6.3 in Conda 4.4.11, Windows 7, the locale strings seem to be the same as other OS. – srodriguex Apr 09 '18 at 18:55
  • 2
    This list is more comprehensive: https://msdn.microsoft.com/en-us/library/cc233982.aspx – Cristian Ciupitu May 10 '18 at 17:30
  • 3
    The linked docs seem to be too recent for WServer2018R2 I had to do use `'eng_usa'` (`'en_US'` didn't work) – Boop May 29 '19 at 08:09
26

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, '')
u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
  • 11
    I didn't mention this in the question because it wasn't directly related, but the code I was writing at the time was designed for use on a web server. In other words, the locale might change with each request, and is not necessarily ever the same as the environment's locale. – DNS Sep 10 '09 at 13:57
  • 3
    DNS: Have you read the docs for locale? It implies it might be dangerous to call setlocale "much", and it is not thread safe. So perhaps something else than setlocale is the solution. Gettext can load different catalogs and switch at runtime for example; but I don't know what you are using the locale for. – u0b34a0f6ae Sep 10 '09 at 15:02
  • Unless your user is not on your local machine – Gabriel Nov 16 '18 at 16:09
21

This is the only correct way to use it for the German locale, as the OP is asking:

import locale

locale.setlocale(
    category=locale.LC_ALL,
    locale="German"  # Note: do not use "de_DE" as it doesn't work
)

Please note, though, that it is best to use this:

import locale

locale.setlocale(
    category=locale.LC_ALL,
    locale=""
)

This sets the locale for all categories to whatever the default locale settings are set for a user on their operating system. I strongly suggest that locale="" should be used always.

Boštjan Mejak
  • 827
  • 9
  • 23
10

Ubuntu

On Ubuntu you may have this problem because you don't have that local installed on your system.

From shell try a:

$> locale -a

and check if you find the locale you are interested in. Otherwise you have to install it:

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

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

$> sudo dpkg-reconfigure locales

After that try again in your python shell:

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

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

kenorb
  • 155,785
  • 88
  • 678
  • 743
linello
  • 8,451
  • 18
  • 63
  • 109
7

I know this has been asked years ago, but I thought I'd try adding what I found out, using Python 3.6 on Windows:

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

I tried some and that also seems to be a way of finding out, what's available on Windows.

Good to know: This for some reason is not compatible to strptime() at the current stable version of Python

And then you simply set the locale:

locale.setlocale(locale.LC_ALL, any_item_of_the_printed_strings)

TheGeekGuy
  • 137
  • 1
  • 7
5

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.
    """"

Under Linux (especially Ubuntu) you can either 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 you have the locale installed on your system:

$ locale -a # to list the currently installed locales
$ (sudo) locale-gen de_DE.UTF-8 # to install new locale
Alex Bitek
  • 6,529
  • 5
  • 47
  • 77