国际化python应用程序

时间:2014-10-05 15:43:15

标签: python internationalization pygtk glade

我看过很多国际化python应用程序的教程。我尝试过这个解决方案并且工作正常:Translating your Python/PyGTK application。我的意思是,它适用于helloworld

我已经按照相同的步骤为我的"大"程序,它不起作用。这是我粘贴在我的代码上的python代码:

import os, locale, gettext
APP_NAME = "myapp"

# Get the local directory since we are not installing anything
self.local_path = os.path.realpath(os.path.dirname(sys.argv[0]))
# Init the list of languages to support
langs = []
#Check the default locale
lc, encoding = locale.getdefaultlocale()
if (lc):
    #If we have a default, it's the first in the list
    langs = [lc]
# Now lets get all of the supported languages on the system
language = os.environ.get('LANGUAGE', None)
if (language):
    """langage comes back something like en_CA:en_US:en_GB:en
    on linuxy systems, on Win32 it's nothing, so we need to
    split it up into a list"""
langs += language.split(":")
"""Now add on to the back of the list the translations that we
know that we have, our defaults"""
langs += ["en_CA", "en_US", "es_ES"]

"""Now langs is a list of all of the languages that we are going
to try to use.  First we check the default, then what the system
told us, and finally the 'known' list"""

gettext.bindtextdomain(APP_NAME, self.local_path)
gettext.textdomain(APP_NAME)
# Get the language to use
self.lang = gettext.translation(APP_NAME, self.local_path
, languages=langs, fallback = True)
"""Install the language, map _() (which we marked our
strings to translate with) to self.lang.gettext() which will
translate them."""
_ = self.lang.gettext

执行时:

$ LANG=es_ES python myapp.py

我明白了:

$ Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.

如果我为LANG执行相同的helloworld配置,则可以正常工作。

知道如何修复这个问题吗?

1 个答案:

答案 0 :(得分:0)

在我的系统上,这就是正常工作:

$ LANGUAGE=es_ES python myapp.py

希望它有效!