Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

locale

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.

like image 326
minerals Avatar asked Oct 31 '13 14:10

minerals


People also ask

What is locale library in Python?

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.

How do I change the locale in Python?

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.


4 Answers

>>> import locale
>>> locale.locale_alias
like image 134
devnull Avatar answered Sep 22 '22 16:09

devnull


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',
}
like image 41
schlamar Avatar answered Sep 23 '22 16:09

schlamar


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

like image 41
alex Avatar answered Sep 22 '22 16:09

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
like image 43
Jim Hessin Avatar answered Sep 22 '22 16:09

Jim Hessin