Reputation: 38189
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?
Upvotes: 94
Views: 139146
Reputation: 46
I had the same problem, when trying to use a "pt_BR.utf-8"
locale and variations, like this:
import locale
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
So i tried this:
import locale
locale.setlocale(locale.LC_ALL, "Portuguese_Brazil.1252")
Here have a list of variations for most locales
Upvotes: 0
Reputation: 977
This is the only correct way to set 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
)
But please note, though, that it is best to set it like this:
import locale
locale.setlocale(
category=locale.LC_ALL,
locale=""
)
We should pass an empty string to the locale
argument as locale=""
, which will automatically choose the default locale that is set on a user's operating system.
I strongly suggest that you always pass locale=""
, because this is the only locale-independent way that will provide absolute versatility.
Upvotes: 25
Reputation: 137
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)
Upvotes: 7
Reputation: 7707
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
Upvotes: 123
Reputation: 8694
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)
Upvotes: 12
Reputation: 534
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
Upvotes: 6
Reputation: 49803
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, '')
Upvotes: 26