Translate standardButton in QDialogButtonBox
i am using pyqt and designer. i have translated all the strings in my app with self.tr() + pylupdate4 and lrelease
here is the snippet of code in my main():
app = QtGui.QApplication(sys.argv) app.setApplicationName('Mental Calculation') # initialize locale and load translation files if available locale = QtCore.QLocale() LOCALENAME = str(locale.system().name()) translator = QtCore.QTranslator() translator.load("mentalcalculation_%s" % LOCALENAME) app.installTranslator(translator)
I use a QDialogButtonBox in a QDialog with a QtGui.QDialogButtonBox.Cancel and a QtGui.QDialogButtonBox.Ok
and the strings in these buttons are not translated. because pylupdate4 does pick any string for them.
Have I missed a configuration step in my app so that they are translated ? I don't understand how the string for standard buttons of QDialogButtonBox are supposed to be translated and can't found doc about that.
Use the method below to set the buttons' text:
then lupdate .ts file, open it with linguist and you'll find the strings.
I finally found what to do: from http://qt.nokia.com/developer/faqs/faq.2008-05-06.6265952148 and http://qt.nokia.com/developer/faqs/705
so I just need to copy, for example french, the qt_fr.qm I found in QTDIR/translations (here /usr/share/qt/translations) into the directory of my application and add
translator.load("qt_%s" % LOCALENAME)
or even copy all the qt_*.qm file from QTDIR/translations to really support the maximum of locales.
NO THIS IS NOT WORKING. Only one of the 2 files is loading. so i can't have either my string translated or the QDailogButtonBox.
damn. this thing is getting in my nervS.
you should install 2 different translators:
app = QtGui.QApplication(sys.argv) translator_my = QtCore.QTranslator() translator_my.load('i18n/i18n_' + QtCore.QLocale.system().name() + '.qm') #translator_my.load('i18n/i18n_ru_Ru.qm') app.installTranslator(translator_my) translator_qt = QtCore.QTranslator() translator_qt.load('i18n/qt_' + QtCore.QLocale.system().name()[:2] + '.qm') #translator_qt.load('i18n/qt_ru.qm') app.installTranslator(translator_qt) myApp = MyMainWindow() myApp.show() sys.exit(app.exec_())
where i18n/i18n_ru_Ru.qm is the path to your i18n file and qt_ru.qm was copied from usr/share/qt4/translations (in my case).
In order to load the native translations from QT in a portable way, you could use the following function call:
translator.load("qt_" + QLocale::system().name(),QLibraryInfo::location(QLibraryInfo::TranslationsPath));
The two (or more) translators as explained before, is the standard way if you would like to avoid the workaround.
From the docs, you must create the translator object before the application widgets. I can't be certain that is your problem since you omitted the code where your widgets are created, but make sure you ensure you are not creating them before the translator.
yes. just below the snippet I gave, I create my main dialog.
The problem is Qtranslator could not translate those QDialogButtonBox button because there is no string to translate. so it must be qt that does that job internally. or with some mechanism I am not aware of.
here is another snippet of code generated by pyuic4
self.buttonBox = QtGui.QDialogButtonBox(Dialog) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") self.gridLayout.addWidget(self.buttonBox, 8, 0, 1, 1)
How QtGui.QDialogButtonBox.Cancel string could be translated when there is no string in the code ???
Is it because I do not create a mainwindow and only use QDialog ??
I can't comment on swanson answer !