38

On linux we can use locale -a to see the list of locales available.

$ locale -a
C
C.UTF-8
en_US.utf8
POSIX 

Is it possible to do the same from python console on windows?

This can be handy when you try to do locale.setlocale(locale.LC_ALL, '???') and simply don't know the name of the locale value.

minerals
  • 6,090
  • 17
  • 62
  • 107

5 Answers5

38
>>> import locale
>>> locale.locale_alias
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 2
    I just found out that ?(at least on a linux system), not all actually existing (and instaled) locales are listed in this dictionary. I did `[item for item in locale.locale_alias.items() if "pt_br" in item[0].lower()]` and got as a result: `[('pt_br.iso88591', 'pt_BR.ISO8859-1'), ('pt_br.88591', 'pt_BR.ISO8859-1'), ('pt_br', 'pt_BR.ISO8859-1'), ('pt_br@euro', 'pt_BR.ISO8859-15'), ('pt_br.iso885915', 'pt_BR.ISO8859-15')]` - the problerm is I have a ut-f8 pt_BR locale not listed above- the following works: `locale.setlocale(locale.LC_TIME, "pt_BR.utf-8")` – jsbueno May 11 '14 at 11:56
  • 4
    `locale_alias` does not work on Windows (checked on Python 2.7.8 Windows 7) – schlamar Nov 14 '14 at 10:01
  • 13
    To be clear: the values from `locale_alias` do not work on Windows. – schlamar Nov 14 '14 at 10:08
  • 2
    I found this to include far more locales than are actually available, as determined by trying to `setlocale(LC_ALL, ...)` some of them. It certainly includes far more than are in my `locale -a` output. It also _didn't_ include 3 of the 4 entries that were in `locale -a`. This was tested on macOS 10.14.6 and Ubuntu 18. – Phoenix Oct 28 '19 at 22:19
12

the richest locale support i found in python is babel.

please install by:

pip install babel

then,

import babel
all_ids = babel.localedata.locale_identifiers()

there is also extensive support for common terms translation etc. babel is being used in various other packages.

hth, alex

alex
  • 651
  • 1
  • 9
  • 11
11

You can look up available locale names on MSDN.

You have to pass the long version from "Language string" in the MSDN list as value to setlocale. The default L10N short codes like en_EN which are in locale_alias do NOT work in general.

I have already extracted some of them as dictionary:

LANGUAGES = {
    'bg_BG': 'Bulgarian',
    'cs_CZ': 'Czech',
    'da_DK': 'Danish',
    'de_DE': 'German',
    'el_GR': 'Greek',
    'en_US': 'English',
    'es_ES': 'Spanish',
    'et_EE': 'Estonian',
    'fi_FI': 'Finnish',
    'fr_FR': 'French',
    'hr_HR': 'Croatian',
    'hu_HU': 'Hungarian',
    'it_IT': 'Italian',
    'lt_LT': 'Lithuanian',
    'lv_LV': 'Latvian',
    'nl_NL': 'Dutch',
    'no_NO': 'Norwegian',
    'pl_PL': 'Polish',
    'pt_PT': 'Portuguese',
    'ro_RO': 'Romanian',
    'ru_RU': 'Russian',
    'sk_SK': 'Slovak',
    'sl_SI': 'Slovenian',
    'sv_SE': 'Swedish',
    'tr_TR': 'Turkish',
    'zh_CN': 'Chinese',
}
manasouza
  • 1,189
  • 1
  • 14
  • 26
schlamar
  • 9,238
  • 3
  • 38
  • 76
  • This will vary by machine and installation so many of these may not be valid for the user, the locale_alias command is the correct way to get the list – Ezekiel Kruglick Apr 17 '15 at 18:13
  • 2
    the values from `locale_alias` do not work on Windows! – schlamar Apr 22 '15 at 06:03
  • I was curious about your response so I just tested locale_alias on three windows machines. It worked on all three. The machines had various flavors of 2.7 installed although I note they use the Enthought distribution installed so perhaps they are doing something cool there? What do you get from: import locale; locale.locale_alias ? And Python version/distribution? – Ezekiel Kruglick Apr 22 '15 at 17:30
  • 1
    `locale_alias` does work but its values cannot be passed to `setlocale` ("locale.Error: unsupported locale setting")! For example, `'de'` gives `'de_DE.ISO8859-1'` on default Python 2.7.9 – schlamar Apr 23 '15 at 12:06
  • oh, that! Yes, Windows reports more locales than can be fully used. As I heard it (caution: have not researched) lots of windows programs install "locations" for themselves to use but don't install the full standards-compliant package - just the bits they need - so windows reports lots of locations that actually aren't supported. A lot from your list also won't be supported (or listed) though, depending on install parameters and coding. Note the MSDN link doesn't list the key codes (& also doesn't apply to Win 7 or 8). The correct code should be shown in locale_alias though. – Ezekiel Kruglick Apr 23 '15 at 16:11
  • 1
    Again: You have to pass the long version from "Language string" in the MSDN list as value to `setlocale`. The default L10N short codes like `en_EN` which are in `locale_alias` do NOT work in general! See https://bitbucket.org/snippets/schlamar/8nxR – schlamar Apr 24 '15 at 06:54
  • Yes, that's basically what I've been saying, perhaps there was a miscommunication here :) You should edit some of this added information into your answer so I can take off the -1 – Ezekiel Kruglick Apr 24 '15 at 16:22
  • I think the Babel solution below is by far more complete. Currently there are 789 locales listed in Babel – Edward Gaere Feb 07 '22 at 04:33
2

This snippet works for me running on repl.it(python 3.8.2), Windows(3.9.1), and LSW(3.9.2):

import locale
available_locales = []
for l in locale.locale_alias.items():
  try:
    locale.setlocale(locale.LC_ALL, l[1])
    available_locales.append(l)
  except:
    pass
Jim Hessin
  • 137
  • 2
  • 11
0

This snippet tries out all the locales known to the locales package and keeps the ones that don't crash, i.e. are available. (Tested on Windows 10 with Python 3.7.3)

import locale
available_locales = []
for l in locale.locale_alias:
    try:
        locale.setlocale(locale.LC_ALL, l)
        available_locales.append(l)
    except:
        pass
Wouter
  • 160
  • 8