Isuru
Isuru

Reputation: 450

Adding multi-language option to a Qt app with hard-coded text

I'm a beginner in Qt. I have an app designed using Qt which has multiple windows with each consisting of several labels with hard coded text. What would be the easiest way to add multi language support for this app? Which built in Qt objects/functions I should use?

My current idea is to maybe create a separate xml file including the text for all labels for a language. Then once the user selects a language icon from the menu, load the relevant xml file. But I have no idea how to do this. Any help would be highly appreciated!

UPDATE: I have tried to implement the example given here. But it throws the following error and I can't fix it. 'class Ui::MainWindow' has no member named 'menuLanguage'

QActionGroup* langGroup = new QActionGroup(ui->menuLanguage);

Upvotes: 1

Views: 4950

Answers (3)

Farhad
Farhad

Reputation: 4181

Try Qt Linguist:

I made you a simple example:

.pro

TRANSLATIONS += translation_fa.ts

.h

#include <QTranslator>
QTranslator translator;

.cpp

if(translator.load("E:/Qt/Linguist/linguist/translation_fa.qm"))
    qDebug()<<"successfully load qm file.";
else
    qDebug()<<"problem in load qm file.";


// change language to second language
qApp->installTranslator(&translator);

// change language to default language
qApp->removeTranslator(&translator);

Don't forget to use Qt Linguist Tools.

image

This is a sample project for your question on github download here.

Upvotes: 4

jester
jester

Reputation: 238

Qt has built in functions for managing multiple languages:

see: https://wiki.qt.io/How_to_create_a_multi_language_application

Research lupdate lrelease and linguist

Upvotes: 0

king_nak
king_nak

Reputation: 11513

Qt has translation support. Look into Qt Linguist

Basically, you mark all you hard coded texts with a call to QObject::tr, e.g.

lbl->setText(tr("My text to translate"));

Qt Linguist parses all source files and UI forms for such calls (using lupdate.exe) and builds a ts file. With the Qt Linguist GUI application, you can translate them. With lrelease you create a qm file that will be loaded at runtime, and automatically translate the texts

Upvotes: 1

Related Questions