[PyQt] i18n

paolo at paolodestefani.it paolo at paolodestefani.it
Mon Feb 27 20:16:55 GMT 2012

I found in the  mailing list archive this old post. As i have the same
problem is there anything new about this ? 
I want to use a short form for QCoreApplication.translate("MyContext", "My
String") but if i set:

tr = QCoreApplication.translate(context, string)

and use tr("MyContext", "My String") in the generated .ts files i see a
wrong "@default" context and confusing strings. The same happens if i use

On Wednesday 29 September 2010, 22:39:44 Eric Frederich wrote:
> After trying a bunch of different ways I finally got the translate
> function to work.
> I am now having a hard time trying to shorten it up.
> This ...
> QCoreApplication.translate("MyContext", "My String")
> ... is a bit much.
> Is there any way to shorten it?
> I forget where I have seen it, but I think other translation schemes
> import the translator function as an underscore so you can have text
> _("my String")
> Is there any way to shorten it?
> I run into problems with either pylupdate4 not picking it up, or into
> problems with linguist.
> If I do...
> from PyQt4.QtCore import QCoreApplication
> mytr = QCoreApplication.translate
> ... then pylupdate4 doesn't pick them up.
> If I try to do something like...
> from functools import partial
> translate = partial(QCoreApplication.translate, "MyContext")
> ... then linguist doesn't pick up the right context.
> Any help is appreciated.
> I do have it working now, but I don't like how long this is...
> QCoreApplication.translate("MyContext", "My String")

You can always define a helper method like this:

    def tr(self, msg, ctx = None, n = -1):
        return QtGui.QApplication.translate("YourForm", msg, ctx,
               QtGui.QApplication.UnicodeUTF8, n)

Unfortunately, with a module global helper function, which would get us en

par with C++ at least on module level, all strings are collected in a 
context "@default":

def tr(msg, ctx = None, n = -1):
    return QtGui.QApplication.translate("YourForm", msg, ctx,

pylupdate4 is written in python, hence these issues should be solvable..
I guess, Phil will happily take patches. I also noticed, that there are
related issues, if you work across subdirs, but I never got around looking

into it (yet).

PyQt mailing list    PyQt at riverbankcomputing.com

More information about the PyQt mailing list