qtqtranslator

How can I remove all QTranslator from the app?


I want to change the language using QCoreApplication::installTranslator with a few different .qm files for every language (different parts of the project result in different .qm files).

It is fine to use more than one .qm file :

QCoreApplication::installTranslator(QTranslator *translationFile)
Multiple translation files can be installed. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched first and the first translation file installed is searched last.

But, if I do not remove the older translators, they are still candidates for translations. Even though they would be the less recently installed translators.

How can I clear any file loaded previously before loading the wanted ones ?

The only way I see is to keep the pointers I installed and remove them one by one when I want to change, but is there something more straightforward ?


Solution

  • I maintain a list of the installed translators :

    // Install the translators
    for (auto fileName : qAsConst(fileList)) {
        auto translator = QSharedPointer<QTranslator>::create();
        translator->load(fileName);
    
        m_currentTranslators << translator;
        QCoreApplication::installTranslator(translator.data());
    }
    

    And to remove them all :

    for (auto translator : qAsConst(m_currentTranslators)) {
        QCoreApplication::removeTranslator(translator.data());
    }
    m_currentTranslators.clear();