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. Translation issue: use another file for a certain form
QtWS25 Last Chance

Translation issue: use another file for a certain form

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.8k 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.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    Hello guys,

    I am developing a program where at the beginning I should use a translation file. However, I have to change the translation file for a certain class (Game). In other words, I want to separate all gui translations into a file and this game translation in another file.

    Steps :
    1- separted the game from the program
    2- build the translation file of the game, translated and made the release.
    3- Problem --->> Translation is not shown. I need help. In the main constructor I placed the following code

    @
    QApplication a(argc, argv);

    KeyBoardMap *w = new Game();
    QTranslator translator;
    translator.load("ab_ab.qm");
    a.installTranslator(&translator);
    

    @

    Is this correct?

    I should then merge this game class into the other software, can I load a translation file from a form.cpp and not from main.cpp?

    thanks

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      does your ab_ab.qm file exist in the same folder where your executable is?

      bq. I should then merge this game class into the other software, can I load a translation file from a form.cpp and not from main.cpp?

      Sure you can, but be sure to create your QTranslator instance on the heap... and use qApp() instead of a

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

        Yes the path is correct, here is a simple example where the complete path is removed.

        how to use qApp? It does not accept

        qApp.installTranslator ?

        do you mean the form that loads the game class should be an Application class?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on last edited by
          #4

          bq. Yes the path is correct, here is a simple example where the complete path is removed.

          Do you actually use QObject::tr() or translate() in your code for strings which needs to be translated?

          bq. how to use qApp? It does not accept

          @
          qApp->installTranslator
          @

          It is only needed if you will load your translation elsewhere when in main function...

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on last edited by
            #5

            And I have forgot to say:

            bq. Note that the translator must be created before the application's widgets.

            God is Real unless explicitly declared as Integer.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SherifOmran
              wrote on last edited by
              #6

              Thank you, now it works

              @
              QTranslator* translator = new QTranslator;
              translator->load("/.../.../.../abc.qm");
              qApp->installTranslator(translator);
              @

              But it works from inside the form. Will this be global, I mean will other forms use the same translation file ? should I load the other translation file again after this form quits?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AcerExtensa
                wrote on last edited by
                #7

                It is application global, but you must be sure what QTranslator instance exist till you unload translation or loads another one... Make it global or singleton.

                God is Real unless explicitly declared as Integer.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Macro
                  wrote on last edited by
                  #8

                  Please edit your Question and add [Solved] infront of it

                  Thanks and Regards...

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

                    Hi SherifOmran,

                    you can use multiple QTranslator instances in one process, that is no problem. Just the translations must be unique. If you use tr() within a QObject, the class name is also part of the search logic, which makes it easier. If you use QObject::translate(...) you add a context as parameter which has the same meaning as the class in a QObject derived class (which is used as context there).

                    So using multiple qm files for different classes / libraries is no problem. At least, my apps always load 2 (my local one and one for Qt).

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    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