How can I list all available windows locales in python console?
Asked Answered
G

5

40

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.

Galloromance answered 31/10, 2013 at 14:21 Comment(0)
B
41
>>> import locale
>>> locale.locale_alias
Barbbarba answered 31/10, 2013 at 14:26 Comment(4)
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")Balm
locale_alias does not work on Windows (checked on Python 2.7.8 Windows 7)Schliemann
To be clear: the values from locale_alias do not work on Windows.Schliemann
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.Piezoelectricity
G
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

Gaspard answered 15/9, 2016 at 8:46 Comment(1)
I think this Babel solution below should be the accepted answer. Currently there are 789 locales listed in BabelGoat
S
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',
}
Schliemann answered 14/11, 2014 at 10:3 Comment(8)
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 listManque
the values from locale_alias do not work on Windows!Schliemann
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?Manque
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.9Schliemann
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.Manque
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 bitbucket.org/snippets/schlamar/8nxRSchliemann
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 -1Manque
I think the Babel solution below is by far more complete. Currently there are 789 locales listed in BabelGoat
H
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
Haihaida answered 29/3, 2021 at 16:59 Comment(0)
H
1

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
Hypesthesia answered 14/10, 2020 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.