15

The following Python code works on my Windows machine (Python 2.5.4), but doesn't on my Debian machine (Python 2.5.0). I'm guessing it's OS dependent.

import locale
locale.setlocale( locale.LC_ALL, 'English_United States.1252' )

I receive the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/locale.py", line 476, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

Questions:

  • Is it OS dependent?
  • How can I find the supported locale list within Python?
  • How can I match between Windows locales and Debian locales?
Janusz
  • 187,060
  • 113
  • 301
  • 369

4 Answers4

22

It is OS dependent.

To get the list of local available you can use locale -a in a shell

I think the local you want is something like Windows-1252

Yoann Le Touche
  • 1,280
  • 9
  • 13
9

try

apt-get install locales-all

for me it works like a charm

pippo2600
  • 91
  • 1
  • 1
8

Look inside the locale.locale_alias dictionary.

>>> import locale
>>> len(locale.locale_alias)
789
>>> locale.locale_alias.keys()[:5]
['ko_kr.euc', 'is_is', 'ja_jp.mscode', 'kw_gb@euro', 'yi_us.cp1255']
>>> 

(In my 2.6.2 installation there are 789 locale names.)

gimel
  • 83,368
  • 10
  • 76
  • 104
  • 5
    Actually, the locales defined in the alias dictionary are not necessarily supported. – krawyoti Sep 08 '09 at 08:11
  • I tried a variation of this which was list(set(locale.locale_alias.values()) (values instead of keys because I want the real values, and convert to set and list again to retain only unique values). However there's another problem, as raised here (http://stackoverflow.com/questions/1728376/python-get-a-list-of-all-the-encodings-python-can-encode-to/1736533#1736533): are there locales which don't have aliases, and won't be in the alias dictionary at all? – metamatt Jan 08 '11 at 00:30
1

On Ubuntu Precise type

sudo locale-gen en_US

netz75
  • 57
  • 1
  • 6