I have interaction with layer defined by custom designed UIs. In layer properties the following settings: Edit UI - customline.ui and Python Init function: customline_set.py.

#coding: utf8
from PyQt4.QtCore import *
from PyQt4.QtGui import *

nameField = None
myDialog = None

def formOpen(dialog,layerid,featureid):
    global myDialog
    myDialog = dialog
    global nameField

    translator = QTranslator()
    loc = QSettings().value('locale/userLocale')[0:2]
    print loc
    filename = "\\i18n\\customline_%s.qm" % loc
    print filename

    print QCoreApplication.translate('editForm',"Generals")
    # return translated string ok

The code work without error but the UI it's not displayed in selected language.

Can anyone point me in the direction of the mistake here?


i can't detect an error on sight but maybe this helps:

On some systems the user locale is not available and your loc-variable will be NULL, then your code will trow an error.

I use the the following code in my plugins:

    locale = QtCore.QSettings().value("locale/userLocale")
    if not locale: locale == 'en' 
    else: locale = locale[0:2]
    localePath = os.path.join(os.path.dirname(__file__), 'i18n', 
    if os.path.exists(localePath):
        self.translator = QtCore.QTranslator()
        if QtCore.qVersion() > '4.3.3': 
| improve this answer | |
  • it's ok, but not this is the problem. Tha translator loading it's ok and functional (print QCoreApplication.translate('editForm',"Generals") - it's working) – catalin.proteasa Dec 10 '14 at 5:04

A bit late, but it might help other people. I had the same issue (python plugin for QGIS, QCoreApplication.translate() giving the correct result but no translation showing in QGIS itself).

In the end, I took example from the Table Manager plugin. I created a MyPlugin.pro file which identifies all the files containing text that should be translated (in my case, .ui and .py files).

Then I used the command pylupdate4 MyPlugin.pro, which generates (or updates) your .ts files. .ts files can then be manually edited to add translations (it's XML-like format); it is also possible to edit them using QT 4 Linguist, which provides a GUI to properly handle QT 4 translations.

After that, don't forget to use lrelease MyPlugin_locale.ts to generate the corresponding .qm file which is actually used.

Note that I work on Ubuntu 12.04. The command pylupdate4 may be different on other systems.

Relevant link that helped me : https://stackoverflow.com/questions/18921213/translation-file-still-working-after-modifying-the-source

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.