wx.Locale mystery

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

wx.Locale mystery

Donn Ingle
Hi again,
 I'm going slightly mad. Here's the setup:

1. I have a .mo file in the right place and I know it works because the
normal Python gettext procedure is working.
2. I have installed the Swedish locale (don't ask me why :) ) and I know it
works because of 1. (I also test with af_ZA.utf8)
3. Before I run the app I set [LANG=sv_SE.utf8]

* I'm on Kubuntu Gnu/Linux, Python 2.5, wxPython 2.8 unicode.

I am doing this (from the wiki):

langid = wx.LANGUAGE_DEFAULT # Assume it fetches from $LANG
localedir = "fontypython/locales"
langid = wx.LANGUAGE_SWEDISH # trying to force it
os.environ['LANG'] = "sv_SE.utf8" # force it again!
mylocale = wx.Locale( langid )
mylocale.AddCatalogLookupPathPrefix( localedir )
mylocale.AddCatalog( "wxgui" ) # it finds this file.
_ = wx.GetTranslation

# All these show that it has Swedish
print mylocale.GetCanonicalName()
print mylocale.GetLanguage()
print mylocale.GetLocale()
print mylocale.GetName()
# But this one will not work... :(
print wx.GetTranslation("Some String")
print _("Some String") # Does not work either.

Also, to test things out, I run the wxPython demo under another LANG setting
and it does not seem to change. I'm pretty sure I have at least af_ZA.utf8
installed fully (Swedish may be broken). I was looking at the stock buttons
control, to see if the text labels would change language but they
steadfastly remain in English.

I am using a few stock buttons in the app and was relying on them 'just
working'.

I am sure I'm to blame, but it's such a minefield that I'm just lost.

Is there another module of wxPython I still need to install?
\d


Reply | Threaded
Open this post in threaded view
|

Re: wx.Locale mystery

Cody Precord
Hello,

I do something like the following,

import gettext
import wx

the_locale = wx.Locale(wx.LANGUAGE_***)
the_locale.AddCatalogLookupPathPrefix("path/to/gettext mo files/")
the_locale.AddCatalog("PROGRAM NAME") # Assuming locale files are  
named PROGRAM NAME.mo
language = gettext.translation("PROGRAM NAME", "path/to/locale files  
dir/",
                                                        ["Canonical  
Name (i.e. sv_SE)"],
                                                        fallback=True)
language.install()

Also be sure that the wxLocale object remains alive during the  
runtime of your program.

Regards,

Cody Precord

On Dec 20, 2007, at 5:12 AM, Donn Ingle wrote:

> Hi again,
>  I'm going slightly mad. Here's the setup:
>
> 1. I have a .mo file in the right place and I know it works because  
> the
> normal Python gettext procedure is working.
> 2. I have installed the Swedish locale (don't ask me why :) ) and I  
> know it
> works because of 1. (I also test with af_ZA.utf8)
> 3. Before I run the app I set [LANG=sv_SE.utf8]
>
> * I'm on Kubuntu Gnu/Linux, Python 2.5, wxPython 2.8 unicode.
>
> I am doing this (from the wiki):
>
> langid = wx.LANGUAGE_DEFAULT # Assume it fetches from $LANG
> localedir = "fontypython/locales"
> langid = wx.LANGUAGE_SWEDISH # trying to force it
> os.environ['LANG'] = "sv_SE.utf8" # force it again!
> mylocale = wx.Locale( langid )
> mylocale.AddCatalogLookupPathPrefix( localedir )
> mylocale.AddCatalog( "wxgui" ) # it finds this file.
> _ = wx.GetTranslation
>
> # All these show that it has Swedish
> print mylocale.GetCanonicalName()
> print mylocale.GetLanguage()
> print mylocale.GetLocale()
> print mylocale.GetName()
> # But this one will not work... :(
> print wx.GetTranslation("Some String")
> print _("Some String") # Does not work either.
>
> Also, to test things out, I run the wxPython demo under another  
> LANG setting
> and it does not seem to change. I'm pretty sure I have at least  
> af_ZA.utf8
> installed fully (Swedish may be broken). I was looking at the stock  
> buttons
> control, to see if the text labels would change language but they
> steadfastly remain in English.
>
> I am using a few stock buttons in the app and was relying on them  
> 'just
> working'.
>
> I am sure I'm to blame, but it's such a minefield that I'm just lost.
>
> Is there another module of wxPython I still need to install?
> \d
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: wxPython-users-
> [hidden email]
>


Reply | Threaded
Open this post in threaded view
|

Re: wx.Locale mystery

Donn Ingle
> I do something like the following,
I have just tried a 'mixture' of the two like that and it's still confusing.

If I mix the two and then remove the line:
_ = wx.GetTranslation
The app now responds to print _("stuff") but the stock buttons are still all
in English.
 I don't think the wx.Locale stuff is doing anything at all - the gettext
stuff is just working via _


> Also be sure that the wxLocale object remains alive during the
> runtime of your program.
How could I check? And what kills it?

\d


Reply | Threaded
Open this post in threaded view
|

Re: Re: wx.Locale mystery

Cody Precord
Hello,

On Dec 20, 2007, at 6:07 AM, Donn Ingle wrote:

>> I do something like the following,
> I have just tried a 'mixture' of the two like that and it's still  
> confusing.
>
> If I mix the two and then remove the line:
> _ = wx.GetTranslation
> The app now responds to print _("stuff") but the stock buttons are  
> still all
> in English.
>  I don't think the wx.Locale stuff is doing anything at all - the  
> gettext
> stuff is just working via _
>
>
>> Also be sure that the wxLocale object remains alive during the
>> runtime of your program.
> How could I check? And what kills it?
>

By stays alive I meant it should be kept somewhere where it doesn't  
get cleaned up (i.e created as a temporary object in function call  
somewhere) until the program is finished.

I usually create the Locale object inside of a main method:

def Main():
        app = wx.App()
        mylocale = wx.Locale(xxxx)
        ...
        app.MainLoop()

if __name__ == '__main__':
        Main()

The 'Main' function doesn't return until the MainLoop is finished so  
the 'mylocale' object doesn't get deleted until the program exits.

> \d
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: wxPython-users-
> [hidden email]
>

Regards,

Cody Precord


Reply | Threaded
Open this post in threaded view
|

Re: Re: wx.Locale mystery

Karsten Hilbert
In reply to this post by Donn Ingle
On Thu, Dec 20, 2007 at 02:07:23PM +0200, Donn Ingle wrote:

> > I do something like the following,
> I have just tried a 'mixture' of the two like that and it's still confusing.
>
> If I mix the two and then remove the line:
> _ = wx.GetTranslation
> The app now responds to print _("stuff") but the stock buttons are still all
> in English.
>  I don't think the wx.Locale stuff is doing anything at all - the gettext
> stuff is just working via _

In my (unicode-)wxPython programm I only ever initialize the
Python level gettext and _() all strings. With this the GUI
is translated nicely (into German) *including* stock
buttons. This is on wx2.6 and Python 2.4/2.5.

It is likely advisable to setup the locale and gettext
system *before* even importing any of the wx code such that
that code will find a pre-initialized locale in the context
of the running application (which might otherwise just be
"C" and thus in need of being set up for wx* by means of
wx-i18n functionality).

You may want to look at

        http://cvs.savannah.gnu.org/viewvc/gnumed/gnumed/client/pycommon/gmI18N.py?root=gnumed&view=log

to see how we are going about this business in GNUmed.

I don't think it's the business of wx* to setup the locale
and gettext system. (Same as I don't believe it's the job of
wx* to setup standard pathes - but, alas, the standard
library doesn't help with that.)

> > Also be sure that the wxLocale object remains alive during the
> > runtime of your program.
> How could I check? And what kills it?

Going out of scope would kill it, for one thing.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346


Reply | Threaded
Open this post in threaded view
|

Re: Re: wx.Locale mystery

jonhattan-2
In reply to this post by Donn Ingle
Donn Ingle escribió:

>> I do something like the following,
>>    
> I have just tried a 'mixture' of the two like that and it's still confusing.
>
> If I mix the two and then remove the line:
> _ = wx.GetTranslation
> The app now responds to print _("stuff") but the stock buttons are still all
> in English.
>  I don't think the wx.Locale stuff is doing anything at all - the gettext
> stuff is just working via _
>
>
>  
>> Also be sure that the wxLocale object remains alive during the
>> runtime of your program.
>>    
> How could I check? And what kills it?
>  
I do this in OnInit:

self.loc = wx.Locale(wx.LANGUAGE_SPANISH)

so the locale object is there untill the app is destroyed.

> \d
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>  


Reply | Threaded
Open this post in threaded view
|

Re: Re: wx.Locale mystery

Donn Ingle
In reply to this post by Karsten Hilbert
Karsten Hilbert wrote:

> In my (unicode-)wxPython programm I only ever initialize the
> Python level gettext and _() all strings. With this the GUI
> is translated nicely (into German) including stock
> buttons. This is on wx2.6 and Python 2.4/2.5.

Okay, I kicked all the wx.Locale stuff out. Funny thing, a moment ago
(before I remarked it out), I saw that the GenericDirCtrl I'm using was
reporting the folder names (like 'Home') is Swedish, but the stock buttons
Next and Forward have always remained English.
 Now, with the wx.Locale all gone, I am only seeing English everywhere
(except for the test where I _("Tell it speak Swedish")

BTW : my stock buttons are defined like this:
self.buttNext = wx.Button(self, wx.ID_FORWARD

What's going on? Is there some more "convincing" way one has to use to
change the locale? I am simply saying:
LANG="sv_SE.utf8"
./fp

I'm reluctant to change the locale within my KDE settings because that means
over 100mb of new downloads for all the KDE translations. This makes me
wonder whether I am not missing some kind of GTK translation files.

/d


Reply | Threaded
Open this post in threaded view
|

Re: Re: wx.Locale mystery

Donn Ingle
In reply to this post by jonhattan-2
jonhattan wrote:

> I do this in OnInit:
>
> self.loc = wx.Locale(wx.LANGUAGE_SPANISH)
>
> so the locale object is there untill the app is destroyed.
Okay - I have tested this and wx.Locale is definitely staying alive. So,
it's not that.

\d


Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: wx.Locale mystery

Stefano Bartaletti
>I was looking at the stock buttons
>control, to see if the text labels would change language but they
>steadfastly remain in English.

>I am using a few stock buttons in the app and was relying on them 'just
>working'.

Had a similar problem some time ago, I'm also on Kubuntu and I believe it's
all related to an incomplete Gnome setup (wxWidgets/wxPython is built on GTK
so some extra setup is needed)

Try installing language-pack-gnome-xx where xx is the locale you want ('se' in
your case?), I think this will do the trick.

--
Cordialmente

Stefano Bartaletti
Responsabile Software

G.Tosi Spa Tintoria

Skype account: stefano.bartaletti
ICQ contact  : 1271960

Viale dell'Industria 61
21052 Busto Arsizio (VA)

Tel. +39 0331 34 48 11
Fax  +39 0331 35 21 23


Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: wx.Locale mystery

Donn Ingle
Stefano Bartaletti wrote:
> Try installing language-pack-gnome-xx where xx is the locale you want
> ('se' in your case?), I think this will do the trick.
You are THE man! :D Thanks. I'll let users know about that too.

I tested this on the wxpython demo, not my app - because I still can't get
wx.Locale to function, but this is a big step forward.

\d


Reply | Threaded
Open this post in threaded view
|

Re: wx.Locale mystery

Donn Ingle
In reply to this post by Donn Ingle
Well,
 In follow up: I have had some success. With that great tip from Stefano
things are looking up. Here's the nutshell:

1. Ensure language-pack-gnome-XX is installed!!!!

2. Code:
## Setup wxPython to access translations : enables the stock buttons.
localedir = "fontypython/locales"
# under that is:
# af
#  LC_MESSAGES
#   wxgui.mo <-- this is the "domain"
# sv
#  LC_MESSAGES
#   wxgui.mo

# Picks this up from $LANG
langid = wx.LANGUAGE_DEFAULT

# Seems (I hope) I don't need this
#os.environ['LANG'] = "sv_SE.utf8"
 
mylocale = wx.Locale( langid )

# I don't seem to need these. Why?
#mylocale.AddCatalogLookupPathPrefix( localedir )
#mylocale.AddCatalog( "wxgui" ) # it finds this file.
#_ = wx.GetTranslation # this clashes badly with gettext

# This refuses to work at all, ever.
#print "GetTranslation::"
#print wx.GetTranslation("Delete Pog")

# Now do this all again, so that gettext is in the house.
# VITAL :: doing this *after* the wx.Locale stuff
#  gets _("this") to work again.

#Another module that sets up standard Python gettext.
import i18n
i18n.setupPlain("wxgui") # pass the "domain".

#This now works.
print _("Test me")

And stock buttons work now too.

3. Watch for catch-22's : It seems the FORWARD and BACKWARD stock buttons
are not translated into Afrikaans yet. I assume this is a Gnome/GTK
problem. They *are* working in Swedish and this confused the heck out of me
until I found other buttons that *did* work in Afrikaans, then it clicked.
 I ran the wxpython demo to see, and sure enough, those two buttons are not
translated.

Well, thanks for all your help!
\d


Reply | Threaded
Open this post in threaded view
|

Re: Re: wx.Locale mystery

Robin Dunn
Donn Ingle wrote:
> Well,
>  In follow up: I have had some success. With that great tip from Stefano
> things are looking up. Here's the nutshell:

I'd like to add a bit more info here for the record.

1. The reason that you need to keep a reference to the wx.Locale object
is because the former locale is restored when the C++ wx.Locale instance
is destroyed.  So if you are setting to a locale other than what is the
system's default you must keep that reference until the end of the
program or until you want to switch or restore locales.

2. If you have already created one wx.Locale, and want to create another
one to switch to a different language, then because of the order that
Python manages garbage collection you have to be careful how you do it.
  The problem arises when you have code like this:

        self.loc = wx.Locale(FOO1)
        ...
        self.loc = wx.Locale(FOO2)

In this case the first locale object is not destroyed until after the
2nd one is created, but since the C++ object will restore the "former"
locale when the object is destroyed then you end up with no current
locale set, and a bad pointer that will likely cause a crash when the
FOO2 object is eventually destroyed.  So instead you  need to do
something like this to give the first object a chance to be destroyed
before the 2nd one is created (and make sure you only have the single
reference to the locale object):

        self.loc = wx.Locale(FOO1)
        ...
        del self.loc
        self.loc = wx.Locale(FOO2)

3. On Linux/Unix there are a few non-obvious things that need to be done
to make it work.  First the system locales need to be set up for the
languages that you want to support, otherwise the C runtime library will
refuse to switch to the locale and the init of wx.Locale will fail.  You
can run "locale -a" to see what locales the system can support.  Second
the message catalogs for GTK need to be installed for the "native"
buttons, dialogs, etc. to get the translated text.  (It seems like there
was a 3rd item that I can't remember right now...)


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