How to translate a program into a language with QtLinguist?
-
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! -
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()
}
-
@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.
-
@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. -
@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! -
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. -
@Christian-Ehrlicher
Hey!
You may be able to get help if you check this out.
Greetings Ingemarmain.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; }
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
-
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()
}
-
A little improved
Language example.zip