Solved translation of button texts
-
What does that file contain ?
Can you show the two main.cpp you have now ?
-
Hi!
Actually the file contains as good as nothing:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1" language="testGermanQt_de_DE"></TS>
The main of the sample app looks like this
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTranslator trans; QString translationFile = QDir::currentPath() + "/translations/qt_de.qm"; if( trans.load(translationFile)) if( QCoreApplication::installTranslator(&trans)) { qInfo() << "Successfully installed language file " << translationFile; } else { qCritical() << "failed to install translations " << translationFile; } else qCritical() << "failed to load translations " << translationFile; MainWindow w; w.show(); return a.exec(); }
My own main.cpp is similar, with some app specific extra stuff:
int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setOrganizationName(qsl("xxx")); // used to store our settings a.setApplicationName(qsl("xxx")); QLocale locale(QLocale::German, QLocale::LatinScript, QLocale::Germany); QLocale::setDefault(locale); // do before starting the event loop setGermanUi(); initLogging(); LOG_CALL; qInfo() << "DKV2 started " << QDate::currentDate().toString(qsl("dd.MM.yyyy")) << qsl("-") << QTime::currentTime().toString(); init_DKDBStruct(); // let propose a db to mainwindow appConfig::setCurrentDb(getInitialDbFile()); #ifndef QT_DEBUG QSplashScreen* splash = doSplash(); // do only AFTER having an app. object splash->show(); QTimer::singleShot(3500, splash, &QWidget::close); #endif MainWindow w; w.show(); int ret = a.exec(); qInfo() << "app finished"; return ret; }
Do you have a glue?
Thx for your efforts and best regards
Holger
PS: setGermanUi() does the QTranslation stuff that is also in the first main.cpp
PPS: I now found, that the sample app, that show the right button texts for the "yes no" message box shows englisch buttons for the file open dialog. That is pretty strange as my system is german ... -
@JonB Thx for your reply. As described I have a working program and one where it does not. And I fail to see the difference. In both cases it is de_de - german for germany. :) this should be easier then the case from your link ;)
regards
Holger -
How do you expect that something gets translated when the ts file does not contain any translations at all?
-
@Christian-Ehrlicher You missed one thing: he is loading Qt's language file. The empty file here has no current role.
@HoMa said in translation of button texts:
QString translationFile = QDir::currentPath() + qsl("/translations/qt_de.qm");
I just noticed: what is that
qsl
? It looks like the main difference between the example that is working and your own application. -
@SGaist said in translation of button texts:
The empty file here has no current role.
Are we sure he loads the correct qt language file and not the one he posted?
-
@SGaist Sorry for not explaining or removing it. It is just a QStringLiteral makro... as the load is successfull this is probalby no problem.
Best regards
Holger -
@Christian-Ehrlicher The qm file comes with Qt: it is originally called qtbase_de.qm, I guess ...
-
How big is the qm file? Are you sure it contains any translations?
-
@Christian-Ehrlicher I had the same Idea ;) it is a bit more then 200k - and it is the same that you get when building Qt\5.15.1\Src\qttranslations in the distribution.
Best regards
Holger -
Ok, just wanted to be sure.
I would suggest you to create a small test example which loads the qm file from disk providing a full path. Then see if it works. A simple main.cpp would be enough.
Did you build Qt on your own? -
@Christian-Ehrlicher I actually did exactly that. I create an empty widget project, added two buttons to the form. In one I opened a Yes No Message box and found the texts to be OK (ja/nein). However: the file open dialog was a disapointment: this also has englisch texts. No Idea where to go from here...
best regards
Holger -
The file open dialog comes from the os (when you use the static versions) - there is nothing Qt can do against it.
-
@HoMa
I was going to say similar to @Christian-Ehrlicher : I don't think you've ever shown which call you are making for " the file open dialog " ? Though even the static ones seem to depend on which OS, which I don't think you've said either?
https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileNameOn Windows, and macOS, this static function will use the native file dialog and not a QFileDialog.
Depending on which call you want to use, you could try the corresponding Qt dialog, and see what you think of it, with a
QFileDialog
instance andQFileDialog::DontUseNativeDialog
set?EDIT I happened just now to come across https://forum.qt.io/topic/74854/change-qfiledialog-buttons-text/9 :
BTW: changing the cancel button works (on WIndows) if you do not use the native dialog:
OpenFile.setOption(QFileDialog::DontUseNativeDialog);
OpenFile.setLabelText(QFileDialog::Reject, tr("Cancell change"));
-
This post is deleted! -
@JonB Thx for your answer - but I can not get anything from your links. I only get an error message
-
@HoMa
This forum has added the:
I typed to the end of the Url :(
It should be https://forum.qt.io/topic/74854/change-qfiledialog-buttons-text/9, I will correct it now.
The other links should be fine. -
Now - which calls I expect to be translated ...
I would like this message box to have "ja" and "nein" :
QMessageBox::question(this, "Titel", "Eine Nachricht");
And it would be even better if this was german, too:
QString dir(getUserData(keyOutdir, QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation))); dir = QFileDialog::getExistingDirectory(parent, qsl("Ausgabeverzeichnis"), dir, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
-
Please start with this basic example
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTranslator t; if (!t.load("qt_de.qm", "C:/Qt5/5.15.1/mingw81_64/translations/")) return -1; a.installTranslator(&t); QMessageBox::question(nullptr, t.translate("QMessageBox", "Show Details..."), t.translate("QMessageBox", "Hide Details...")); return a.exec(); }
-
@Christian-Ehrlicher said in translation of button texts:
QMessageBox::question(nullptr, t.translate("QMessageBox", "Show Details..."), t.translate("QMessageBox", "Hide Details..."));
Hi Christian! Wow - this made a difference. And I have to admit that I don't know why. With the call from you
QMessageBox::question(nullptr, t.translate("QMessageBox", "Show Details..."), t.translate("QMessageBox", "Hide Details..."));
I get not only a german title and message, but also the buttons are in german - that's great!
However - when I callQMessageBox::question(this, "Titel", "Eine Nachricht");
in a different function the buttons are yes and no! Does QMessageBox need QTranslator be in scope or ...
So you solved it - kind of - and my msg box is still not in german ... should I laugh or cry ;) ?
Anyhow: that was a great step forward - thank you. I would appreciate if you have additional thoughts...
regards
HolgerPS: I played around and with QTranslator being a global object all messageboxes say "ja" and "nein" ...