Python:gettext不会在Windows上加载翻译 [英] Python: gettext doesn't load translations on Windows

查看:79
本文介绍了Python:gettext不会在Windows上加载翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段特定的代码在Linux上运行良好,但在Windows上却无法运行:

This particular piece of code works very well on Linux, but not on Windows:

locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain('exposong', LOCALE_PATH)
gettext.textdomain('exposong')

来自此处的代码

即使我在locale.setlocale中指定了语言环境(我尝试了不同的格式),也无法正常工作. 一个问题可能是未在环境变量中设置语言环境(但我使用的是德语Windows版本;已在XP和Vista上进行了测试).如果我在命令行上执行"Set Lang=de_DE",那么一切都会按预期进行.

Even if i specify the locale in locale.setlocale (I tried different formats) it doesn't work. One problem might be that the locale is not set in the environment variables (but I use a German Windows version; tested on XP and Vista). If I do "Set Lang=de_DE" on the command line, everything works as expected.

有什么想法吗?

推荐答案

Python中的标准gettext模块不使用Windows设置中的 startdard语言设置,而是依靠环境变量之一的存在: LANGUAGELC_MESSAGESLC_ALLLANG. (我想这是 slack 将Unix/Linux库移植到Windows的示例.)

Standard gettext module in Python does not use startdard language settings from Windows settings, but instead relies on presence one of the environment variables: LANGUAGE, LC_MESSAGES, LC_ALL or LANG. (I'd say this is example of slack porting of Unix/Linux library to Windows.)

上面提到的环境变量在典型的Windows机器上不存在,因为OS Windows和本机应用程序使用注册表中的设置.因此,您需要从Windows注册表中获取语言设置,并将其置于流程环境中.

The environment variables mentioned above do not present on typical Windows machine, because OS Windows and native applications use settings from registry instead. So you need to get the language settings from Windows registry and put them into process environment.

您可以为此使用我的帮助程序模块: https://launchpad.net/gettext-py-windows

You can use my helper module for this: https://launchpad.net/gettext-py-windows

此帮助程序从Windows设置获取语言设置,并为当前进程设置LANG变量,因此gettext可以使用此设置.

This helper obtains language settings from Windows settings and set LANG variable for current process, so gettext can use this settings.

因此,如果所涉及的应用程序不是您的应用程序,则可以执行以下操作.像往常一样使用python setup.py install安装我的gettext助手.然后在locale.setlocale(locale.LC_ALL, '')之前添加这些行:

So, if the application in question is not yours you can do the following. Install my gettext helper as usual with python setup.py install. Then add these lines before locale.setlocale(locale.LC_ALL, ''):

import gettext_windows
gettext_windows.setup_env()

仅此而已.

这篇关于Python:gettext不会在Windows上加载翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆