1

I am running a standalone PyQGIS application. Using QgsMessageBar I noticed that it is not using the local translation.

Consider this minimal example running in a OSGeo4W environment (using all the right environment variables) on a German Windows machine. Ignore how broken it might be in terms of Qt structure, I stripped it down as much as possible to have a minimal example that shows the issue.

from qgis.core import QgsApplication, Qgis
from qgis.gui import QgsMessageBar

app = QgsApplication([], True)
app.initQgis()

print(app.locale())
# "de" = correct

print(app.i18nPath())
# "C:/Users/user/path/apps/qgis-ltr/./i18n/"
# "C:\Users\user\path\apps\qgis-ltr\i18n\" contains "qgis_de.qm"
# seems correct

message_bar = QgsMessageBar()
message_bar.pushMessage("Locale", f"{app.locale()}", level=Qgis.Critical)
# Bug: Mouseover on the X icon of the message says "Close", not the german "Schließen"

app.exec()

This results in a tiny window like this:

enter image description here

If I run the QGIS of that environment normally, the GUI is in German and a QgsMessageBar's close button does say "Schließen".

As you can see in the code above, I checked the locale and path to the translation file(s) and it all seems correct.

How do I get the available translation to work in my standalone application?

2
  • What this gives you QSettings().value('locale/userLocale', QLocale().name()) ?
    – Taras
    Commented Nov 9, 2022 at 10:48
  • That gives me de_DE Commented Nov 9, 2022 at 11:03

1 Answer 1

1

You need to set up and install a QTranslator:

from qgis.PyQt.QtCore import QTranslator

translator = QTranslator(app)
translator.load(f"{app.i18nPath()}qgis_{app.locale()}.qm")
app.installTranslator(translator)

This will fail silently if the specified translation file could not be loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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