views:

396

answers:

3

hi. 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.

A: 

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.

swanson
A: 

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 ??

WTF ! i can't comment on swanson answer !

solsTiCe
Ah it looks like you could possibly solve the problem by adding you own Cancel and Ok buttons instead of using `setStandardButtons()`
swanson
yes. I resist to use that work-around. because those buttons are swapped places when the platform changes. but it seems it is my only option.
solsTiCe
A: 

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.

solsTiCe
so i manually merge part of qt_fr.ts file into my ts file. sigh...how this is possible ? noone use this or what ? at least in Qt.
solsTiCe
after filling a bug report for this http://bugreports.qt.nokia.com/browse/QTBUG-8798 , i finally got it working the way it should:"a QTranslator is a representation of exactly one TS file. you need to install multiple translators into the QApplication object. the FAQ entry is just wrong and will be fixed."
solsTiCe