简体   繁体   English

如何在 python 控制台中列出所有可用的 Windows 语言环境?

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

On linux we can use locale -a to see the list of locales available.在 linux 上,我们可以使用locale -a查看可用的语言环境列表。

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

Is it possible to do the same from python console on windows ?是否可以从Windows上的 python 控制台执行相同操作?

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.当您尝试执行locale.setlocale(locale.LC_ALL, '???')并且根本不知道区域设置值的名称时,这会很方便。

>>> import locale
>>> locale.locale_alias

You can look up available locale names on MSDN .您可以在MSDN上查找可用的语言环境名称。

You have to pass the long version from "Language string" in the MSDN list as value to setlocale .您必须将 MSDN 列表中“语言字符串”中的长版本作为值传递给setlocale The default L10N short codes like en_EN which are in locale_alias do NOT work in general.默认的 L10N 短代码(如en_EN中的locale_alias通常不起作用。

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.我在 python 中发现的最丰富的语言环境支持是 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.还有对常用术语翻译等的广泛支持。 babel 正被用于各种其他软件包。

hth, alex嗯,亚历克斯

This snippet tries out all the locales known to the locales package and keeps the ones that don't crash, ie are available.此代码段尝试使用 locales 包已知的所有语言环境,并保留不会崩溃的语言环境,即可用的语言环境。 (Tested on Windows 10 with Python 3.7.3) (使用 Python 3.7.3 在 Windows 10 上测试)

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):此代码段适用于在 repl.it(python 3.8.2)、Windows(3.9.1) 和 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM