Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start using QtLinguist to translate my QtCreator project?

Tags:

qt

qt-linguist

I have a simple .pro Qt Creator project. I have most, if not all, of my application text in Qt .ui XML form files. I want to use Qt Linguist to translate text in that project, but I have no idea how to start. There's no useful "Getting started with Qt Linguist" article on the internet. Page proudly calling itself Qt Linguist Manual is also not very helpful as it only contains generic info, not practical hints.

I found these items in Qt Creator:

image description

I am not so sure what to do to make them work and what is their relation to Qt Linguist, which is totally standalone application. I opened Qt Linguist and it doesn't even contain any File -> New... item menu, which is where I would start.

like image 540
Tomáš Zato - Reinstate Monica Avatar asked Dec 14 '22 09:12

Tomáš Zato - Reinstate Monica


2 Answers

In the beginning it requires quite some work, but then it is all automatic.

Source code

In your source file you must put translatable strings inside a tr() function. All QObject derived classes have their own tr() function. For strings inside that class use the tr function of that class: so in stead of

QString str ="string to be translated";

use

QString str = MyClass::tr("string to be translated");

If the class has no tr() function use

QString str = QOject::tr("string to be translated");

Qt Creator

In Qt Creator you go to Tools/External/Configure... and fill out the location of lupdate and lrelease. You can hard code their location or use macro's. These utilities are located in a bin subdir of the Qt install location.

ts files

Suppose you want to translate from English to Dutch. You will need two files. We will call them: myapp_en.ts and myapp_nl.ts . In principle you do not need to include the English ts file but it helps to treat English on the same footing as another language.

Project file

add to the pro file

TRANSLATIONS+=  /path_to_/myapp_en.ts

TRANSLATIONS+=  /path_to_/myapp_nl.ts

First-time ts files

The first time you run lupdate the ts files will be created.

Actual translation

Open the ts file with Qt Linguist. You can set some properties of this file. All the strings that need to be translated should now be visible in the Qt Linguist view on the ts file. You can insert the translations. And save the modified ts file.

Update ts files

Any time you have changed your source code you should run lupdate. Add the translations of any new strings with Qt Linguist and run lupdate and then lrelease. The program lrelease converts the text files to compressed binary files (extension "qm").

Load translation

The only thing left now is to tell your application to load a specific translation (that is to load a specific qm file). You do this with the Qt class QTranslator with excellent documentation.

like image 64
adlag Avatar answered Jun 03 '23 23:06

adlag


Will be able to view ".ts" file select any single file right click and see is it showing option to open file with qt4 linguist?

if not viewing go to properties/open with
in the last there will be a reset button click will be able to see now.

if not install package "qt4-dev-tools - to download qt linguist"

Vicky Sharma

like image 36
vicky Avatar answered Jun 04 '23 01:06

vicky