PDA

View Full Version : QT application with dinamic language (QTranslator) does not work



ddonate
25th May 2016, 10:04
I have a QT application and I need to change language at runtime. I am using QTranslator and .qm files. The language is loaded properly but my widgets don't show the text.

Y have 'MyClass..cpp' (inherits QMainWindow):



MyClass::MyClass()
{
...
selectLanguage("en");

m_pLabel = new QLabel(tr("User"));
m_pLabel->show();

...
}

void MyClass::selectLanguage(QString language) {

QString sTranslationFile = QString("texts_%1").arg(language);

QTranslator translator;
//translator.load(":/translations/" + sTranslationFile);
translator.load(sTranslationFile, ":/translations/");

if (qApp->installTranslator(&translator))
qDebug() << "Ok";
}

void MyClass::changeEvent(QEvent *pEvent)
{
if(pEvent)
{
switch(pEvent->type())
{
case QEvent::LanguageChange:
retranslate();
break;
}
}

QMainWindow::changeEvent(pEvent);
}

void MyClass::retranslate()
{
qDebug() << "retranslate";

if (m_pLabel)
m_pLabel->setText(tr("User"));
}


In 'myProject.pro':



...
QMAKE_POST_LINK = lrelease.exe myProject.pro
...
RESOURCES += \
resources/resources.qrc

TRANSLATIONS += resources/translations/texts_en.ts \
resources/translations/texts_es.ts
...
DISTFILES += \
...
resources/translations/texts_en.qm \
resources/translations/texts_es.qm
In 'resources.qrc':

...
translations/texts_en.qm
translations/texts_es.qm


In 'texts_en.ts':



<!DOCTYPE TS><TS>
<context>
<name>MyClass</name>
<message>
<source>User</source>
<translation>UserEn</translation>
</message>
<message>
<source>Group</source>
<translation>GroupEn</translation>
</message>
</context>
</TS>


Language is loaded ("Ok" appears) and "retranslate" is called, but the label does NOT show "UserEn", but "User"...

I call 'selectLanguage("es")' after a while, but the same...

I hope somebody could help me.

Thanks in advance,

Diego

yeye_olive
25th May 2016, 10:36
You claim that the translations are successfully installed, but your code does not check the return value of QTranslator::load().

Anyway, the main problem is that MyClass::selectLanguage() allocates the QTranslator on the stack, which means that it is destroyed as soon as you exit the function. Qt's documentation is weak when it comes to specifying lifetime requirements, but it seems to me that the QTranslator passed to QCoreApplication::installTranslator() must live until it is removed or the QCoreApplication itself is destroyed, whichever comes first.

ChrisW67
30th May 2016, 21:59
The observation that installTranslator() accepts a pointer and not a "const QTranslator&" is a fairly good indicator that it does not take a copy. I agree that perhaps the doc should say that and also whether the QCoreApplication takes ownership of the QTranslator instance.

anda_skoa
31st May 2016, 09:14
The observation that installTranslator() accepts a pointer and not a "const QTranslator&" is a fairly good indicator that it does not take a copy.
And QTranslator being a QObject derived class, which means it cannot even be copied.

Cheers,
_