Farshid.M
Farshid.M

Reputation: 389

qt translator doesn't show translated text in program

I use qt linguist to translate my program in different languages but it doesn't show he translated text in the program. I set proper fonts and add .ts file to TRANSLATIONS.

I use lupdate and lrelease commands.

how can i create .ts file also? (I create text file and change the format to .ts is it correct way?)

Upvotes: 0

Views: 2203

Answers (1)

mohabouje
mohabouje

Reputation: 4050

Revie Qt Translation.

To translate the app dynamically:

1 - Open Qt command terminal and go to your project folder. 2 - Get all the translatable string from your project

lupdate -pro Example.pro -ts example.ts

3 - Translate all the string to the language you want using QLinguist 4 - Generate the .qm file with all translation executing:

lrelease example.ts

5 - Add this file, example.qm, as a resource to your project to include it with the executable file. Resource System

6 - Now, use QTranslator to translate the app:

QTranslator* translator = new QTranslator;
if(translator->load(":/"+example.qm)){
    qApp->removeTranslator(translator); // Remove the translator if was used before
    qApp->installTranslator(translator); // Install again the translator to force a new translation.
    qDebug() << "Translation success!" ;
}else{
    qDebug() << "Error file not found!";
}

7 - You can handle the translation event using changeEvent:

void MainWindow::changeEvent(QEvent* event)
{
     if (event) {
          switch(event->type()) {
              // When the translator is loaded this event is send.
              case QEvent::LanguageChange:
                    break;
              // Whem the system language changes this event is send.
              case QEvent::LocaleChange:
                    //retranslate the ui.
                    break;
              default:
                    break;
          }
     }
     QMainWindow::changeEvent(event);
}

That's all folks!

Upvotes: 5

Related Questions