Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Translations] Can't go back to english translation once i switch
Forum Updated to NodeBB v4.3 + New Features

[Translations] Can't go back to english translation once i switch

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nwoki
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nwoki
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Why are you using .toStdString().c_str() ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            1
            • SGaistS SGaist

              @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.

              N Offline
              N Offline
              nwoki
              wrote on last edited by
              #6

              @SGaist Yeah, i need to reload the tr strings as defined as the sourceText in order for it to work. In the end, I opted for a you 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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved