How can I list all available windows locales in python console?
Question:
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.
Answers:
>>> import locale
>>> locale.locale_alias
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',
}
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
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
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
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.
>>> import locale
>>> locale.locale_alias
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',
}
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
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
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