[PyQt] Translating from a function
Hans-Peter Jansen
hpj at urpla.net
Wed Sep 29 23:14:14 BST 2010
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 will
> import the translator function as an underscore so you can have text like
> _("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,
QtGui.QApplication.UnicodeUTF8,n)
pylupdate4 is written in python, hence these issues should be solvable..
I guess, Phil will happily take patches. I also noticed, that there are path
related issues, if you work across subdirs, but I never got around looking
into it (yet).
Pete
More information about the PyQt
mailing list