[PyQt] Translation program

Jeremy Sanders jeremy at jeremysanders.net
Sat Jun 2 18:50:21 BST 2012


I've written a program to find translation strings for PyQt. It uses 
python's ast module to interpret python source code. It works a bit 
differently to pylupdate4, as it looks for functions with a fixed 
contexts to do the translation.

If you'd like to try it out, it is available here:
https://github.com/jeremysanders/veusz/blob/master/scripts/pyqt_find_translatable
Click raw to get the real source code. It requires lxml and argparse 
(which comes with Python 2.7+).

There is some help in the source. It basically looks for functions which 
have disambiguation and context as optional arguments.

def trans(text, disambiguation=None, context='trans_context'):
    return QCoreApplication.translate(context, text,
                                      disambiguation=disambiguation)


After the definition in your source code (the name "_" works!) you can 
then call

trans("text")
trans("other text", "some disambiguation")
trans("more text", context="other_context")

To run the command line looks like:
  pyqt_find_translatable --output=file.ts --verbose *.py *.ui

It will write a new .ts file, or update an existing one, as appropriate. 
It will mark strings as obsolete if they do not exist in a currently 
processed files.

Please give any feedback and patches! One downside is that the spacing 
and format are a bit different to the default from linguist or pylupdate.

Jeremy



More information about the PyQt mailing list