Internationalization and PyQt

View: New views
6 Messages — Rating Filter:   Alert me  

Internationalization and PyQt

by Thorsten Kampe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I've "upgraded" a PyQt demo application of mine so it includes not only
a translation of the user interface in German but also in French.

My problem is that it simply works but I think it shouldn't (no don't
laugh). This is the code in my demo application (called my_application)
that does the internationalization:

# Internationalization
qtTranslator  = QtCore.QTranslator()
qtTranslator.load(':/qt.qm')
app.installTranslator(qtTranslator)

appTranslator = QtCore.QTranslator()
appTranslator.load(':/my_application.qm')
app.installTranslator(appTranslator)
#

This is the relevant snippet from my_application.qrc:

<qresource lang="de">
<file alias="my_application.qm">translation/my_application_de.qm</file>
<file alias="qt.qm">translation/qt_de.qm</file>
</qresource>
<qresource lang="fr">
<file alias="my_application.qm">translation/my_application_fr.qm</file>
<file alias="qt.qm">translation/qt_fr.qm</file>

Please note that I'm using the same alias ("my_application.qm" and
"qt.qm") for the German /and/ English tranlation resources.

Mark Summerfield suggests in his excellent book about Rapid GUI
programming to use this approach:

# Internationalization
locale = QLocale.system().name()
qtTranslator = QTranslator()
if qtTranslator.load("qt_" + locale, ":/"):
    app.installTranslator(qtTranslator)

appTranslator = QTranslator()
if appTranslator.load("imagechanger_" + locale, ":/"):
    app.installTranslator(appTranslator)
#

So why does my code (that does not query the locale and choose the right
tranlation file according to the locale) work?


Thorsten

_______________________________________________
PyQt mailing list    PyQt@...
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Re: Internationalization and PyQt

by Giovanni Bajo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On sab, 2008-10-04 at 20:31 +0200, Thorsten Kampe wrote:

> So why does my code (that does not query the locale and choose the
> right tranlation file according to the locale) work?

Why it should not? QTranslator.load() takes care of embedding the locale
name into the filename by itself (see the documentation).
--
Giovanni Bajo
Develer S.r.l.
http://www.develer.com


_______________________________________________
PyQt mailing list    PyQt@...
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Re: Internationalization and PyQt

by Thorsten Kampe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

* Giovanni Bajo (Sun, 05 Oct 2008 12:46:15 +0200)
> On sab, 2008-10-04 at 20:31 +0200, Thorsten Kampe wrote:
> > So why does my code (that does not query the locale and choose the
> > right tranlation file according to the locale) work?
>
> Why it should not? QTranslator.load() takes care of embedding the
> locale name into the filename by itself (see the documentation).

If the author of a PyQt book writes code that explicitly queries the
locale and uses that to load the actual translation then this should be
reason enough to believe that QTranslator doesn't.

Thorsten

_______________________________________________
PyQt mailing list    PyQt@...
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Re: Internationalization and PyQt

by Thorsten Kampe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

* Giovanni Bajo (Sun, 05 Oct 2008 12:46:15 +0200)
> On sab, 2008-10-04 at 20:31 +0200, Thorsten Kampe wrote:
> > So why does my code (that does not query the locale and choose the
> > right tranlation file according to the locale) work?
>
> Why it should not? QTranslator.load() takes care of embedding the
> locale name into the filename by itself (see the documentation).

Okay, I read the documentation to QTranslator.load(). It does actually
nothing of what you claim. To the contrary: it /strips/ the locale name
from the file name (if it can't find it). It appends the /suffix/ (.qm)
which again has nothing to do with what I asked.

Thorsten

_______________________________________________
PyQt mailing list    PyQt@...
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Re: Re: Internationalization and PyQt

by Giovanni Bajo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On dom, 2008-10-05 at 18:19 +0200, Thorsten Kampe wrote:

> * Giovanni Bajo (Sun, 05 Oct 2008 12:46:15 +0200)
> > On sab, 2008-10-04 at 20:31 +0200, Thorsten Kampe wrote:
> > > So why does my code (that does not query the locale and choose the
> > > right tranlation file according to the locale) work?
> >
> > Why it should not? QTranslator.load() takes care of embedding the
> > locale name into the filename by itself (see the documentation).
>
> Okay, I read the documentation to QTranslator.load(). It does actually
> nothing of what you claim. To the contrary: it /strips/ the locale name
> from the file name (if it can't find it). It appends the /suffix/ (.qm)
> which again has nothing to do with what I asked.

Sorry, I stand corrected.

Anyway, it looks like your code works equally well because you demand to
the Qt Resource system to choose the right file based on the current
locale. It might well be that Mark's example didn't use the same trick
(maybe his resources weren't using the same aliased name but the raw
filenames).

Does this answer your question?
--
Giovanni Bajo
Develer S.r.l.
http://www.develer.com


_______________________________________________
PyQt mailing list    PyQt@...
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Re: Re: Internationalization and PyQt

by Thorsten Kampe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

* Giovanni Bajo (Sun, 05 Oct 2008 19:24:17 +0200)

> On dom, 2008-10-05 at 18:19 +0200, Thorsten Kampe wrote:
> > * Giovanni Bajo (Sun, 05 Oct 2008 12:46:15 +0200)
> > > On sab, 2008-10-04 at 20:31 +0200, Thorsten Kampe wrote:
> > > > So why does my code (that does not query the locale and choose
> > > > the right tranlation file according to the locale) work?
> > >
> > > Why it should not? QTranslator.load() takes care of embedding the
> > > locale name into the filename by itself (see the documentation).
> >
> > Okay, I read the documentation to QTranslator.load(). It does
> > actually nothing of what you claim. To the contrary: it /strips/ the
> > locale name from the file name (if it can't find it). It appends the
> > /suffix/ (.qm) which again has nothing to do with what I asked.
>
> Sorry, I stand corrected.
>
> Anyway, it looks like your code works equally well because you demand
> to the Qt Resource system to choose the right file based on the
> current locale. It might well be that Mark's example didn't use the
> same trick (maybe his resources weren't using the same aliased name
> but the raw filenames).
>
> Does this answer your question?

Yes, thanks.

Thanks.

_______________________________________________
PyQt mailing list    PyQt@...
http://www.riverbankcomputing.com/mailman/listinfo/pyqt
LightInTheBox - Buy quality products at wholesale price!