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.
Python's locale module is part of the standard library for internationalization (i18n) and localization (l10n) in Python. The locale module allows developers to deal with certain cultural issues in their applications.
Probing the Current Locale The most common way to let the user change the locale settings for an application is through an environment variable ( LC_ALL , LC_CTYPE , LANG , or LANGUAGE , depending on the platform). The application then calls setlocale() without a hard-coded value, and the environment value is used.
>>> 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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With