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?
Forum Updated to NodeBB v4.3 + New Features

How to translate a program into a language with QtLinguist?

Scheduled Pinned Locked Moved Solved Qt 6
12 Posts 6 Posters 1.4k Views 3 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.
  • G gouneken

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

      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!

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on 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
      2
      • artwawA artwaw

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

        artwawA 1 Reply Last reply
        0
        • G gouneken

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

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on 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
          1
          • artwawA artwaw

            @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 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 Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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

              posktomtenP 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                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.

                posktomtenP Offline
                posktomtenP Offline
                posktomten
                wrote on last edited by posktomten
                #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 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()
                  

                  }

                  posktomtenP 1 Reply Last reply
                  0
                  • G gouneken

                    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()
                    

                    }

                    posktomtenP Offline
                    posktomtenP Offline
                    posktomten
                    wrote on 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
                    • posktomtenP Offline
                      posktomtenP Offline
                      posktomten
                      wrote on last edited by posktomten
                      #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