[Translations] Can't go back to english translation once i switch
-
Strange problem with translations here. I can correctly switch from the default translation (english in my c++ code) to Italian (via the .qm file) but when I try to load the
app_en.qm
file again I don't get any string changes for my widgets and actions. Here are the code snippets used for loading the translation files.main.cpp - // Setup translation - setup the initial translation from last preference used by the user QTranslator translator; QString langCode = "it"; /* TODO - take from settings */ if (translator.load(QString("dvo_%1.qm").arg(langCode), QString("%1%2%3").arg(QApplication::applicationDirPath()).arg(QDir::separator()).arg(TRANSLATION_FOLDER))) { QApplication::installTranslator(&translator); }
mainwindow.cpp void MainWindow::onLanguageChanged(QAction *action) { if (action == nullptr) { return; } QString langCode = action->data().toString(); qApp->removeTranslator(&m_translator); if (m_translator.load(QString("dvo_%1.qm").arg(langCode), QString("%1%2%3").arg(QApplication::applicationDirPath()).arg(QDir::separator()).arg(TRANSLATION_FOLDER))) { qDebug() << "YEAH! LOADED NEW TRANSLATION FILE!: " << langCode; qApp->installTranslator(&m_translator); } else { qDebug("SORRY; COULDN'T LOAD IT THIS ITME!"); } }
Once this is done, the
changeEvent
slot is activated. Slot which i reimplemented as follows:void MainWindow::changeEvent(QEvent *event) { if (!event) { return; } // handle our language change if (event->type() == QEvent::LanguageChange) { ui->retranslateUi(this); // translate actions foreach (QAction *action, findChildren<QAction*>()) { action->setText(tr(action->text().toStdString().c_str())); } // translate explorer (and if already open) m_explorer->updateTranslations(); } QMainWindow::changeEvent(event); }
Considering the actions, what I think might be causing the problem is the
action->setText(tr(action->text().toStdString().c_str()));
line. I create my actions as follows:m_fileMenu->addAction(QIcon(":/assets/icons/buttons/new.svg"), tr("&New")); m_fileMenu->addAction(QIcon(":/assets/icons/buttons/load.svg"),tr("&Open")); m_fileMenu->addAction(QIcon(":/assets/icons/buttons/save.svg"),tr("&Save")); m_fileMenu->addAction(QIcon(":/assets/icons/buttons/save.svg"),tr("&SaveAs")); m_fileMenu->addAction(QIcon(":/assets/icons/buttons/stampa.png"), tr("&Print"));
so the only way I found to update the translations for them was to cycle through ad set their text. This works the first time around, but once i switch, it's not possible anymore. Any ideas on how to do this correctly?
-
So the error seems to be the fact that when I call
action->setText(tr(action->text().toStdString().c_str()));
i'm using the translated text and not the original source text used for mapping the translation. Other than stashing the original strings somewhere and using them when switching, is there another "cleaner" solution for this? -
Hi,
Why are you using
.toStdString().c_str()
? -
Not sure of the current state of Qt, but under Qt4, when I had to do I18N stuff, the text strings did seem to be locked to whatever the initial translation was. We had to restart the app to change translations.
-
@Kent-Dorfman Dynamic translation was already possible be required to handle the LanguageChange event.
@nwoki Just realised one thing, you're "doing it wrong".
If you look again at the implementation of the changeEvent method in the Dynamic Translation part of the Internationalisation documentation, you'll see that the text are set there. You can also call a method that sets all the texts for your different actions/widgets but don't try to reload the content as you do currently.
-
@Kent-Dorfman Dynamic translation was already possible be required to handle the LanguageChange event.
@nwoki Just realised one thing, you're "doing it wrong".
If you look again at the implementation of the changeEvent method in the Dynamic Translation part of the Internationalisation documentation, you'll see that the text are set there. You can also call a method that sets all the texts for your different actions/widgets but don't try to reload the content as you do currently.
@SGaist Yeah, i need to reload the
tr
strings as defined as thesourceText
in order for it to work. In the end, I opted for ayou have to restart the application in order for changes to take effect
and load the required/desired language on startup. Saved me having to reset text for all my non .ui based components and writing a bunch of extra code to do that.