Change Language in QT StandardDialogs (QColorDialog)

345 Views Asked by At

I've tried to change the Language of a QColorDialog from English to German but unfortunately it's not working. I am using PyQt.

What I've tried so far:

Setting the locale for the Dialog:

colordialog = QtWidgets.QColorDialog()
colordialog.setLocale(QtCore.QLocale(QtCore.QLocale.German, QtCore.QLocale.Germany))

Installing a translator for the whole application:

app = QtWidgets.QApplication(sys.argv)
qt_translator = QtCore.QTranslator()
qt_translator.load("qt_" + QtCore.QLocale.system().name(),
QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath))
app.installTranslator(qt_translator)
1

There are 1 best solutions below

10
On BEST ANSWER

If you want to translate texts to German, you need to install a translator for German, not the system locale. Also the QColorDialog() shows the system color dialog if you don't specify the DontUseNativeDialog option. So do like this for "Qt for Python"(PySide2).

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

app = QApplication([])
trans = QTranslator()
trans.load('qt_de',
    QLibraryInfo.location(QLibraryInfo.TranslationsPath))
QCoreApplication.installTranslator(trans)

dlg = QColorDialog(None, options = QColorDialog.DontUseNativeDialog)
dlg.exec()

Of course, you can use the name of the system locale instead of hard-coding the locale like this, as you did, if you want to use the system locale.

trans.load('qt_' + QLocale.system().name(),
    QLibraryInfo.location(QLibraryInfo.TranslationsPath))

For PyQt5, do like this.

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

app = QApplication([])
trans = QTranslator()
trans.load('qt_de',
    QLibraryInfo.location(QLibraryInfo.TranslationsPath))
QCoreApplication.installTranslator(trans)

dlg = QColorDialog(None)
dlg.setOptions(QColorDialog.DontUseNativeDialog)
dlg.exec()

But the current version(5.15.9) has is a bug that the qtscript_*.qm files are missing. So you should manually copy files from other Qt distribution. The following is an example of copying files from PySide2 in my Linux Anaconda environment.

$ cp ~/miniconda3/lib/python3.8/site-packages/PySide2/Qt/translations/qtscript*.qm ~/miniconda3/lib/python3.8/site-packages/PyQt5/Qt5/translations

For PyQt6, the above bug was fixed and you can do like this.

from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *

app = QApplication([])
trans = QTranslator()
trans.load('qt_de',
    QLibraryInfo.path(QLibraryInfo.LibraryPath.TranslationsPath))
QCoreApplication.installTranslator(trans)

dlg = QColorDialog(None, options = QColorDialog.ColorDialogOption.DontUseNativeDialog)
dlg.exec()