View Full Version : Dynamic Translate

liran ritkop
23rd March 2011, 15:34
I have problems in understanding how should i implement the dynamic language in my case:
I have a class "GlobalClass" contains an array of strings in this manner:

const char * const ids[] = {

I have some different widgets (icons) that contain a QString, which I initialize in this manner:

icons[index].Text = tr(GlobalClassObject->ids[index2]);

Till now everything is good, and i can see the text on the screen.
But! if i want to dynamic change it, how do i do it??
Please respond! thanks in advance..

23rd March 2011, 15:40
What do you mean by that you want to dynamically change it? What do you mean by "dynamically" and what is "it"?

liran ritkop
27th March 2011, 13:36
Dynamically translate is changing the language of your application "on-the-fly" (without reset your application).
You can read about it in the qt programming guide.
So anyone know how to implement what i meant?

27th March 2011, 14:55
I know what is dynamic translation but seeing just one line of your code it is not possible to deduce what you want. If you want to change the language you need to retranslate all user-visible strings (like what retranslateUi() method of forms generated by uic does). Without knowing where your single line of code fits in it is not possible to suggest a solution.

28th March 2011, 10:00
Maybe you need this: dynamic translation (http://doc.trolltech.com/latest/internationalization.html#dynamic-translation)

liran ritkop
28th March 2011, 11:33
So thats exactly what i'm asking! how should i write the retranslate function to fit my needs.
I know how to do it when i have the string in the tr() function (like tr("string")), but i don't know how am i suppose to retranslate when i have the string in that manner of QT_TR_NOOP array.

28th March 2011, 12:02
Exactly the same way. Eventually you'll have to call tr() to do the translation.

liran ritkop
30th March 2011, 10:56
Hi again!
Well, i did exactly what you said, but it still doesn't work and i'm starting to get crazy.
This is what i do:
1. I have a global class, which have this strings array initialize:

GeneralStruct::GeneralStruct(QWidget *parent):QWidget(parent){
greeting_strings[0] = QT_TR_NOOP("France");
greeting_strings[1] = QT_TR_NOOP("Energy");
greeting_strings[2] = QT_TR_NOOP("Caps");
greeting_strings[3] = QT_TR_NOOP("Switch");

2.Now, in order to install the translator when necessary, i send from the main the QApplication pointer, and later install in it the translator:


3. Then, I change the texts of the visible icons to the new language like this:

for (i=0; i<Number_Of_Boxes; i++)
boxes[i].Text = tr(generalStructObject->greeting_strings[i]);

The new word is exactly the same! it doesn't change at all!
What is the problem here? do i miss something?

30th March 2011, 10:59
What do you apart setting these Text properties? Do you map them to some widget or anything? What's the type of "boxes"?

liran ritkop
30th March 2011, 11:14
the type of boxes are:

typedef struct _menu_box
QPixmap pixmap;
QPixmap pixmapAlpha;
int clicked;
int action;
QString Text;
QString ActionID;
} menu_box;

and are placed in a widget called menu_screen:

class menu_screen : public QGraphicsView
menu_box *boxes;

30th March 2011, 11:26
How are they "placed" there? Are you assuming that:

QString str = "x";
QLabel lab;
str = "y";
will make the label's text change from "x" to "y"? It won't.

liran ritkop
30th March 2011, 11:44
They are placed there as i wrote before (look at paragraph 3).
the tr() function return the same string in the default language.

30th March 2011, 12:35
Did installTranslator() return true? Did you load a proper message catalogue?

liran ritkop
30th March 2011, 13:14
After a long digging in this issue, I succeeded to change the language "on the fly"!
The problem was here:
When you declare about QT_TR_NOOP or tr() functions, the "lupdate" tool build a ts file, where each such declaration is placed under it's object label. In my case:

<translation type="unfinished">translatedMeter</translation>

I dont know why Qt developed it that way, i hope you can tell me the reason.
Anyway, I invoke the tr() function in another object (called menu_screen) as you can see from my previous comments.
It didn't translate the strings, till i changed the ts file manually, and turned it that way:

<translation type="unfinished">translatedMeter</translation>
This time, it worked!
Thanks for your responses, and i hope it will help others who facing this problem.

30th March 2011, 13:22
I dont know why Qt developed it that way, i hope you can tell me the reason.
Because the same message might be translated differently in different contexts. You can set the context by passing an argument to the macro and to tr().