Solved Translation does not work when the text of QLineEdit is modified
-
@sierdzio I don't know the inner working of Qt. If it is so, then what would you recommend me to modify?
-
This is rather inner working of C++ itself. But I am only guessing to be honest.
What you can try is to define this string array (btw. you can also use QStringList) in a singleton class that is initialized after translation is loaded, so that it gets correct data.
-
@sierdzio My constant variable is called from the ui. But the ui class is instantiated after the translation is loaded. Shouldn't it be OK (it isn't)?
-
Try something like this:
const char * const days[7] = { QT_TR_NOOP("Monday"), QT_TR_NOOP("Tuesday"), // ... and so on } void ZellerGUI::on_yearEdit_editingFinished() { // ... calculate day QString day = QCoreApplication::tr(days[calculatedDay - 1]); ui->resultEdit->setText(day); }
-
@kshegunov When I call lupdate on the project file, it throws these warnings:
tr() cannot be called without context
and these strings do not appear in Linguist.
-
Yes I think my example is rotten. Try with
QT_TRANSLATE_NOOP
and give it a context:const char * const days[7] = { QT_TRANSLATE_NOOP("Weekdays", "Monday"), QT_TRANSLATE_NOOP("Weekdays", "Tuesday"), // ... and so on }
-
@kshegunov Thank you. Now Linguist recognizes it but my application doesn't appreciate it yet. In the debugger, I also see the English name.
-
Well, did you translate it?
QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
-
@kshegunov I just used your previous solution:
QString day = QCoreApplication::tr(days[calculatedDay - 1]);
Using your last command
QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
perfectly works. Now I just have to think it over why it works.
Thank you very much for your precious help!
-
You're welcome.
Happy coding!Edit:
Btw, moving the keys to the function should also work:void ZellerGUI::on_yearEdit_editingFinished() { // If this is initialized here it should work okay static const char * const days[7] = { QT_TR_NOOP("Monday"), QT_TR_NOOP("Tuesday"), // ... and so on }; // ... calculate day QString day = QCoreApplication::tr(days[calculatedDay - 1]); ui->resultEdit->setText(day); }
-
@Zoltan I have the same problem, must I do?