c++qtqt-creator

Converting Qt CMake project to ID based translation model


We have a CMake Qt project (plain widgets, no QML). Right now it has only one MainWindow with menu and some controls. Initially translation was done with a default plain-text-based mechanism. We now want to turn the project to text-ID-based mechanism. We have valid reasons, and this needs to happen. I added the option -idbased to qt_add_translations(... LRELEASE_OPTIONS ...) in CMakeLists.txt file.

I understood that in text-ID-based mechanism there are no contexts, yet in function Ui_MainWindow::retranslateUi I still see bunch of lines like this one (that were there before I added the -idbased option):

menuHelp->setTitle(QCoreApplication::translate("MainWindow", "id-help", nullptr));

So, menu item Help has text ID "id-help" in translation context "MainWindow", which makes no sense to me. Are my expectations wrong, or am I doing something wrong? How to make the Qt Creator generate "contextless" translation entries?


Solution

  • While the form is open in the Designer go to menu Tools > Form Editor > Form Settings... In new dialog check "ID-based" check box.

    When you rebuild the project all QCoreApplication::translate calls in retranslateUi will be changed to qtTrId. Also, all parameters in those calls will reset to empty string. If you already changed some texts to ID strings those changes will disappear. Save them somewhere before you do all this.

    All GUI elements will now have an id property. This is where all the ID strings go. The existing text properties contain so called "Engineering English" texts. They show only in the Qt Designer, and they show in the Qt Linguist, essentially as if in .cpp code you add a //% comment before a line that defines a string for translation. They don't show in run-time. You have to fill up the .ts file.