[wxPython-users] Use another or native Language
Robin Dunn
robin at alldunn.com
Mon Aug 14 10:18:42 PDT 2006
Franz Steinhäusler wrote:
> Example from PyCrust:
>
>>>> _("&Next")
> Traceback (most recent call last):
> File "<input>", line 1, in ?
> NameError: name '_' is not defined
>>>> lc = wx.Locale(wx.LANGUAGE_DEFAULT)
>>>> _("&Next")
> Traceback (most recent call last):
> File "<input>", line 1, in ?
> NameError: name '_' is not defined
>>>> import os
>>>> import gettext
> ...
>>>> localedir = os.path.join(wx.__path__[0], "locale")
> ...
>>>> langid = wx.LANGUAGE_DEFAULT # use OS default; or use LANGUAGE_JAPANESE, etc.
>>>> domain = "wxstd" # the translation file is messages.mo
> ...
>>>> mylocale = wx.Locale(langid)
>>>> mytranslation = gettext.translation(domain, localedir,
> ... [mylocale.GetCanonicalName()], fallback = True)
>>>> mytranslation.install()
> ...
>>>> _("&Next")
> '&Weiter'
>
> And I'm looking for a way to shorten this procedure.
_ is just a shortcut to a function (typically) and so you just need to
give it a definition, or set it to some existing function. For example:
>>> _ = wx.GetTranslation
>>> lc = wx.Locale(wx.LANGUAGE_FRENCH)
>>> _("Next")
u'Suivant'
>>> del lc
>>> lc = wx.Locale(wx.LANGUAGE_JAPANESE)
>>> _("Next")
u'\u6b21\u3078'
Or you could set it globally like the gettext module does by putting it
in the __builtin__ module:
import __builtin__
__builtin__.__dict__['_'] = wx.GetTranslation
>
> Second question:
> Ok, now the paths for the catalog for wxstd are initailized.
>
> I'm looking for a convenient way to initialize a catalog file:
> For example: my App starte in c:\Eigene Dateien\python\drpython\drpython.py
>
> First: what would be a convenient place to locate the .mo file?
It depends on the platform, as each has their standard locations for .mo
files, although I think that it always looks in the current working
directory first. You can also add additional directories to the search
path by calling wx.Locale.AddCatalogLookupPathPrefix.
> Second: is it possible to use the (untranslated) file .po directly? (it doesn't seem to work)
Yes and No. Normally you can't, but if you really wanted to you could
write a function that can parse the .po files and pull the translations
out for you, and then assign that function to _, but only your code that
uses that function would be using the .po file instead of the .mo file
> Third: What is the right way to initialize to use this catalog (in addition to wxstd.mo)?
>
> (mo file is in the following location):
> c:\Eigene Dateien\python\drpython\locale\de\LC_MESSAGES\drpython.mo
lc.AddCatalog('drpython')
BTW, I think that on Windows it will not look in
<prefix>/<lang>/LC_MESSAGES like it does on the other platforms, just
<prefix>/<lang>.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list