Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. translation of button texts
Forum Updated to NodeBB v4.3 + New Features

translation of button texts

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 4 Posters 4.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HoMaH Offline
    HoMaH Offline
    HoMa
    wrote on last edited by
    #1

    Hi there and thanks for your time.

    I codes this very early in my main. function:

    QTranslator trans;
    QString translationFile = QDir::currentPath() + qsl("/translations/qt_de.qm");
    if( trans.load(translationFile))
        if( QCoreApplication::installTranslator(&trans)) {
            qInfo() << "Successfully installed language file " << translationFile;
            return;
        }
    qCritical() << "failed to load translations " << translationFile;
    

    and expected, that Message box buttons and wizard buttons should be in german. But they are not. What did I wrong?
    (the program logs support, of course ...)

    Best regards
    Holger

    1 Reply Last reply
    0
    • HoMaH Offline
      HoMaH Offline
      HoMa
      wrote on last edited by HoMa
      #27

      Hi all - and thanks a lot to all who wanted to help.

      I write this as a final note, because I consider this as solved. My solution looks now like this:

      QTranslator* pTrans = new QTranslator();
          if( pTrans->load(translationFile, path))
              if( QCoreApplication::installTranslator(pTrans)) {
                  qInfo() << "Successfully installed language file " << translationFile;
                  return;
              }
          qCritical() << "failed to load translations " << translationFile;
      

      So the QTranslator, which is "installed" into the application does never run out of scope. One might consider this a leak - but as I do not intend to offer a switch of language I guess for me this is OK.
      So the take away for everyone who has similar problems: let your QTranslate survive, then it will work for you!

      Best regards
      Holger

      PS: This will translate MessageBox buttons as well as Wizard buttons. It does not work for File dialogs which are provided from the OS.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Can you show the code you use where the text is not translated ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • HoMaH Offline
          HoMaH Offline
          HoMa
          wrote on last edited by
          #3

          Well, showing a QMessageBox or a QWizard has no ui language specific code, right? So anyhow my expectation was that there is „abbrechen“ instead of „cancel“.

          But if you think it could help I will post some code tomorrow.
          Thx and regards
          Holger

          JonBJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            A minimal buildable example is always a good idea.

            You should also add the Qt version you are using as well as OS you are running.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            HoMaH 1 Reply Last reply
            0
            • HoMaH HoMa

              Well, showing a QMessageBox or a QWizard has no ui language specific code, right? So anyhow my expectation was that there is „abbrechen“ instead of „cancel“.

              But if you think it could help I will post some code tomorrow.
              Thx and regards
              Holger

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @HoMa
              You should read the following:
              https://stackoverflow.com/questions/31533019/how-to-translate-the-buttons-in-qmessagebox
              https://forum.qt.io/topic/84194/qmessagebox-buttons-translation-not-working

              Seems to depend on your exact language.

              HoMaH 1 Reply Last reply
              0
              • SGaistS SGaist

                A minimal buildable example is always a good idea.

                You should also add the Qt version you are using as well as OS you are running.

                HoMaH Offline
                HoMaH Offline
                HoMa
                wrote on last edited by HoMa
                #6

                @SGaist OK - so... I created a new QWidget project and in the Wizard to do so I chose German (Germany) as the desired language. With this code:

                void MainWindow::on_pushButton_clicked()
                {
                    QMessageBox::question(this, "Titel", "Eine Nachricht");
                }
                

                I get a message box with "yes" and "no" buttons- and not "Ja" and "Nein".

                if I add the following code to main.cpp and main function, I get the correct message box:

                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;
                

                However - in my program this does not work. Even with the code shown above running with success, the texts stay with "yes" etc.

                A difference I found to the generated example was the following lines in the .pro file:

                TRANSLATIONS += \
                    testGermanQt_de_DE.ts
                

                However - this did nothing to my project :(
                What else could it be?

                Best regards
                Holger

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  What does that file contain ?

                  Can you show the two main.cpp you have now ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • HoMaH Offline
                    HoMaH Offline
                    HoMa
                    wrote on last edited by HoMa
                    #8

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

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @HoMa
                      You should read the following:
                      https://stackoverflow.com/questions/31533019/how-to-translate-the-buttons-in-qmessagebox
                      https://forum.qt.io/topic/84194/qmessagebox-buttons-translation-not-working

                      Seems to depend on your exact language.

                      HoMaH Offline
                      HoMaH Offline
                      HoMa
                      wrote on last edited by
                      #9

                      @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

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        How do you expect that something gets translated when the ts file does not contain any translations at all?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        SGaistS 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          How do you expect that something gets translated when the ts file does not contain any translations at all?

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          HoMaH 1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

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

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            HoMaH 1 Reply Last reply
                            0
                            • SGaistS SGaist

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

                              HoMaH Offline
                              HoMaH Offline
                              HoMa
                              wrote on last edited by
                              #13

                              @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

                              1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

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

                                HoMaH Offline
                                HoMaH Offline
                                HoMa
                                wrote on last edited by
                                #14

                                @Christian-Ehrlicher The qm file comes with Qt: it is originally called qtbase_de.qm, I guess ...

                                1 Reply Last reply
                                0
                                • Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  How big is the qm file? Are you sure it contains any translations?

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  HoMaH 1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Christian Ehrlicher

                                    How big is the qm file? Are you sure it contains any translations?

                                    HoMaH Offline
                                    HoMaH Offline
                                    HoMa
                                    wrote on last edited by
                                    #16

                                    @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

                                    1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Offline
                                      Christian EhrlicherC Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #17

                                      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?

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      HoMaH 1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Christian Ehrlicher

                                        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?

                                        HoMaH Offline
                                        HoMaH Offline
                                        HoMa
                                        wrote on last edited by
                                        #18

                                        @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

                                        JonBJ 1 Reply Last reply
                                        0
                                        • Christian EhrlicherC Offline
                                          Christian EhrlicherC Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #19

                                          The file open dialog comes from the os (when you use the static versions) - there is nothing Qt can do against it.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          1 Reply Last reply
                                          2
                                          • HoMaH HoMa

                                            @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

                                            JonBJ Offline
                                            JonBJ Offline
                                            JonB
                                            wrote on last edited by JonB
                                            #20

                                            @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#getOpenFileName

                                            On 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 and QFileDialog::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"));

                                            HoMaH 1 Reply Last reply
                                            0

                                            • Login

                                            • Login or register to search.
                                            • First post
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved