Translating actions dynamically - do actions have some permanent label, or do I have to assign one?

  • I have a big lump of lots of code, that I am retrofitting translation capability to. So far, so good, but now I'm trawling through and having actions translated (or rather, what the action displays on screen through its text() or tooltip() function).

    Many of these actions are generated within docking windows, within the constructor of said docking window. So they might be created something like this:

    @QAction* localAction = new QAction("Do something ACTIONY!", this);@

    which became this when I added translations

    @QAction* localAction = new QAction(TranslationHelper::tr(QObject::tr("Do something ACTIONY!")), this);@

    The TranslationHelper::tr static function I have there handles loading translators and holding them and all that sort of thing, and at the point this action is created, everything's great. If there is a translation loaded, it gets used.

    The user can later select another language. The dock widget overrides the function
    @void changeEvent(QEvent* event)@
    and if the event is a @QEvent::LanguageChange@ it sparks a @ui->retranslateUi(this);@ BUT that doesn't change the text of the action, because that was already created.

    I have ended up doing this:
    and at the point the action is created, I am storing its original text in its objectName property, so that whenever the user changes the language, I can get whatever its original (in my example, English) text was and translate that (I cannot simply attempt to translate its text, as that text could be Japanese by now, or Dutch, or German, or any other language, and my translations only go from English to other languages). In effect I am hijacking the objectName property to use as a permanent record of the original English text of an action.

    I really don't like doing this; it feels icky. It seems like the action should have some kind of permanent label that never changes that I use to identify it, but I can't find one. There must be something, as the QT Creator debugger always presents me with what looks like a permanent label of some kind for every action I examine.

    The actual question; does anyone know of just such a permanent label or the like? Maybe it IS objectName, but even when objectName is empty, the debugger still presents me with some top-level identifying name for it, so something somewhere must be keeping track of it.

    Extra-question; is there a better way to do this? The actions are created dynamically at construction of the dock widgets, and the translation works fine, but because they're not part of the UI I have to manually do a retranslation; it's working, but it's ugly (and also, sometimes what the user sees for an action is the tooltip, and sometimes the text, which so far is not a problem as they're always the same, but if they're different I won't be able to use the single objectName property to hold onto both the originals).

  • Lifetime Qt Champion


    One thing that is constant is the class name.

    As for the translation of such widgets what is generally done is to keep all actions/labels etc. that need translation as member variables and add a function that sets the text on them. You call it once at the end of the constructor to set the default text and again to react on QEvent::Language changed.

    Hope it helps

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.