Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I load multiple QTranslator files, each for different parts of my application?

Tags:

qt

qtranslate

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

like image 276
cnd Avatar asked Dec 23 '14 11:12

cnd


1 Answers

Yes, you can. As 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 searched for in the reverse order in which they were installed, so the most recently installed translation file is searched first and the first translation file installed is searched last. The search stops as soon as a translation containing a matching string is found.

Installing or removing a QTranslator, or changing an installed QTranslator generates a LanguageChange event for the QCoreApplication instance. A QApplication instance will propagate the event to all toplevel windows, where a reimplementation of changeEvent can re-translate the user interface by passing user-visible strings via the tr() function to the respective property setters. User-interface classes generated by Qt Designer provide a retranslateUi() function that can be called.

The function returns true on success and false on failure.

You need to load some translation file, qApp macro to get instance of QApplication outside main() function 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 translator with:

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

Internationalization is a big part, so I can suggest also next links:

Internationalization with Qt

Writing Source Code for Translation

And books:

Foundations of Qt Development (Expert's 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. Professionalnoe programmirovanie na C++ (Russian) Chapter 31 (for russian-speakers)

like image 157
Kosovan Avatar answered Oct 06 '22 00:10

Kosovan