Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translation not working in Qt

I have been all day long googling for a solution and changing my code, but no luck.

Basically, I have added translation to my app. It is working fine except here:

QString MainWindow::getMessage(Messages msg) {
    static const char* const messages[] = {
        QT_TR_NOOP("Setting power on"),
        QT_TR_NOOP("Reading ID..."),
        QT_TR_NOOP("Programming..."),
        QT_TR_NOOP("Setting write-protect"),
        QT_TR_NOOP("Finished ok"),
        QT_TR_NOOP("PROGRAMMED OK"),
        QT_TR_NOOP("ERROR!"),
        QT_TR_NOOP("OK"),
        QT_TR_NOOP("The programmer is not connected.\nPlease check the connection."),
        QT_TR_NOOP("Disconnect the board, it is in short!!"),
        QT_TR_NOOP("ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."),
        QT_TR_NOOP("Board is already programmed and write protected."),
        QT_TR_NOOP("Check device connection or there is short."),
        QT_TR_NOOP("Unknown error.")    
    };

    return tr(messages[msg]);
}

However, I don't get the translation. The files for translation seems to be ok, the UI translations are applied fine. I also tried this:

static const char* const messages[] = {
    QT_TRANSLATE_NOOP("test", "Setting power on"),
    QT_TRANSLATE_NOOP("test", "Reading ID..."),
    QT_TRANSLATE_NOOP("test", "Programming..."),
    QT_TRANSLATE_NOOP("test", "Setting write-protect"),
    QT_TRANSLATE_NOOP("test", "Finished ok"),
    QT_TRANSLATE_NOOP("test", "PROGRAMMED OK"),
    QT_TRANSLATE_NOOP("test", "ERROR!"),
    QT_TRANSLATE_NOOP("test", "OK"),
    QT_TRANSLATE_NOOP("test", "The programmer is not connected.\nPlease check the connection."),
    QT_TRANSLATE_NOOP("test", "Disconnect the board, it is in short!!"),
    QT_TRANSLATE_NOOP("test", "ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."),
    QT_TRANSLATE_NOOP("test", "Board is already programmed and write protected."),
    QT_TRANSLATE_NOOP("test", "Check device connection or there is short."),
    QT_TRANSLATE_NOOP("test", "Unknown error.")
};

Then, I have a method to get the message:

QString MainWindow::getMessage(Messages msg) {
    return qApp->translate("test", messages[msg]); 
}

But it doesn't work either.

Any tips or suggestions?

like image 387
Leonardo Giordano Avatar asked Feb 13 '15 21:02

Leonardo Giordano


2 Answers

I have found the real issue here. Usually translators are installed at main.cpp, so the translator object remains in memory. However, in my case, I was switching the translator after the user changes the language at settings dialog, using a local variable but void QCoreApplication::installTranslator ( QTranslator * translationFile ) [static] needs a pointer. That local variable is removed as soon as the function exits. So, I declared a QTranslator object on my class, so it keeps in memory.

like image 158
Leonardo Giordano Avatar answered Oct 19 '22 02:10

Leonardo Giordano


Maybe not applicable in this situation, but often people forget to place the Q_OBJECT macro in the class declaration. Resulting in (amongst others) tr() not working.

like image 2
Maarten Avatar answered Oct 19 '22 03:10

Maarten