Translation of QGIS Plugins

We have begun to realize that there are parts of the QGIS GUI that are still in English, even though we have translated a large amount of the GUI now (up to 82% at time of writing). Of course these parts of the GUI stand out like a sore thumb when the rest of the GUI is in Norwegian. These strings come from elements that are not part of the core QGIS code – mainly plugins.

It is absolutely fantastic that whoever writes a QGIS plugin then decides to release the plugin into the main QGIS plugin repository so others can make use of it. I am a strong believer that if the plugin you write saves you time, there is bound to be someone else, somewhere, who will benefit from it as well.

Plugins are often coded to achieve a certain small task, and understandably, often do not take into consideration translations. It can be complicated to understand what you need to do if it is the first python plugin you have ever written. It is also possible that, like I did when I wrote QGIS plugins when I lived in the UK, you just do not think about translations as you write your plugin.

I decided I was interested to see how one goes about translating a plugin, so what better place to start than with one of your own plugins, which is called Layers By Field (I needed to fix a bug anyway so it seemed like a good time to add a Norwegian translation to the plugin).

Here I will give an overview of what needs to be done to enable others to translate your plugin. I have used OSGeo4W for my setup.

1) Create a new plugin

When you use the Plugin Builder plugin to create the basis for a new plugin, you are provided with (nearly) everything you need to build translation into your plugin. This includes:

a) A folder called i18n – this is where the translation files live, and
b) The code to deal with the locale of your system (these lines of code are found in the __init__ method)


locale = QSettings().value("locale/userLocale")[0:2]
localePath = os.path.join(self.plugin_dir, 'i18n', 'tfdf_{}.qm'.format(locale))

if os.path.exists(localePath):
 self.translator = QTranslator()
 self.translator.load(localePath)

 if qVersion() > '4.3.3':
  QCoreApplication.installTranslator(self.translator)

2) Prepare the strings that need translating

In your plugin code, you can prepare things by calling the QCoreApplication.translate method every time you write a string that will be in the user interface. This takes two arguments.

1) the context that the string should be added to, and
2) the string that you want to be available for translation.

To add a menu item into the Vector menu i QGIS for my Split Layers By Field plugin, it looks something like this:

self.iface.addPluginToVectorMenu(QCoreApplication.translate('menu_items', "Split Layers By Field"), self.actionRun)

The source text “Split Layers By Field” will be added to The to the context menu_items in the .ts file.

3) Create a .pro file

The only thing missing that the plugin builder doesn’t create is a .pro file. You need to create a .pro file in the i18n folder. This file has the following format:

FORMS = ../layersbyfielddialogbase.ui

SOURCES = ../layers_by_field.py \
../layers_by_field_dialog.py \
../layersbyfielddialogbase.py

TRANSLATIONS = layers_by_field_nb.ts

The .pro file states which of your python and ui files contain translatable text. FORMS are the .ui files, SOURCES are you python code files (.py) are TRANSLATIONS are the names of the outputted .ts files (in this case just the Norwegian .ts file).

4) Create the .ts file using Pylupdate

To create a .ts file that you can translate in Qt Linguist you need to run a command from command line using Pylupdate. In my case, I have used the OSGeo4W command line. The command I used was:

Pylupdate4 –noobsolete layers_by_field.pro

The .ts file should be saved in the i18n folder.

5) Translate the strings using Qt Linguist

If you open the .ts file in Qt linguist, you can now go about translating the strings. I will not cover this here as its well documented already here.

In the picture below you can see the string we added into the context menu_items in point 2 above:

menu_items_context

6) “Release” the file in Qt Linguist

Once you have finished translating the strings, you can release the file (File > Release from the menu in Qt Linguist). This will create the .qm file that QGIS needs to use the translations. This should be saved into the i18n folder.

Below you can see the two different languages in use in QGIS.

english_version

norsk_versjon

I am guessing it will be a very difficult job to translate all QGIS plugins, but we have decided that we would like to try with the main ones. Plugins are written by so many different authors, and it’s possible that not all authors have written the plugins with translation in mind.

Advertisements

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: