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 load translation for static library widget
Forum Update on Monday, May 27th 2025

How to load translation for static library widget

Scheduled Pinned Locked Moved Solved General and Desktop
qtranslatorstatic library
32 Posts 5 Posters 14.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.
  • kshegunovK kshegunov

    @mbnoimi
    What about loading like this:

        translator->load("arabic", ":/languages");
    

    And as a second suggestion: I think you should create another QTranslator object for the second translation file.

    Kind regards.

    M Offline
    M Offline
    mbnoimi
    wrote on last edited by mbnoimi
    #21

    @kshegunov said:

    And as a second suggestion: I think you should create another QTranslator object for the second translation file.

    Thanks. This fixed Tester app. problem but it didn't fix the main problem (loading translation from static library) I still get this error message:

    Failed to load translation from static library

    kshegunovK 1 Reply Last reply
    0
    • M mbnoimi

      @kshegunov said:

      And as a second suggestion: I think you should create another QTranslator object for the second translation file.

      Thanks. This fixed Tester app. problem but it didn't fix the main problem (loading translation from static library) I still get this error message:

      Failed to load translation from static library

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #22

      @mbnoimi
      And have you tried the loading like this translator->load("arabic", ":/languages");, since you didn't comment on that?

      Read and abide by the Qt Code of Conduct

      M 2 Replies Last reply
      0
      • kshegunovK kshegunov

        @mbnoimi
        And have you tried the loading like this translator->load("arabic", ":/languages");, since you didn't comment on that?

        M Offline
        M Offline
        mbnoimi
        wrote on last edited by
        #23

        @kshegunov said:

        @mbnoimi
        And have you tried the loading like this translator->load("arabic", ":/languages");, since you didn't comment on that?

        Didn't work :( I get same error message

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #24

          Can I ask some silly maybe ?
          The list shows
          :/i18n/tester_ar.qm

          There is no "arabic" under languages
          there is
          ":/languages/i18n/WidgetKeyboard_ar.qm"

          1 Reply Last reply
          0
          • kshegunovK kshegunov

            @mbnoimi
            And have you tried the loading like this translator->load("arabic", ":/languages");, since you didn't comment on that?

            M Offline
            M Offline
            mbnoimi
            wrote on last edited by mbnoimi
            #25

            @kshegunov said:

            And have you tried the loading like this translator->load("arabic", ":/languages");, since you didn't comment on that?

            I tried to use translator->load(":/languages/i18n/WidgetKeyboard_ar") instead. The error message disappeared ("Failed to load translation from static library") but the widget comes from the static library still appears in English!

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #26

              Silly question number 2.
              The test calls ui->retranslateUi(this);
              void Tester::changeEvent(QEvent *event)
              {
              if (event->type() == QEvent::LanguageChange) {
              ui->retranslateUi(this);
              } else
              QWidget::changeEvent(event);
              }
              But tester ui->retranslate() knows about the widgets from the library ?
              retranslateUi() just call all ->setText for elements inserted on the UI.
              How can it know about widgets in the Lib?
              Who calls widget_in_lib->setText?

              M 1 Reply Last reply
              0
              • mrjjM mrjj

                Silly question number 2.
                The test calls ui->retranslateUi(this);
                void Tester::changeEvent(QEvent *event)
                {
                if (event->type() == QEvent::LanguageChange) {
                ui->retranslateUi(this);
                } else
                QWidget::changeEvent(event);
                }
                But tester ui->retranslate() knows about the widgets from the library ?
                retranslateUi() just call all ->setText for elements inserted on the UI.
                How can it know about widgets in the Lib?
                Who calls widget_in_lib->setText?

                M Offline
                M Offline
                mbnoimi
                wrote on last edited by
                #27

                @mrjj said:

                But tester ui->retranslate() knows about the widgets from the library ?
                retranslateUi() just call all ->setText for elements inserted on the UI.
                How can it know about widgets in the Lib?
                Who calls widget_in_lib->setText?

                Fair point. But as you can see from the code the widget in static library built manually not by using Qt Designer so retranslate doesn't exist and I don't know how to create an alternative to it :(

                mrjjM 1 Reply Last reply
                0
                • M mbnoimi

                  @mrjj said:

                  But tester ui->retranslate() knows about the widgets from the library ?
                  retranslateUi() just call all ->setText for elements inserted on the UI.
                  How can it know about widgets in the Lib?
                  Who calls widget_in_lib->setText?

                  Fair point. But as you can see from the code the widget in static library built manually not by using Qt Designer so retranslate doesn't exist and I don't know how to create an alternative to it :(

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #28

                  @mbnoimi
                  well for dynamic translation, meaning load translation AFTER keyboard been created would be to call
                  void WidgetKeyBoard::createKeyboard(void) again.
                  As it seems to be the only place with tr(text);
                  retranslateUi() is very simple in all programs. it simply just sets the texts again
                  and the tr function gets the new text from the active translation.
                  Alternatively you need enum all widgets (in keyboard), get the text and re-set it via tr / directly look up trans text.
                  But it can be messy if many types of Widgets as then u need to handle the "setText" for each.

                  M 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @mbnoimi
                    well for dynamic translation, meaning load translation AFTER keyboard been created would be to call
                    void WidgetKeyBoard::createKeyboard(void) again.
                    As it seems to be the only place with tr(text);
                    retranslateUi() is very simple in all programs. it simply just sets the texts again
                    and the tr function gets the new text from the active translation.
                    Alternatively you need enum all widgets (in keyboard), get the text and re-set it via tr / directly look up trans text.
                    But it can be messy if many types of Widgets as then u need to handle the "setText" for each.

                    M Offline
                    M Offline
                    mbnoimi
                    wrote on last edited by
                    #29

                    @mrjj said:

                    @mbnoimi
                    well for dynamic translation, meaning load translation AFTER keyboard been created would be to call
                    void WidgetKeyBoard::createKeyboard(void) again.

                    I tried to call createKeyboard() once again as following but still gives same result although this function calls tr() so it suppose to do what you talked about!

                    void Tester::changeEvent(QEvent *event)
                    {
                        if (event->type() == QEvent::LanguageChange) {
                            ui->retranslateUi(this);
                            myKeyboard->createKeyboard();
                        } else
                            QWidget::changeEvent(event);
                    }
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #30

                      Hi
                      Maybe you can
                      delete myKeyboard;
                      myKeyboard = new Keyboard(this);
                      as im not sure if calling createKeyboard() for same instance is 100% ok. (did not try it)

                      Then there is the context thing.
                      for mainwindow, its "mainwindow" (doh), but I do wonder about the translation that comes from static.

                      Since they are linked into "main", I assume its context is the same as main (tester) but if the
                      qm is different, it will not work.

                      So open them (qms) in Linguist and check the context they were extracted as.

                      then try with
                      http://doc.qt.io/qt-5/qcoreapplication.html#translate

                      To test a translation with that context and see if it does resolve.

                      First then I would worry about tr working in createKeyboard()

                      If I may say so.

                      update:
                      Context is "WidgetKeyBoard"
                      So calling createKeyboard() should be in the right context as we are in that class.

                      So testing with qcoreapplication.html#translate
                      to see if all is loaded and happy.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mbnoimi
                        wrote on last edited by
                        #31

                        Thank you guys, I'm no longer work on this project since Digia announcement.

                        mrjjM 1 Reply Last reply
                        0
                        • M mbnoimi

                          Thank you guys, I'm no longer work on this project since Digia announcement.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #32

                          @mbnoimi
                          Ok, why if I can ask?

                          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