translation doesn't work when executing application

6k views Asked by At

I am using Qt linguist to translate my application (ui file and the rest of the code). Everything goes alright , the problem is all translations (in ui) work fine except an element added not from designer, here is my code to further explain :

 tableWidget = new MyDropTableWidget(ui->verticalLayoutWidget_2);
 if (tableWidget->columnCount() < 1)
 tableWidget->setColumnCount(1);
 tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("My non translated text"))); if (tableWidget->rowCount() < 21)
 tableWidget->setRowCount(21); 
          ...  
 ui->verticalLayout_2->addWidget(tableWidget);
          ...

all texts in ui are translated fine , only "My non translated text" was not. *.ts file are ok ("My non translated text" was detected and checked) , .qm file are well placed, I rebuild re lupdate re lrelease my application but in vain: when executing my application, all texts are translated expect "My non translated text" still in the source language.

Any help will be appreciated.

Edit:

in main file:

QTranslator  Translator ;
qDebug()<<"Translator->load( lang)"<< Translator.load(":/"+language);
app.installTranslator( &Translator );

in mainwindow constructor:

ui->retranslateUi(this);
2

There are 2 answers

1
Antwane On BEST ANSWER

A non-translated text in Qt GUI is often caused by a bad order in your code.

It is impossible to answer precisely without reading your whole code. I suggest you to read the content of Ui::retranslateUi() method. You will see many code like:

button->setText(QApplication::translate("MainWindow", "Import", 0));

The retranslateUi metod is generated for you when compiling a *.ui file, but it is only a list of calls to setText(), setToolTip() or another setXXX() methods on widgets you defined in the ui file. When a setText() method is called (for example), the parameter must be tr("my text"), this tr() is replaced by the corresponding translation at the moment you call it. So you should check if when you initialize your table widget item, your Translator has correctly been installed.

For example, if you create your item in the MainWindow constructor and if you have a main function like:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow w;

    QTranslator  Translator ;
    qDebug()<<"Translator->load( lang)"<< Translator.load(":/"+language);
    app.installTranslator( &Translator );

    w.show();

    return app.exec();
}

it will not work, because the translator is installed after MainWindow constructor call, so when the item is initialized, the translator is not set.

To find the issue in your case, I suggest you to put qDebug() calls at some point to check the order of the calls.

1
Kévin Renella On

Are your .qm located in the same folder as your binary ?

As you are using tr() function, yout text should be translated. I am using this syntax in my program and the translation works fine :

QTranslator translator;

if (QLocale::system().language() == QLocale::French)
    translator.load("fr_lang", QCoreApplication::applicationDirPath());
else
    translator.load("en_lang", QCoreApplication::applicationDirPath());

app.installTranslator(&translator);

My translation files are fr_lang.qm and en_lang.qm