我如何(在GNU/Linux系统上)找到与模块一起使用的所有可用语言环境locale?
我发现在模块中唯一关闭的是locale_alias具有区域设置别名的字典.有时会将其视为查看您所拥有的语言环境的位置,但它不包含所有别名.在我的系统上这个程序
#! /usr/bin/python3
import locale
for k, v in sorted(locale.locale_alias.items()):
if k.startswith('fr_') or v.startswith('fr_'):
print('{:20}{}'.format(k, v))
Run Code Online (Sandbox Code Playgroud)
版画
c-french fr_CA.ISO8859-1
fr fr_FR.ISO8859-1
fr_be fr_BE.ISO8859-1
fr_ca fr_CA.ISO8859-1
fr_ch fr_CH.ISO8859-1
fr_fr fr_FR.ISO8859-1
fr_lu fr_LU.ISO8859-1
français fr_FR.ISO8859-1
fre_fr fr_FR.ISO8859-1
french fr_FR.ISO8859-1
french.iso88591 fr_CH.ISO8859-1
french_france fr_FR.ISO8859-1
Run Code Online (Sandbox Code Playgroud)
忽略所有utf-8语言环境,例如'fr_FR.utf8',它们确实可以用作参数locale.setlocale.从壳,locale -a | grep "^fr_.*utf8"给出
fr_BE.utf8
fr_CA.utf8
fr_CH.utf8
fr_FR.utf8
fr_LU.utf8
Run Code Online (Sandbox Code Playgroud)
显示了很多选项.(当然,一种方法是从Python运行这个shell命令,但我认为有一种方法可以直接从Python中执行此操作.)
似乎没有直接从 Python 执行此操作的好方法,因此我将回答如何从 Python 运行此 shell 命令。
\n\n#! /usr/bin/python3\nimport subprocess\n\ndef find_locales():\n out = subprocess.run([\'locale\', \'-a\'], stdout=subprocess.PIPE).stdout\n try:\n # Even though I use utf8 on my system output from "locale -a"\n # included "bokm\xc3\xa5l" in Latin-1. Then this won\'t work, but the\n # exception will.\n res = out.decode(\'utf-8\')\n except:\n res = out.decode(\'latin-1\')\n return res.rstrip(\'\\n\').splitlines()\n\nif __name__ == "__main__":\n for loc in find_locales():\n print(loc)\nRun Code Online (Sandbox Code Playgroud)\n\n请注意,这subprocess.run是 Python 3.5 中的新功能。对于早期的 Python 版本,请参阅此问题,了解运行 shell 命令的替代方法。
| 归档时间: |
|
| 查看次数: |
363 次 |
| 最近记录: |