Book Image

Application Development with Qt Creator - Second Edition

Book Image

Application Development with Qt Creator - Second Edition

Overview of this book

Table of Contents (20 chapters)
Application Development with Qt Creator Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Including localized strings in your application


In order to supply translated strings to the tr and qsTr functions in your application, your application needs to include a QTranslator object to read the .qm files and replace the strings provided to tr and qsTr with their translated counterparts. We can do this in your main entry point function, as follows:

QApplication a(argc, argv);
QTranslator translator;
bool result = translator.load("QtLinguistExample-epo.qm");
a.installTranslator(&translator);

    // Other window setup stuff goes here
    
return a.exec();

This code allocates a QTranslator object and loads the indicated translation file into the translator before installing it into the QApplication object. In this example, we're hardcoding the language in order to localize to Esperanto.

Note that if you want to support the locale as picked by the system, you might choose to do it this way:

QString locale = QLocale::system().name();
QTranslator translator;
translator.load(QString("QtLinguistExample...