Unsolved Qt dynamic translation on a serialized enum key in a comboBox
-
I am trying to lean on how to internationalize an existing code with a dynamic translation and am stuck at dynamically translating enum keys. So, I started doing some searches through Google and narrowed down to this answer (which is a rather 10 years old post). To show what I have done, I took a working demo code from chapter 11 of Cross-Platform Development with Qt 6 and Modern C++ book. My code can be downloaded from here. As you can see, in my CustomWidget::init() method, I added some routines (see below) to update the comboBox with a QStringList object filled with translated enum keys. The program compiles just fine, but the comboBox still shows the original (not translated) text when a language is changed. Did I do something wrong? I don't know if this makes any difference: The original post uses QObject::staticQtMetaObject while my code uses staticMetaObject.
void CustomWidget::init() { int counter = metaEnumColor.keyCount(); qDebug() << "[" << __func__ << "]: counter =" << counter; for(int i=0; i<counter; i++) { QString colorKey = tr(metaEnumColor.key(i)); enumColorKeys << colorKey; qDebug() << "[" << __func__ << "]:" << colorKey << "color added to enum"; } ui->comboBoxColor->addItems(enumColorKeys); }
-
@Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:
Did I do something wrong?
You do not translate anything as far as I can see. You simply convert enum value names into strings.
enum {
First; // Will be translated into "First"
} -
@jsulm said in Qt dynamic translation on a serialized enum key in a comboBox:
@Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:
Did I do something wrong?
You do not translate anything as far as I can see. You simply convert enum value names into strings.
enum {
First; // Will be translated into "First"
}I merely followed exactly what the original poster claimed the problem solved about 10 years ago by using the tr() function to build the list of values for the enumeration and then translated that value in the file. <. ts> file. Isn't that's exactly what I did? If so, what exactly did I do wrong? The following is the original code:
QMetaEnum metaEnum = QObject::staticQtMetaObject.enumerator(index); QStringList enumNames; for (int i = 0; i < metaEnum.keyCount(); i++) { enumNames << tr(metaEnum.key(i)); }
and below is my code:
void CustomWidget::init() { int counter = metaEnumColor.keyCount(); qDebug() << "[" << __func__ << "]: counter =" << counter; for(int i=0; i<counter; i++) { QString colorKey = tr(metaEnumColor.key(i)); enumColorKeys << colorKey; qDebug() << "[" << __func__ << "]:" << colorKey << "color added to enum"; } ui->comboBoxColor->addItems(enumColorKeys); }
-
Hi
From your code we don't know if you installed a translator, when you did it, if you created the corresponding translation files nor if loaded them properly.
-
@Habibie You're right, somehow I oversaw the tr().
-
@SGaist said in Qt dynamic translation on a serialized enum key in a comboBox:
Hi
From your code we don't know if you installed a translator, when you did it, if you created the corresponding translation files nor if loaded them properly.
Perhaps, I wasn't clear enough; however, if you carefully read my OP above, I clearly stated that I took a working demo code from chapter 11 of Cross-Platform Development with Qt 6 and Modern C++ book and all I did was to add a CustomWidget::init() method to update the comboBox with a QStringList object filled with translated enum keys. The original working code still works fine in its translation (must have the translator properly installed) while the translation ONLY fails in the comboBox.
-
@Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:
the translation ONLY fails in the comboBox
Obviously, because you haven't provided any translation for the enum keys values as strings!
From your code, and taking the Spanish translation file WidgetTranslationDemo_es_ES.ts for instance.
Where are there any entries for Blue, Green and Red? much less their translations as Azul, Verde and RojoThe problem with your use case is that the lupate tool (used to extract literal strings from tr() calls into the translation files (.ts) won't work because of not having literals at all to extract!:
tr(metaEnumColor.key(i))
-
@Pablo-J-Rogina said in Qt dynamic translation on a serialized enum key in a comboBox:
@Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:
the translation ONLY fails in the comboBox
Obviously, because you haven't provided any translation for the enum keys values as strings!
From your code, and taking the Spanish translation file WidgetTranslationDemo_es_ES.ts for instance.
Where are there any entries for Blue, Green and Red? much less their translations as Azul, Verde and RojoThe problem with your use case is that the lupate tool (used to extract literal strings from tr() calls into the translation files (.ts) won't work because of not having literals at all to extract!:
tr(metaEnumColor.key(i))
When you said the translation files (.ts), did you mean the file generated by lupdate utility? If so, I noticed, the lupdate utility does not pickup all the enum keys and I reckon that's the problem. For instance, after I made the necessary changes to my code and save it, I executed the lupdate utility from within the qtcreator and I noticed lupdate utility reported with a 0 new. However, according this rather 10 years old post, it works and that's what I did in my code. This means there is something wrong with my code and I just couldn't find it.
-
@Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:
according this rather 10 years old post
From that post, I'll extract this phrase from OP:
And I tranlate that value in my file. <. ts>
given that it was never stated that the lupdate tool was responsible for doing that, my bet is that the enum keys were manually added to the .ts file(s) and then translated as usual.
-
@Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:
If so, I noticed, the lupdate utility does not pickup all the enum keys and I reckon that's the problem.
Because you do not properly define them - see QT_TRANSLATE_NOOP documentation.