qt QTranslator reuse

qt, runtime, translation

Solution

QTranslator::load basically in simple sense can be considered as a function that opens a given `.qm` file, reads in all the translated values and loads it in for a specific language.

Now in general operation you would not want to reuse this for many languages as by "reusing" (even if its allowed) your adding the overhead of parsing this given `.qm` file for every language every time you switch your UI language, which is just basically an overhead you don't need.

Your assumption of creating a `QTranslator` for each language is correct. As for your side question, Yes you can also re-use it. Thats the benefit of having individual `QTranslator` objects per translation. Just call qApp->removeTranslator() with the current translation and then `qApp->installTranslator()` with the new one. This way you are reusing the loaded translations as and when you please.

The way we structure this is by sub-classing `QApplication` and adding 2 functions

void Application::CreateTranslators() {
  // translators_ is a QMap<QString, QTranslator*>
  if (!translators_.isEmpty())  
    return;
  QStringList languages;
  languages << "en" << "ar" << "zh";
  foreach(QString language, languages) {
    QTranslator* translator = new QTranslator(instance());
    translator->load(language);
    translators_.insert(language, translator);
  }
}

Now this function is called at the very start of the application.

2nd function is as following

void Application::SwitchLanguage(QString language) {
  // current_translator_ is a QTranslator*
  if (current_translator_)
    removeTranslator(current_translator_);

  current_translator_ = translators_.value(language, nullptr);
  if (current_translator_)
    installTranslator(current_translator_);
}

That's it. Using the second function you can switch your language at run-time as you please.

Couple things you'll also need to be aware of is changing `QTranslator` at run-time will update all translations from your `.ui` file strings marked as translatable automatically, however those set from code will not be. To get that you will have to override QWidget::changeEvent() and then check if the event is of type `QEvent::LanguageChange` and then set the required strings for that `QWidget` accordingly (Don't forget the `tr()` while doing so)

Problem

I noticed that the Qt documentation is not very verbose on some of the aspects of the translations. I was fooling around with it trying to figure out their behaviour using trial & error. The ultimate goal is to get the translation changed on runtime but I am very confused as to what extent the QTranslator object can be re-used. Consider this (where 'a' is the main instance of the application): ``` QTranslator translator; translator.load("mytranslation_cz"); a.installTranslation(&translator); (...) a.removeTranslation(&translator) ``` Now the translator was removed from the application but what happened to the translator object? In my tests when above code was followed by this again ``` translator.load("mytranslation_fr"); a.installTranslation(&translator); ``` it did not do anything in main() and it crashed the application when called from one of the widgets (using pointer to main app). Therefore I am suspecting that I would need to create one QTranslator object per translation I want to load in the application and that I cannot reuse the QTranslator object. Am I right in this assumption? And as a side question. Assuming the QTranslator object is untouched by the removeTranslation(), is it possible to simply install it later again like this? ``` QTranslator translator; QTranslator translator1; translator.load("mytranslation_cz"); translator1.load("mytranslation_fr"); a.installTranslation(&translator); (...) a.removeTranslation(&translator); a.installTranslation(&translator1); (...) a.removeTranslation(&translator1); a.installTranslation(&trasnlator); //Will this work? ``` Thanks for any clarification as I am somewhat confused as to what happens to the QTranslation objects when installing and removing translations from the application and especially if the QTranslation object can be reused for multiple translations somehow on runtime?

Original source