PDA

View Full Version : how to use tr() for QMessageBox?



gfunk
15th November 2007, 20:20
I'm displaying a simple Yes/No QMessageBox. This works fine in English. However, I want to internationalize this for French, so that it displays Oui/Non instead. Is there an easy way to change the text in the buttons for Yes/No?

marcel
15th November 2007, 20:26
T (http://doc.trolltech.com/4.3/i18n.html#produce-translations)here's no easy way, but a standard one: http://doc.trolltech.com/4.3/i18n.html#produce-translations

^NyAw^
16th November 2007, 11:04
Hi,



QMessageBox messageBox(tr("Info"),
tr("My message translated"),
QMessageBox::Question,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape, //This makes "Esc" key to Cancel the Dialog
QMessageBox::NoButton,
this);
//Set the translation text of the buttons. Here you need to have a QTranslator installed that will translate the "Yes" and "No" texts:
messageBox.setButtonText(QMessageBox::Yes,tr("Yes"));
messageBox.setButtonText(QMessageBox::Cancel,tr("Cancel"));

Byngl
17th November 2007, 10:07
What version of Qt are you using?

This should work for 4.2.2 (it does for me):


QT_TRANSLATE_NOOP("QDialogButtonBox", "&Yes");
QT_TRANSLATE_NOOP("QDialogButtonBox", "&No");

Check QDialogButtonBox.cpp for other strings to translate

bender86
17th November 2007, 12:30
You should load a Qt library translator. There are some translators in %QTDIR%\translations which translate every user visible text in Qt library. French one is incomplete, but you may modify it in linguist.



QTranslator *qtTranslator = new QTranslator;
QTranslator *appTranslator = new QTranslator;
qtTranslator->load(
QString("qt_%1").arg(lang),
QLibraryInfo::location(QLibraryInfo::TranslationsP ath)
);
appTranslator->load(...); // Load your translator

installTranslator(appTranslator);
installTranslator(qtTranslator);