How to use Qt translations directly with QApplication :: tr ()

16

In an application developed in Qt I have a non-visual class (that is, it is not inherited from a QWidget ) but it handles text strings that must be presented to the user. To use the Qt translation engine, I defined all strings using the QApplication::tr() function.

The strings are correctly displayed in the Linguist tool, under the context of QApplication (as expected - see image below). However, when using code I change the Locale of the application, only the strings under the context of MainWindow are changed.

ThecodeIusetochangethelanguageisasfollows:

void MainWindow::setLocale(QString sLocale) { QTranslator *pTranslator = m_mpTranslators[sLocale]; if(pTranslator) { for(map<QString, QTranslator*>::iterator it = m_mpTranslators.begin(); it != m_mpTranslators.end(); ++it) { qApp->removeTranslator(it->second); QApplication::removeTranslator(it->second); // Essa linha não existia antes! } qApp->installTranslator(pTranslator); QApplication::installTranslator(pTranslator); // Essa linha também não! if(sLocale == "pt_BR") m_pLocaleButton->menuAction()->setIcon(QIcon(":/resources/icons/pt_BR")); else m_pLocaleButton->menuAction()->setIcon(QIcon(":/resources/icons/en_UK")); ui->retranslateUi(this); updateUI(); } }

I think it's important to note that changing text in the graphical interface is immediate (via ui->retranslateUi(this) and updateUI() calls), but non-visual class calls occur after the locale , then the text should be translated correctly.

In the code above, I also included the lines marked with comments, but that did not cause the translation to accompany the locale defined.

Does anyone have any idea where I might be mistaken?

Editing:

The version of Qt is 5.1.0 (32 bit), and I'm running on Windows 7 (64 bit). Translators are created in the constructor of class MainWindow , as follows:

// Setup the translation system
QTranslator *pPortuguese = new QTranslator(this);
QString sPortuguese = QCoreApplication::applicationFilePath().replace(".exe", "_pt_BR.qm");
if(pPortuguese->load(sPortuguese))
    m_mpTranslators.insert(map<QString, QTranslator*>::value_type(QString("pt_BR"), pPortuguese));
else
    qWarning() << "Portuguese translation file not found";

QTranslator *pEnglish = new QTranslator(this);
QString sEnglish = QCoreApplication::applicationFilePath().replace(".exe", "_en_UK.qm");
if(pEnglish->load(sEnglish))
    m_mpTranslators.insert(map<QString, QTranslator*>::value_type(QString("en_UK"), pEnglish));
else
    qWarning() << "English translation file not found";

The m_mpTranslators attribute is a simple map that relates the locale string to the translator: std::map<QString, QTranslator*> m_mpTranslators;

    
asked by anonymous 11.12.2013 / 18:59

2 answers

9

The problem really came from a failure (my mistake, sorry!) in updating the translation files (* .qm) in the target directory of the build. In the .pro file there were no instructions for automated copying of the translations to the destination directories, so the tests used an earlier version of the translations (which I had probably copied manually). This resulted in the incorrect display of the texts regardless of the context used.

To solve the problem definitely , I changed the .pro file to include in the release settings and debug the automated copy of the files to the correct directories: >

. . .

copyfiles.commands += @echo Copying translation files... &

# Configurações para Windows
win32{

    . . .

    # Troca '/' por '\' nos diretórios de origem e destino
    SOURCE_WIN = $${PWD}
    SOURCE_WIN ~= s,/,\,g
    TARGET_WIN = $${OUT_PWD}
    TARGET_WIN ~= s,/,\,g

    debug_and_release {
        CONFIG -= debug_and_release
        CONFIG += debug_and_release
    }

    # Configurações de debug
    CONFIG(debug, debug|release) {
        CONFIG -= debug release
        CONFIG += debug

        . . .

        copyfiles.commands += @call copy $${SOURCE_WIN}\*.qm $${TARGET_WIN}\debug\
    }

    # Configurações de release
    CONFIG(release, debug|release) {
        CONFIG -= debug release
        CONFIG += release

        . . .

        copyfiles.commands += @call copy $${SOURCE_WIN}\*.qm $${TARGET_WIN}\release\
    }

}

# Inclui os comandos de cópia das traduções na execução do QMake
QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles

. . .
    
12.12.2013 / 13:55
4

I use an older version, but as far as I understand, the TR call in your class is wrong.

The documentation says that if your class is not a subclass of QObject , use TR of QCoreApplication :

  

If the quoted text is not in a member function of a QObject subclass,   use either the tr () function of an appropriate class, or the   QCoreApplication :: translate () function directly

The question is: in what way do you call it? Below is the documentation example:

 qApp->translate("LoginWidget", "Password:"),
             logwid);

I find it interesting that this example above should use an instance of QApplication , and it uses translate instead of TR .

    
11.12.2013 / 20:50