[PyQt] Switching languages dynamically example
Hans-Peter Jansen
hpj at urpla.net
Thu Dec 9 21:13:31 GMT 2010
Dear Giovanni,
On Wednesday 08 December 2010, 16:27:12 Giovanni Bajo wrote:
> On mer, 2010-12-08 at 15:26 +0100, Hans-Peter Jansen wrote:
> > Hi,
> >
> > I found a Qt example of switching languages dynamically here:
> >
> > http://www.qtcentre.org/wiki/index.php?title=Dynamic_translation_in_Qt4_applications
> >
> > In addition to the original version, the PyQt version does try to
> > preselect the language intelligently to the current system default.
> >
> > Since it isn't completely trivial, it might be a good addition for the
> > contribution section of PyQt?
> >
> > Since the original version didn't carry any license, I've put it under
> > the DWTFYW V2 public license (http://sam.zoy.org/wtfpl/COPYING).
>
> Just for discussion...
>
> I would love a library that does this for me without me having to do
> anything to support it; writing code that moves translatable string into
> a separate function is IMO less readable because strings are useful in
> reading Qt UI construction code; also it forces to declares a lot of
> useless instance attributes like label1,label2,label3,label4, which are
> error prone to match with the correct strings, and also wastes memory
> (more so in larger applications) by causing larger __dict__ instances.
>
> I think of a library that walks the QWidget hierarchy, finds all
> standard widgets, fetches their current text, translates it, and sets it
> back into the widget.
I really don't want to disappoint you here, but if I understand your approach correctly, you immediately loose the language switchability then, since (in sane projects) context and the english text is the key to the translations.
Given, that you you have thrown away one part of the key after the first translation, you need to reorganize the whole translation infrastructure to support your approach. You will need to go through all translations at startup, and assign the objects to any translated string. Consequently, you need much more memory and cpu resources.
If you want to carry out this step at build time, you need to assign the translations of simple strings to module/line type of information, which is going to be very fragile.
What, if additional translation parameter like disambiguation, encoding, or plural forms come into play?
But I might simply have misunderstood your idea, and in no way do I grok the Qt translation system completely.
> For completely custom widgets, there could be an
> API like retranslate(), but at least standard dialogs, main windows,
> toolabars, tooltips, etc. wouldn't need such a treatment.
>
> IMO it has to be possible to do something like this, it's just that most
> people don't think it's worth it, probably :)
Probably they tried and finally gave up. ;)
Your idea is nice on the first sight, but after giving it some thoughts, many controversies appear.
Given, that designer (and pyuic) does the necessary steps anyway, and one should use this for complex UI tasks anyway, the existing approach has proven to work out at least.
OTOH, if somebody has a few resources left, it might be a good idea to make the pylupdate parser more user friendly, or rewrite it in python right away.
Pete
More information about the PyQt
mailing list