19
In an application developed in Qt I have a non-visual class (i.e., that is not inherited from a QWidget
) but that handles text strings that must be presented to the user. To use the Qt translation engine, I set all strings using the function QApplication::tr()
.
Strings are correctly displayed in the tool Linguist
, in the context of QApplication
(as expected - see image below). However, when I see code I change the Locale
of the application, only the strings under the context of MainWindow
are amended.
The code I use to change the language is as follows::
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 is important to note that the change of the texts in the graphical interface is immediate (via calls for ui->retranslateUi(this)
and updateUI()
), but calls from non-visual class occur later the change of the locale, then the text should be translated correctly.
In the code above, I also came to include the lines marked with comments, but this did not make the translation follow 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 am running on Windows 7 (64 bit). Translators are created in the class constructor 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;
What version of QT do you use, and where does the Translator? I see
mpTranslators
, but where and how this variable receives the Translator?– Marcos Zolnowski
Sorry @bigown. I fixed it. :)
– Luiz Vieira
@Marcoszolnowski, I edited the question to include the information you requested. As I said, the translations work properly for the graphical interface. They do not only work for texts extracted via Qapplication::tr().
– Luiz Vieira
Downvote for not writing Qt would be worse @bigown :)
– Marcos Zolnowski