locale vs wxLocale

Hello,

Now I would like to make my wxPython application multilingual, or at least read a language dictionary. Only I am still a litte bit confused, there is the Python locale, there is wxLocale, and I am missing the big picture. What I tried to do is aattached. Some questions:

1. Apparantly I need to install something for gettext.
    What does the parameter passed to the install method mean ?

2. What is the role of Python's locale if wxLocale is used.
    What is the relation to the gettext install method ?

3. When adding a Catalog, what is the syntax. Apparently it is a file.
    Should it be a .mo or a.po, or just the basename, and AddCatablog
    will look for both ?

4. What are the search directories ?

5. What is the search order for language catalogue, default, then
    additional ones ? For example if I redefine "File" ?

6. Is the python gettext method called for a _(""),
    or wxGetTranslation().
    In the first case, where does gettext look for the text ?

7. Is there a sample application somewhere.
    The internat in the wxWindows sample dir does not really help me.

Thanks for any hints.
peter

···

---

#!/usr/bin/env python

from wxPython.wx import *

class Ctest(wxApp):
     def OnInit(self):
         wxInitAllImageHandlers()

         l = wxLocale()
         l.Init(wxLANGUAGE_GERMAN)
         l.AddCatalog("test.po")

         self.frame = wxFrame(None, -1, _("Test Title"),
             style=wxDEFAULT_FRAME_STYLE)
         self.SetTopWindow(self.frame)
         self.frame.Show(True)

         return True

if __name__ == "__main__":
     import gettext
     gettext.install("test")
     test = Ctest()
     test.MainLoop()

Peter,

I can't give you answers to all your questions, but enclosed is a little test app I created when trying to figure this stuff out. I am sure there are still many improvements one could make but it does what I like to do for the moment.

Note that the locale directory has to be put into the same directory where you put python files (gettexttest etc).

I also create a registry enty (on Win XP, on Unixes I think it creates a file), you might want to take that code out or cleanup your registry after.

See up
Werner

P.s. to maintain the mo/po files I use a product called POEDIT.

Peter Wurmsdobler wrote:

gettexttest.zip (5.18 KB)

···

Hello,

Now I would like to make my wxPython application multilingual, or at least read a language dictionary. Only I am still a litte bit confused, there is the Python locale, there is wxLocale, and I am missing the big picture. What I tried to do is aattached. Some questions:

1. Apparantly I need to install something for gettext.
   What does the parameter passed to the install method mean ?

2. What is the role of Python's locale if wxLocale is used.
   What is the relation to the gettext install method ?

3. When adding a Catalog, what is the syntax. Apparently it is a file.
   Should it be a .mo or a.po, or just the basename, and AddCatablog
   will look for both ?

4. What are the search directories ?

5. What is the search order for language catalogue, default, then
   additional ones ? For example if I redefine "File" ?

6. Is the python gettext method called for a _(""),
   or wxGetTranslation().
   In the first case, where does gettext look for the text ?

7. Is there a sample application somewhere.
   The internat in the wxWindows sample dir does not really help me.

Thanks for any hints.
peter
---

#!/usr/bin/env python

from wxPython.wx import *

class Ctest(wxApp):
    def OnInit(self):
        wxInitAllImageHandlers()

        l = wxLocale()
        l.Init(wxLANGUAGE_GERMAN)
        l.AddCatalog("test.po")

        self.frame = wxFrame(None, -1, _("Test Title"),
            style=wxDEFAULT_FRAME_STYLE)
        self.SetTopWindow(self.frame)
        self.frame.Show(True)

        return True

if __name__ == "__main__":
    import gettext
    gettext.install("test")
    test = Ctest()
    test.MainLoop()

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Peter Wurmsdobler wrote:

Hello,

Now I would like to make my wxPython application multilingual, or at least read a language dictionary. Only I am still a litte bit confused, there is the Python locale, there is wxLocale, and I am missing the big picture. What I tried to do is aattached. Some questions:

1. Apparantly I need to install something for gettext.
   What does the parameter passed to the install method mean ?

Here's the source from gettext.py:

def install(domain, localedir=None, unicode=False):
     translation(domain, localedir, fallback=True).install(unicode)

2. What is the role of Python's locale if wxLocale is used.
   What is the relation to the gettext install method ?

You can use both, or you can use just wxLocale. But if you use just Python's gettext then the strings internal to wxWindows will not get translated. I think using just wxLocale woudl be easiest.

3. When adding a Catalog, what is the syntax. Apparently it is a file.
   Should it be a .mo or a.po, or just the basename, and AddCatablog
   will look for both ?

Just the basename. It's actually the translation domain like the gettext.install() above.

4. What are the search directories ?

See wxLocale_AddCatalogLookupPathPrefix

5. What is the search order for language catalogue, default, then
   additional ones ? For example if I redefine "File" ?

I would guess that it is the order that the catalogs are added.

6. Is the python gettext method called for a _(""),
   or wxGetTranslation().
   In the first case, where does gettext look for the text ?

You can set _ to be whatever you want with a simple assignment statement.

  _ = wxGetTranslation

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin,

thanks for your answers so far.

Here's the source from gettext.py:

def install(domain, localedir=None, unicode=False):
    translation(domain, localedir, fallback=True).install(unicode)

I am sorry, but this does not tell me anything.

What is a (translation) domain ?

My application lives in several files. I have the impression that if I have a

     import gettext
     gettext.install("test")

before the main application loop in only one file, any subsequent _() in all other files is apparently interpreted as gettext().

I guess that the gettext.install("test") tells python where to search for translations, AND the _() is gettext().

Is there a similar thing for wxLocale(), such that all _() or _T() or whatever is seen as wxGetTranslation() ?

peter

Werner,

thanks a lot for your example. The solution I could derive from it works !

First I create a locales instance in my application

class Cdm(wxApp):
     def OnInit(self):
         self.locales = CLocales(self)

with

class CLocales:
     def __init__(self,app):
         self.languages = [["English", ['en']],
                           ["German", ['de']],
                           ["French", ['fr']]]
         import locale
         import gettext

         localesDir = os.path.join(app.cwd, 'locales')
         gettext.install('dm', localesDir, unicode=0)

         self.languagedic = {}
         for language in self.languages:
             langstring = language[0]
             langcodes = language[1]
             self.languagedic[langstring] = gettext.translation('dm', localesDir, languages=langcodes)

         locale.setlocale(locale.LC_ALL, '')

         self.languagedic["English"].install()

     def SetLanguage(self,language):
         if self.languagedic.has_key(language):
             self.languagedic[language].install()

The locales object is passed to the menubar creation code with the callback of the selected language

     def OnLanguage(self,event):
         language = self.locales.languages[0][0] # actually get the language from the menu item,...
         self.locales.SetLanguage(language)
         self.SetMenuLabels()

Maybe the same could be done with wxLocale, but I could not figure out how to do it.

Thanks again,
peter

Peter Wurmsdobler wrote:

Robin,

thanks for your answers so far.

Here's the source from gettext.py:

def install(domain, localedir=None, unicode=False):
    translation(domain, localedir, fallback=True).install(unicode)

I am sorry, but this does not tell me anything.

What is a (translation) domain ?

I don't know, but I figured that with a little nudge you could do what I woudl have to do in order to answer your question: look at the source and docs.

My application lives in several files. I have the impression that if I have a

    import gettext
    gettext.install("test")

before the main application loop in only one file, any subsequent _() in all other files is apparently interpreted as gettext().

I guess that the gettext.install("test") tells python where to search for translations, AND the _() is gettext().

Is there a similar thing for wxLocale(), such that all _() or _T() or whatever is seen as wxGetTranslation() ?

Yep. To make something visible in all modules you just put it in the builtins module:

  __builtins__._ = wxGetTranslation

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin,

I don't know, but I figured that with a little nudge you could do what I woudl have to do in order to answer your question: look at the source and docs.

Use the source Luke! I will do so.

Yep. To make something visible in all modules you just put it in the builtins module:

    __builtins__._ = wxGetTranslation

Ahh, thanks.
peter