Can I upload multiple QTranslator files, each for a different part of the application?

I can install translator as myApp.installTranslator(&translator)

Is it possible to have multiple translation files and load them from different parts of my application? How can i do this?

+3


source to share


1 answer


Yes, you can. As the doc said:

Adds the translation file translationFile to the list of translation files to be used for translations.

Multiple translation files can be installed. Translations are performed in the reverse order in which they were installed, so most of the newly installed translation file is executed first, and the first translation file is installed last. The search stops as soon as a translation is shown containing the matching string.

Installing or removing a QTranslator, or changing an installed QTranslator generates a LanguageChange event for the QCoreApplication example. The QApplication instance will propagate the event to all toplevel windows, where a reimplementation of changeEvent can re-translate the UI by passing user-visible strings through the tr () function to the appropriate property setters. The user interface classes created by Qt Designer provide a reanslateUi () function that you can call.

The function returns true on success and false on error.

You need to load the translation file, qApp

macro, to get an instance of the function QApplication

outside main()

and do something like:

QTranslator translator;//somewhere

void MainWindow::on_someButton_clicked()
{
    translator.load("://en.qm");
    qApp->installTranslator( &translator );
    ui->retranslateUi(this);               //for Qt designer
}

      

Also you can remove the translator with

void MainWindow::on_someButton_2_clicked()
{
    qApp->removeTranslator(&translator);
    ui->retranslateUi(this);
}

      

Internationalization is the big part, so I can suggest the following links as well:



Internationalization with Qt

Writing source code for translation

And books:

Qt Development Basics (Expert Voice in Open Source) Chapter 10

C ++ GUI Programming with Qt 4 (2nd Edition) (Prentice Hall Open Source Software Development Series) Chapter 18

Qt4.8. Professional programming in C ++ (Russian) Chapter 31 (for Russian speakers)

+5


source







All Articles