Problem with QTranslator (by GNU gettext user)

c.buhtz at posteo.jp c.buhtz at posteo.jp
Fri Jun 16 20:46:15 BST 2023


x-Post: 
https://forum.qt.io/topic/145827/problem-with-qtranslator-by-gnu-gettext-user

Hello,

usually I do use GNU gettext to localize my Python applications. Today I 
realized that Qt (PyQt) do not localize; e.g. Yes/No buttons.

I found code using QTranslator and tried it out. But the buttons in my 
test application are not translated (to German). I checked the docu but 
I don't understand the background of it.

I assume that Qt do need a po/mo file. But how do I know where it is? I 
assume it was installed while I installed Qt. Shouldn't Qt know for 
itself where to find its translation and selected it by default based on 
the current locale?

Here is my minimal (not so) working Python example.

#!/usr/bin/env python3
from PyQt5.QtWidgets import QApplication, QDialogButtonBox
from PyQt5.QtCore import QTranslator, QLocale, QLibraryInfo

def get_translator():
     translator = QTranslator()
     locale = QLocale.system().name()
     print(f'{locale}')  # <-- de_DE

     translator.load('qt_%s' % locale,
                     
QLibraryInfo.location(QLibraryInfo.TranslationsPath))

     return translator

if __name__ == '__main__':
     app = QApplication([])
     t = get_translator()
     app.installTranslator(t)

     buttonBox = QDialogButtonBox(
         QDialogButtonBox.Ok
         | QDialogButtonBox.Cancel
         | QDialogButtonBox.Yes
         | QDialogButtonBox.No
     )

     buttonBox.show()
     app.exec()


More information about the PyQt mailing list