[Qgis-developer] python plugin translations

Borys Jurgiel borysiasty at aster.pl
Thu Apr 23 19:10:24 EDT 2009


Thursday 23 of April 2009 18:32:04 Alex Mandel napisał(a):

> I asked briefly on IRC the other day if there was a "how to" for
> including translation strings in a plugin, but no one seemed to know of
> such a thing. I'd like to allow for translation of my plugin but I'm not
> sure where to start.

Hi, 
I'm not sure if it makes any sense because I'm extremely sleepy, but here is 
what I've just written to Maxim. Note that I never did it, but it should work.

------------------------------------

There are two different approaches:

A. For external plugins, you can translate them 'locally': all the translation 
stuff is inside the plugin directory and it links to qgis in the runtime. It 
doesn't affect the main qgis .ts files, so the plugin maintainer has to keep an 
eye on the translations and ask the translators.

Unfortunately I didn't ever try it.  At the time there is one plugin 
implementing such internalization. It's Horst's Geoprocessing. Use the code as 
an example. In general, you need to:

1) Pass all strings through the QCoreApplication.translate("context","text") 
static method. Maybe you can use just self.tr("text") instead, but I'm not sure 
whether it works in this case - it may be not able to pass the strings to the 
application. Please pay attention for a text context. It's very hard for 
translators to deal with text with enigmatic context. Usually the context is 
just the name of the class where the string is used. So it's very important to 
give classes a 'translator-friendly' ;) names, like "QgsReprojectRasterDialog" 
instead of just a "Dialog".

2) Create a .pro file and run pylupdate, which catches all the tr() parameters 
from both .py and .ui files (given in the .pro file) and create all the local 
.ts files. Translate the .ts files and compile them to .qm

3) Init and connect the QTranslator() object - look at the begining of Horst's 
geoprocessing.py file.



B. For 'internal' plugins, like fTools placed in svn, it's much simpler. You 
just need to use the self.tr() or QCoreApplication.translate() methods in .py 
files. All such strings in .py and .ui will be catched by the 
qgis/update_ts_files.sh script and put to the main qgis .ts files.



More information about the Qgis-developer mailing list