[Qgis-developer] Plugin Translation

Alex Mandel tech_dev at wildintellect.com
Mon Apr 27 22:36:24 EDT 2009


That's exactly the information I needed. Based on it I made a generic
function that will work for any plugins who want to implement
translations. All you need to do is call the function in your plugin
init passing it the self object, plugin path(which I do dynamically),
and the name of your plugin directory.
http://apps.sourceforge.net/trac/qgismetaedit/browser/trunk/translate.py

Translation files need to be in a i18n(number 18) folder and labeled
like pluginname_de.ts
http://apps.sourceforge.net/trac/qgismetaedit/wiki/Translations

I played with the --lang option which is great but it doesn't apply to
plugins. Specifically looking at main.cpp
"
myTranslationCode = QLocale::system().name();
464    //setting the locale/userLocale when the --lang= option is not
set will allow third party
465    //plugins to always use the same locale as the QGIS, otherwise
they can be out of sync
466    mySettings.setValue( "locale/userLocale", myTranslationCode );"

I was wondering if the mySettings is available via the python api so
that it can be checked by my new function. In fact changing the setting
in QGIS doesn't work either for plugins(Horst's or mine) since we don't
know how to detect the QGIS setting and only rely on the system setting
for now.

Last, anyone have a list of language codes to use. Maybe what qt expects
or what people.

Thanks,
Alex

PS: If you run a German login you can test our metaedit translation,
requires svn checkout for now.

Borys Jurgiel wrote:
> Monday 27 of April 2009 07:07:07 Alex Mandel napisał(a):
>> I think I'm most of the way to getting a translation done on the
>> metaedit plugin(added some other features too) but I wanted to check on
>> how QGIS knows to load a particular language. Do I have to add code to
>> my plugin or will simply having the .qm files and the .pro file do it?
> 
> I'm not sure I understand you properly. As I've written, you need to set a 
> QTranslator in your code. In Geoprocessing Plugin, Horst did it in that way: 
> 
>     userPluginPath = 
> QFileInfo(QgsApplication.qgisUserDbFilePath()).path()+"/python/plugins/geoprocessing"
>     systemPluginPath = 
> QgsApplication.prefixPath()+"/share/qgis/python/plugins/geoprocessing"
>     myLocaleName = QLocale.system().name()
>     myLocale = myLocaleName[0:2]
> 
>     if QFileInfo(userPluginPath).exists():
>       pluginPath = userPluginPath+"/i18n/geoprocessing_"+myLocale+".qm"
>     elif QFileInfo(systemPluginPath).exists():
>       pluginPath = systemPluginPath+"/i18n/geoprocessing_"+myLocale+".qm"
> 
>     self.localePath = pluginPath
>     self.translator = QTranslator()
>     self.translator.load(self.localePath)
> 
> 
> Of course you need also to use the QCoreApplication.translate(context,string) 
> for every translated string.
> 
> The .pro file is ONLY for creating/updating the .ts files with pylupdate4. Then 
> you can compile the files to .qm with lrelease-qt4.
> 
>> Also is there any way to test alternate languages in qgis without
>> modifying the computer's locale setting. ie: a menu or an api call via
>> python console that would just switch the language? maybe a command line
>> option?
> 
> You need to either run qgis with a paramater --lang , e.g. qgis --lang de
> or set it in the Options dialog and restart qgis
> 
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer




More information about the Qgis-developer mailing list