2014-10-05 95 views
1

我看过很多教程来国际化python应用程序。我已经试过这个解决方案,并且工作正常:Translating your Python/PyGTK application。我的意思是,它可以很好地与helloworld国际化python应用程序

我对我的“大”程序采取了相同的步骤,但它不起作用。这是我在我的代码粘贴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它工作正常。

任何想法如何解决这个问题?

回答

0

在我的系统,这是什么工作:

$ LANGUAGE=es_ES python myapp.py 

希望工程!