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. How to implement i18N whiling generating the GUI dynamically ?
Forum Update on Monday, May 27th 2025

How to implement i18N whiling generating the GUI dynamically ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.4k Views
  • 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.
  • cyberpunkerC Offline
    cyberpunkerC Offline
    cyberpunker
    wrote on last edited by
    #1

    We want to generating the GUI dynamically through reading XML files, and at the same time ,the GUI should support i18N , so , the linguist method CAN NOT be implemented .

    How to resolve this problem ?

    raven-worxR 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      UI files are xml files.
      http://doc.qt.io/qt-5/quiloader.html
      So its can already do what you want.

      Also the linguist way can work as it's already working with UI files.
      so if you use the tr() macro and do as docs says
      when loading a translator , it will work.

      The translation with linguist is a lookup system and all that is needed is to re-set the text if u change language.
      as seens in
      void retranslateUi(QMainWindow *MainWindow)
      {
      MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
      pushButton->setText(QApplication::translate("MainWindow", "Go", 0));
      } // retranslateUi

      So even if you use your own xml format and generate widgets on the fly from it, then
      the translator way, is still as valid as always. Same thing must happen. Re-apply the text.

      So im not sure i agree that "the linguist method CAN NOT be implemented " :)

      cyberpunkerC 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        UI files are xml files.
        http://doc.qt.io/qt-5/quiloader.html
        So its can already do what you want.

        Also the linguist way can work as it's already working with UI files.
        so if you use the tr() macro and do as docs says
        when loading a translator , it will work.

        The translation with linguist is a lookup system and all that is needed is to re-set the text if u change language.
        as seens in
        void retranslateUi(QMainWindow *MainWindow)
        {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
        pushButton->setText(QApplication::translate("MainWindow", "Go", 0));
        } // retranslateUi

        So even if you use your own xml format and generate widgets on the fly from it, then
        the translator way, is still as valid as always. Same thing must happen. Re-apply the text.

        So im not sure i agree that "the linguist method CAN NOT be implemented " :)

        cyberpunkerC Offline
        cyberpunkerC Offline
        cyberpunker
        wrote on last edited by
        #3

        I mean the GUI's character is stored in the xml files, NOT in the source code , and I designed all the GUI by C++,

        so the linguist method CAN NOT be implemented.

        1 Reply Last reply
        0
        • cyberpunkerC cyberpunker

          We want to generating the GUI dynamically through reading XML files, and at the same time ,the GUI should support i18N , so , the linguist method CAN NOT be implemented .

          How to resolve this problem ?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @cyberpunker
          QTranslator/tr()/... are all based on linguist which you do not want to use.

          Thus you will have to implement your custom translator, which reads the values from your XML file. Then instead of using Qt's tr() you use MyCustomTranslator::tr() which looks up the translation in the XML file (or better in a cached structure). If it can't find the value it simply returns the value.
          Whenever the loaded language in your custom translator changes, send an QEvent::LanguageChange event to the QApplicationInstance. This propagates the event to all widgets.
          Then listen in your widgets for the LanguageChange event and reapply the strings again.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          cyberpunkerC 1 Reply Last reply
          2
          • raven-worxR raven-worx

            @cyberpunker
            QTranslator/tr()/... are all based on linguist which you do not want to use.

            Thus you will have to implement your custom translator, which reads the values from your XML file. Then instead of using Qt's tr() you use MyCustomTranslator::tr() which looks up the translation in the XML file (or better in a cached structure). If it can't find the value it simply returns the value.
            Whenever the loaded language in your custom translator changes, send an QEvent::LanguageChange event to the QApplicationInstance. This propagates the event to all widgets.
            Then listen in your widgets for the LanguageChange event and reapply the strings again.

            cyberpunkerC Offline
            cyberpunkerC Offline
            cyberpunker
            wrote on last edited by cyberpunker
            #5

            @raven-worx

            OK , I got it .

            Also , what is " a cached structure" ? Any grammar indicate it ?

            And at the same time , GUI need to change languages dynamically, and if the other parts of the application implements the linguist way , I think this method does NOT conflict with it , right ?

            Thanks very much.

            raven-worxR 1 Reply Last reply
            0
            • cyberpunkerC cyberpunker

              @raven-worx

              OK , I got it .

              Also , what is " a cached structure" ? Any grammar indicate it ?

              And at the same time , GUI need to change languages dynamically, and if the other parts of the application implements the linguist way , I think this method does NOT conflict with it , right ?

              Thanks very much.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @cyberpunker said in How to implement i18N whiling generating the GUI dynamically ?:

              Also , what is " a cached structure" ? Any grammar indicate it ?

              i meant not to search the XML file everytime you request a translation. Instead load the XML values into memory (QHash, ....)

              And at the same time , GUI need to change languages dynamically, and if the other parts of the application implements the linguist way , I think this method does NOT conflict with it , right ?

              right.
              Only the LanguageChanged event is in common. So if you use this it might also trigger Linguist (re-)translations of course.
              You can overcome this by registering your custom event type and listen to this instead.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              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