在使用不同语言环境的 PC 上用俄语打印 Python

时间:2021-05-20 19:16:58

标签: python internationalization gettext

我正在 Python 中学习 i8N,使用西方字符集一切正常,但我在使用东方和东方字符集验证/测试时遇到问题。我的电脑设置为意大利和西班牙,因此为了测试其他语言,我强制将区域设置按代码设置为以下语言之一:

    locale.setlocale(locale.LC_ALL, 'German')
    locale.setlocale(locale.LC_ALL, 'Spanish')
    locale.setlocale(locale.LC_ALL, 'Italian')
    locale.setlocale(locale.LC_ALL, 'French')
    locale.setlocale(locale.LC_ALL, 'Russian')

前四个正在工作,然后对于那些不以俄语工作的人,我设置了默认的 stdout 和 stderr 字符集:

...
elif "Russian" in l[0] or "ru_" in l[0]:
    ru = gettext.translation('base', localedir='locales', languages=['ru'])
    ru.install()
    _ = ru.gettext
    import io
    sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
    sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

这样就没有运行时错误但输出似乎不是俄语,这应该是“Hello Python!”的俄语翻译:

Привет,Python!

所以,我搜索了网络,但没有找到任何有用的东西,我想我错过了一些东西......

0 个答案:

没有答案