Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Interface translation does not work before QApplication starts. Is there any way around this?



  • Before the QApplication object starts (the exec () method), I need to prepare certain files that are needed to start correctly so that the application can find them.

    Files preparation requires a user choice with the question "where to extract the necessary files?". Accordingly, you need to open a window in which there will be a selection interface. This window is inherited from QDialog, it works even before the main application is launched.

    But here is the problem: in this window there is a code for automatically detecting the system language. And this code is works, and it defines the language, and installs language to the application. This code is call at first line in the window constructor:

    void InstallDialog::setupAutoLangTranslation()
    {
        QString lang=QLocale().system().name().split('_').first().toLower();
        QStringList availableLang={"en", "ru"};
    
        if( !availableLang.contains(lang) )
            lang="en";
    
        qDebug() << "Auto detect lang in installator: " << lang;
    
        QString langFileName=":/resource/translations/mytetra_"+lang+".qm";
        qDebug() << "Lang file: " << langFileName;
    
        QTranslator langTranslator;
        bool loadResult=langTranslator.load(langFileName);
    
        if(loadResult)
        {
            qDebug() << "Success load translation file";
            qApp->installTranslator(&langTranslator);
        }
        else
            qDebug() << "Can't load translation file";
    }
    

    All strings settings in the class of this window occur after this code. Naturally, all strings are wrapped in tr(), and running lupdate, linguist, lrelease.

    However, the translation of the interface for this dialogue does not occur.

    I thought that the translation does not work due to the fact that the language setting is done in the window constructor. However, inside the window class, there is a call to another QMessageBox dialog (created on stack):

    void InstallDialog::onAccepted()
    {
        if( !ui->m_radioButtonStandart->isChecked() and
            !ui->m_radioButtonPortable->isChecked() )
        {
            QMessageBox msgBox;
            msgBox.setText(tr("Please select one of install mode."));
            msgBox.exec();
        }
        else
        {
            this->accept();
        }
    }
    

    And in this window, too, the translation does not work.

    I now only sin that the translation does not work because the application object was created, but its main loop is not running. And the translation application make somewhere in the depths of the launch of the main cycle.

    The question is, is it possible to somehow get out of this problem and make the Qt localization system work for there windows? Maybe there is some method that will update the localization system without starting the main application loop? Or maybe there is some other method?

    I really would not want to fundamentally restructure my application because of such a peculiarity behavior of Qt.

    Sources: https://github.com/xintrea/mytetra_dev

    Source places:

    /app/src/views/installDialog/InstallDialog.h
    /app/src/views/installDialog/InstallDialog.cpp
    /app/src/libraries/GlobalParameters.cpp, initWorkDirectory () method
    /app/src/main.cpp, function main (), calling globalParameters.init (); 
    

    Note: For show the problematic window to appear, ~ /.config/mytetra directory must not exist.



  • Solution: QTranslator was installed in QApplication via installTranslator (), but was immediately deleted when the setupAutoLangTranslation () method exited, because it was on the stack.

    It is enough to make QTranslator as a property of the InstallDialog dialog so that it exists all the time the window works, and the translation starts to work.


Log in to reply