Important: Please read the Qt Code of Conduct -

Getting the actual messages out of a QTranslator

  • Back in the day, it was possible to manually reach into a QTranslator and get all the messages inside it, for various uses (via a messages() member function, it looks like). It was useful for things such as handling translations of dynamically created menus and actions (i.e. ones that would not be automatically translated when all the other menus were, because only menus that exist at compile time get translated automatically).

    Nowadays, it looks like a lot of this functionality has been removed; if it doesn't work automatically, you're out of luck. Does anyone know a way to reach into the QTranslator and get the bi-directional messages? This would be particularly useful for reversing the lookup.

    The specific problem is that when the menus are generated, they are given text according to the current translation, so if they're created whilst in Japanese mode, their text is Japanese, and when the user switches to English, I have no way to look up that Japanese text and get the appropriate English.

  • Lifetime Qt Champion


    Why don't you use QEvent::LanguageChange ?

    From the documentation

    void MyWidget::changeEvent(QEvent *event)
    if (event->type() == QEvent::LanguageChange) {
    titleLabel->setText(tr("Document Title"));
    } else

    No need for revers lookups

  • bq. Why don’t you use QEvent::LanguageChange ?

    The menu-builder is not a QWidget; currently pushing the "Switch Language" button triggers the special handling for things that are not QWidgets and won't get the event; informing the Menu handlers that it's time to change language is not a problem.

    Keeping the text to be used on various different menus inside a changeEvent (or some other) function is just ugly. The menu objects should be in charge of themselves and hold their own data. Given that there can be a different number every time, I'd have to have each one add itself in some way as it went, along with a record of what its original English text is. I would much rather the objects simply have the capability to switch their display language themselves, internally, like nice encapsulated objects.

    I could give them a "retranslate" function of their own, but I would then have to permanently keep track of the original English wording; it would be nice if they could simply look at their own current display text and translate that.

Log in to reply