Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to translate a program into a language with QtLinguist?
QtWS25 Last Chance

How to translate a program into a language with QtLinguist?

Scheduled Pinned Locked Moved Solved Qt 6
12 Posts 6 Posters 1.3k Views
  • 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.
  • G Offline
    G Offline
    gouneken
    wrote on 21 Dec 2021, 21:26 last edited by
    #1

    Good evening to the whole community, I am writing to you because I wrote code on QtCreator to translate the GUI of my application into English and Spanish. The .ts translation files have been generated. And I translated strings to English on QtLinguist (but not Spanish), and I ticked the fields with a green arrow to show that I was sure of the translation. But when I generated the files .qm thanks to lrelease, the IDE wrote:
    Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_en.qm'...

    Generated 3 translation(s) (3 finished and 0 unfinished)
    

    Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_es.qm'...

    Generated 0 translation(s) (0 finished and 0 unfinished)
    Ignored 3 untranslated source text(s)
    

    "C:\QtSdk2\6.2.1\mingw81_64\bin\lrelease.exe" finished
    But the text to be translated has not been translated into English. However, I put the .qm file in the same folder as the executable of my software and I wrote the following code in the main file:
    #include "FenPrincipale.h"

    #include <QApplication>
    #include <QTranslator>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load("zeroclassgenerator_en");
    a.installTranslator(&translator);
    FenPrincipale fenetre;
    fenetre.show();
    return a.exec();
    }

    I don't know where I went wrong.
    Thank you in advance for your answers!

    K R A 3 Replies Last reply 22 Dec 2021, 00:38
    0
    • G Offline
      G Offline
      gouneken
      wrote on 8 Jan 2022, 14:38 last edited by
      #10

      It's good, I found the solution to my problem, thank you anyway for the help you gave me. The solution is as follows:
      What is probably happening is that QTranslator :: load fails; since I didn't specify an absolute path and didn't pass a directory as the second argument, it will only try to find the file in my current working directory.

      To make this more robust, I need to a) specify the directory as the second argument and b) check the return value of installTranslator ():
      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      QTranslator translator;

      if (!translator.load("zeroclassgenerator_en", QApplication::applicationDirPath()))
          qWarning("Could not load translation file");
      a.installTranslator(&translator);
      FenPrincipale fenetre;
      fenetre.show();
      return a.exec()
      

      }

      P 1 Reply Last reply 8 Jan 2022, 15:09
      0
      • G gouneken
        21 Dec 2021, 21:26

        Good evening to the whole community, I am writing to you because I wrote code on QtCreator to translate the GUI of my application into English and Spanish. The .ts translation files have been generated. And I translated strings to English on QtLinguist (but not Spanish), and I ticked the fields with a green arrow to show that I was sure of the translation. But when I generated the files .qm thanks to lrelease, the IDE wrote:
        Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_en.qm'...

        Generated 3 translation(s) (3 finished and 0 unfinished)
        

        Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_es.qm'...

        Generated 0 translation(s) (0 finished and 0 unfinished)
        Ignored 3 untranslated source text(s)
        

        "C:\QtSdk2\6.2.1\mingw81_64\bin\lrelease.exe" finished
        But the text to be translated has not been translated into English. However, I put the .qm file in the same folder as the executable of my software and I wrote the following code in the main file:
        #include "FenPrincipale.h"

        #include <QApplication>
        #include <QTranslator>
        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        QTranslator translator;
        translator.load("zeroclassgenerator_en");
        a.installTranslator(&translator);
        FenPrincipale fenetre;
        fenetre.show();
        return a.exec();
        }

        I don't know where I went wrong.
        Thank you in advance for your answers!

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 22 Dec 2021, 00:38 last edited by
        #2

        Start by checking what QTranslator::load returned. If there's a problem loading the file you're going to get a false, in which case you'd start troubleshooting why the file can't be found.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • G gouneken
          21 Dec 2021, 21:26

          Good evening to the whole community, I am writing to you because I wrote code on QtCreator to translate the GUI of my application into English and Spanish. The .ts translation files have been generated. And I translated strings to English on QtLinguist (but not Spanish), and I ticked the fields with a green arrow to show that I was sure of the translation. But when I generated the files .qm thanks to lrelease, the IDE wrote:
          Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_en.qm'...

          Generated 3 translation(s) (3 finished and 0 unfinished)
          

          Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_es.qm'...

          Generated 0 translation(s) (0 finished and 0 unfinished)
          Ignored 3 untranslated source text(s)
          

          "C:\QtSdk2\6.2.1\mingw81_64\bin\lrelease.exe" finished
          But the text to be translated has not been translated into English. However, I put the .qm file in the same folder as the executable of my software and I wrote the following code in the main file:
          #include "FenPrincipale.h"

          #include <QApplication>
          #include <QTranslator>
          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          QTranslator translator;
          translator.load("zeroclassgenerator_en");
          a.installTranslator(&translator);
          FenPrincipale fenetre;
          fenetre.show();
          return a.exec();
          }

          I don't know where I went wrong.
          Thank you in advance for your answers!

          R Offline
          R Offline
          r3d9u11
          wrote on 22 Dec 2021, 14:11 last edited by r3d9u11
          #3

          @gouneken Hello.
          Check that example,.
          probably, can be will useful for your: https://github.com/R3D9477/EmptyQmlMuiApp

          1 Reply Last reply
          0
          • G gouneken
            21 Dec 2021, 21:26

            Good evening to the whole community, I am writing to you because I wrote code on QtCreator to translate the GUI of my application into English and Spanish. The .ts translation files have been generated. And I translated strings to English on QtLinguist (but not Spanish), and I ticked the fields with a green arrow to show that I was sure of the translation. But when I generated the files .qm thanks to lrelease, the IDE wrote:
            Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_en.qm'...

            Generated 3 translation(s) (3 finished and 0 unfinished)
            

            Updating 'C:/Users/user/Documents/ZeroClassGenerator/zeroclassgenerator_es.qm'...

            Generated 0 translation(s) (0 finished and 0 unfinished)
            Ignored 3 untranslated source text(s)
            

            "C:\QtSdk2\6.2.1\mingw81_64\bin\lrelease.exe" finished
            But the text to be translated has not been translated into English. However, I put the .qm file in the same folder as the executable of my software and I wrote the following code in the main file:
            #include "FenPrincipale.h"

            #include <QApplication>
            #include <QTranslator>
            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);
            QTranslator translator;
            translator.load("zeroclassgenerator_en");
            a.installTranslator(&translator);
            FenPrincipale fenetre;
            fenetre.show();
            return a.exec();
            }

            I don't know where I went wrong.
            Thank you in advance for your answers!

            A Offline
            A Offline
            artwaw
            wrote on 22 Dec 2021, 15:03 last edited by
            #4

            @gouneken What is your platform/OS? You load the qm file from the startup path which might not be the path where your executable is located! Either use full path for debug purposes or embed qm files into the resource (this way location is always known).

            Please follow what @kshegunov wrote - my gut feeling is that the qm file is not loaded at all.

            For more information please re-read.

            Kind Regards,
            Artur

            G 1 Reply Last reply 25 Dec 2021, 22:34
            2
            • A artwaw
              22 Dec 2021, 15:03

              @gouneken What is your platform/OS? You load the qm file from the startup path which might not be the path where your executable is located! Either use full path for debug purposes or embed qm files into the resource (this way location is always known).

              Please follow what @kshegunov wrote - my gut feeling is that the qm file is not loaded at all.

              G Offline
              G Offline
              gouneken
              wrote on 25 Dec 2021, 22:34 last edited by
              #5

              @artwaw Good evening , How to make sure that my .qm file is loaded , I'm using Windows 7
              Thank you for your answer

              A 1 Reply Last reply 27 Dec 2021, 10:00
              0
              • G gouneken
                25 Dec 2021, 22:34

                @artwaw Good evening , How to make sure that my .qm file is loaded , I'm using Windows 7
                Thank you for your answer

                A Offline
                A Offline
                artwaw
                wrote on 27 Dec 2021, 10:00 last edited by
                #6

                @gouneken

                @artwaw said in How to translate a program into a language with QtLinguist?:

                Either use full path for debug purposes or embed qm files into the resource (this way location is always known).
                Please follow what @kshegunov wrote - my gut feeling is that the qm file is not loaded at all.

                For more information please re-read.

                Kind Regards,
                Artur

                G 1 Reply Last reply 7 Jan 2022, 21:23
                1
                • A artwaw
                  27 Dec 2021, 10:00

                  @gouneken

                  @artwaw said in How to translate a program into a language with QtLinguist?:

                  Either use full path for debug purposes or embed qm files into the resource (this way location is always known).
                  Please follow what @kshegunov wrote - my gut feeling is that the qm file is not loaded at all.

                  G Offline
                  G Offline
                  gouneken
                  wrote on 7 Jan 2022, 21:23 last edited by
                  #7

                  @artwaw I modified the code in the main file to get the following code:
                  #include "FenPrincipale.h"

                  #include <QApplication>
                  #include <QTranslator>
                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  QTranslator translator;
                  translator.load("zeroclassgenerator_en.qm");
                  if(!translator.load("zeroclassgenerator_en"))
                  {
                  return 1;
                  } else {
                  a.installTranslator(&translator); }
                  FenPrincipale fenetre;
                  fenetre.show();
                  return a.exec();

                  }

                  But, when I compile it doesn't show an application. Does this mean that the file has not been loaded? Because I don't understand, I did what I thought had to be done. Apart from the wrong path is there some other reason why the translation file would not load?
                  Hope you will answer this time. Thank you!

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 8 Jan 2022, 09:28 last edited by
                    #8

                    When your program doesn't start up then your if() statement evaluated to true I would guess... but you can really check it by yourself.
                    Why do you try to load the translator twice? Are you sure your qm file is in the current working directory where you start your application from? Better move the qm files to a Qt resource file. There are enough topics around with this kind of problems -> Search function.

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

                    P 1 Reply Last reply 8 Jan 2022, 13:58
                    0
                    • Christian EhrlicherC Christian Ehrlicher
                      8 Jan 2022, 09:28

                      When your program doesn't start up then your if() statement evaluated to true I would guess... but you can really check it by yourself.
                      Why do you try to load the translator twice? Are you sure your qm file is in the current working directory where you start your application from? Better move the qm files to a Qt resource file. There are enough topics around with this kind of problems -> Search function.

                      P Offline
                      P Offline
                      posktomten
                      wrote on 8 Jan 2022, 13:58 last edited by posktomten 1 Aug 2022, 14:11
                      #9

                      @Christian-Ehrlicher
                      Hey!
                      You may be able to get help if you check this out.
                      Greetings Ingemar

                      screenshot.png

                      main.cpp

                      #include "mainwindow.h"
                      #include <QApplication>
                      #include <QLocale>
                      #include <QTranslator>
                      #include <QSettings>
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          QTranslator translator;
                          QSettings settings(QSettings::IniFormat, QSettings::UserScope, "TEST", "test");
                          settings.beginGroup("Language");
                         // Retrieve the settings from QSettings (AppData\Roaming\TEST)
                          QString language = settings.value("language", "").toString();
                          settings.endGroup();
                      
                          // If no language is selected, the local language is loaded
                          // (if there is any translation)
                          if(language.isEmpty()) {
                              const QStringList uiLanguages = QLocale::system().uiLanguages();
                      
                              for(const QString &locale : uiLanguages) {
                                  const QString baseName = "test_" + QLocale(locale).name();
                      
                                  if(translator.load(":/i18n/" + baseName)) {
                                      a.installTranslator(&translator);
                                      break;
                                  }
                              }
                      
                              // If the user clicked and selected a language
                          } else  {
                              // If the original language is selected, no translation is required
                              if(language != "en_US") {
                                  const QString baseName = "test_" + language;
                      
                                  // Language file is loaded according to the selection
                                  // loaded from QSettings
                                  if(translator.load(":/i18n/" + baseName)) {
                                      a.installTranslator(&translator);
                                  }
                              }
                          }
                      
                          MainWindow w;
                          w.show();
                          return a.exec();
                      }
                      

                      mainwindow.h

                      #ifndef MAINWINDOW_H
                      #define MAINWINDOW_H
                      
                      #include <QMainWindow>
                      
                      QT_BEGIN_NAMESPACE
                      namespace Ui
                      {
                      class MainWindow;
                      }
                      QT_END_NAMESPACE
                      
                      class MainWindow : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          MainWindow(QWidget *parent = nullptr);
                          ~MainWindow();
                      
                      private:
                          Ui::MainWindow *ui;
                          void restart();
                      };
                      #endif // MAINWINDOW_H
                      

                      mainwindow.cpp

                      #include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      #include <QSettings>
                      #include <QProcess>
                      #include <QDebug>
                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent)
                          , ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                          ui->textEdit->setText(tr("Hello World!"));
                          // Click on "Spanish"
                          connect(ui->pbSpanish, &QPushButton::clicked, [this]() -> void {
                      
                              QSettings settings(QSettings::IniFormat, QSettings::UserScope, "TEST", "test");
                              settings.beginGroup("Language");
                              // Saves the language selection in QSettings (AppData\Roaming\TEST)
                              settings.setValue("language", "es_ES");
                              settings.endGroup();
                              restart();
                          });
                      
                          // Click on "Swedish"
                          connect(ui->pbSwedish, &QPushButton::clicked, [this]() -> void {
                      
                              QSettings settings(QSettings::IniFormat, QSettings::UserScope, "TEST", "test");
                              settings.beginGroup("Language");
                              // Saves the language selection in QSettings (AppData\Roaming\TEST)
                              settings.setValue("language", "sv_SE");
                              settings.endGroup();
                              restart();
                          });
                         
                       // Click on "English"
                          connect(ui->pbEnglish, &QPushButton::clicked, [this]() -> void {
                      
                              QSettings settings(QSettings::IniFormat, QSettings::UserScope, "TEST", "test");
                              settings.beginGroup("Language");
                              // Saves the language selection in QSettings (AppData\Roaming\TEST)
                              settings.setValue("language", "en_US");
                              settings.endGroup();
                              // The program is restarted to load the new language settings.
                              restart();
                          });
                      }
                      
                      void MainWindow::restart()
                      {
                          // Closes and starts the program
                          const QString executable = QCoreApplication::applicationFilePath();
                          QProcess p;
                          p.setProgram(executable);
                          close();
                          p.startDetached();
                      }
                      
                      MainWindow::~MainWindow()
                      {
                          delete ui;
                      }
                      

                      test.pro

                      QT       += core gui
                      
                      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                      
                      CONFIG += c++11
                      
                      # You can make your code fail to compile if it uses deprecated APIs.
                      # In order to do so, uncomment the following line.
                      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                      
                      SOURCES += \
                          main.cpp \
                          mainwindow.cpp
                      
                      HEADERS += \
                          mainwindow.h
                      
                      FORMS += \
                          mainwindow.ui
                      
                      TRANSLATIONS += \
                          i18n/test_sv_SE.ts \
                          i18n/test_es_ES.ts
                      
                      CONFIG += lrelease
                      CONFIG += embed_translations
                      
                      # Default rules for deployment.
                      qnx: target.path = /tmp/$${TARGET}/bin
                      else: unix:!android: target.path = /opt/$${TARGET}/bin
                      !isEmpty(target.path): INSTALLS += target
                      

                      posktomten

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gouneken
                        wrote on 8 Jan 2022, 14:38 last edited by
                        #10

                        It's good, I found the solution to my problem, thank you anyway for the help you gave me. The solution is as follows:
                        What is probably happening is that QTranslator :: load fails; since I didn't specify an absolute path and didn't pass a directory as the second argument, it will only try to find the file in my current working directory.

                        To make this more robust, I need to a) specify the directory as the second argument and b) check the return value of installTranslator ():
                        int main(int argc, char *argv[])
                        {
                        QApplication a(argc, argv);
                        QTranslator translator;

                        if (!translator.load("zeroclassgenerator_en", QApplication::applicationDirPath()))
                            qWarning("Could not load translation file");
                        a.installTranslator(&translator);
                        FenPrincipale fenetre;
                        fenetre.show();
                        return a.exec()
                        

                        }

                        P 1 Reply Last reply 8 Jan 2022, 15:09
                        0
                        • G gouneken
                          8 Jan 2022, 14:38

                          It's good, I found the solution to my problem, thank you anyway for the help you gave me. The solution is as follows:
                          What is probably happening is that QTranslator :: load fails; since I didn't specify an absolute path and didn't pass a directory as the second argument, it will only try to find the file in my current working directory.

                          To make this more robust, I need to a) specify the directory as the second argument and b) check the return value of installTranslator ():
                          int main(int argc, char *argv[])
                          {
                          QApplication a(argc, argv);
                          QTranslator translator;

                          if (!translator.load("zeroclassgenerator_en", QApplication::applicationDirPath()))
                              qWarning("Could not load translation file");
                          a.installTranslator(&translator);
                          FenPrincipale fenetre;
                          fenetre.show();
                          return a.exec()
                          

                          }

                          P Offline
                          P Offline
                          posktomten
                          wrote on 8 Jan 2022, 15:09 last edited by
                          #11

                          @gouneken
                          It was good!
                          I usually add icons, license text, translations, etc. in a resource file. So everything is in the exe file.

                          Ingemar

                          posktomten

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            posktomten
                            wrote on 8 Jan 2022, 22:04 last edited by posktomten 1 Aug 2022, 22:20
                            #12

                            A little improved
                            Language example.zip

                            posktomten

                            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