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 aren't working
Forum Updated to NodeBB v4.3 + New Features

Translations aren't working

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.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.
  • T Offline
    T Offline
    thsprppr
    wrote on last edited by
    #1

    I'm sure I'm doing something wrong, but for the life of me I can't figure out what it is.

    I have a CMake project where I'm creating and adding TS files with qt5_create_translation and qt5_add_translation. Here's how my qt5_create_translation call looks:

    qt5_create_translation(QM_FILES ${SOURCES} ${TS_FILES})
    

    The TS files seem to generate properly and I can translate as normal in Qt Linguist. I then include them in the build with a resource file that looks like this:

    <RCC>
      <qresource prefix="/ts">
        <file>en_GB.qm</file>
      </qresource>
    </RCC>
    

    The TS files seem to release and compile correctly. I get output that looks like this, which seems correct:

    Updating '/Users/thsprppr/app/ts/en_GB.qm'...
        Generated 5 translation(s) (3 finished and 2 unfinished)
        Ignored 842 untranslated source text(s)
    

    The application can also see the file exists if I call QFileInfo::exists(":/ts/en_GB.qm") (returns true) and QTranslate::language() even correctly determines the locale is en_GB (unless it's just getting that from the filename.

    But no matter which way I try to load it, none of the lines seem to translate. I've read dozens of threads and tried everything I could find but nothing seems to work.

    Here's the code I've got in main()

    QApplication* a = new QApplication(argc, argv);
    QTranslator t;
    qDebug() << t.load(":/ts/en_GB.qm");
    qDebug() << QCoreApplication::installTranslator(&t);
    

    This is before any GUI objects are created and immediately after the QApplication process is created. All UI strings are wrapped in tr(). Both of those qDebug() lines return true, and this is in main() so the QTranslator shouldn't be going out of scope. Calling QTranslator::language() even correctly returns the language I set for this file (unless it's just getting that from the filename). I also made sure to add the Q_OBJECT macro to any objects that I was warned didn't already have it. Yet none of my text is being translated whatsoever.

    Since neither of those calls are providing errors, my current guess is the lines just aren't being matched somehow? I'm not sure if my qt5_create_translation line is correct, but it is as far as I can tell.

    I really have no idea what's going on. Any help would be very much appreciated.

    J.HilkJ 1 Reply Last reply
    0
    • T thsprppr

      I'm sure I'm doing something wrong, but for the life of me I can't figure out what it is.

      I have a CMake project where I'm creating and adding TS files with qt5_create_translation and qt5_add_translation. Here's how my qt5_create_translation call looks:

      qt5_create_translation(QM_FILES ${SOURCES} ${TS_FILES})
      

      The TS files seem to generate properly and I can translate as normal in Qt Linguist. I then include them in the build with a resource file that looks like this:

      <RCC>
        <qresource prefix="/ts">
          <file>en_GB.qm</file>
        </qresource>
      </RCC>
      

      The TS files seem to release and compile correctly. I get output that looks like this, which seems correct:

      Updating '/Users/thsprppr/app/ts/en_GB.qm'...
          Generated 5 translation(s) (3 finished and 2 unfinished)
          Ignored 842 untranslated source text(s)
      

      The application can also see the file exists if I call QFileInfo::exists(":/ts/en_GB.qm") (returns true) and QTranslate::language() even correctly determines the locale is en_GB (unless it's just getting that from the filename.

      But no matter which way I try to load it, none of the lines seem to translate. I've read dozens of threads and tried everything I could find but nothing seems to work.

      Here's the code I've got in main()

      QApplication* a = new QApplication(argc, argv);
      QTranslator t;
      qDebug() << t.load(":/ts/en_GB.qm");
      qDebug() << QCoreApplication::installTranslator(&t);
      

      This is before any GUI objects are created and immediately after the QApplication process is created. All UI strings are wrapped in tr(). Both of those qDebug() lines return true, and this is in main() so the QTranslator shouldn't be going out of scope. Calling QTranslator::language() even correctly returns the language I set for this file (unless it's just getting that from the filename). I also made sure to add the Q_OBJECT macro to any objects that I was warned didn't already have it. Yet none of my text is being translated whatsoever.

      Since neither of those calls are providing errors, my current guess is the lines just aren't being matched somehow? I'm not sure if my qt5_create_translation line is correct, but it is as far as I can tell.

      I really have no idea what's going on. Any help would be very much appreciated.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @thsprppr
      have you tried

      QTranslator t;
      qDebug() << t.load("en_GB.qm", ":/ts/");
      

      does this return true?
      also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      T 1 Reply Last reply
      0
      • KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

        @thsprppr said in Translations aren't working:

        Here's the code I've got in main()
        QApplication* a = new QApplication(argc, argv);
        QTranslator t;
        qDebug() << t.load(":/ts/en_GB.qm");
        qDebug() << QCoreApplication::installTranslator(&t);

        Why are you doing QApplication* a = new QApplication(argc, argv);?
        Don't make sense to me.
        Is this you main() code?

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          hi @thsprppr
          have you tried

          QTranslator t;
          qDebug() << t.load("en_GB.qm", ":/ts/");
          

          does this return true?
          also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)

          T Offline
          T Offline
          thsprppr
          wrote on last edited by thsprppr
          #4

          @J-Hilk said in Translations aren't working:

          hi @thsprppr
          have you tried

          QTranslator t;
          qDebug() << t.load("en_GB.qm", ":/ts/");
          

          does this return true?
          also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)

          Yes, I mention in the OP that both qDebug()s return true and that this is indeed inside main(). I also tried QTranslator* t = new QTranslator(); just to test it and it made no difference.

          @KroMignon said in Translations aren't working:

          @thsprppr said in Translations aren't working:

          Here's the code I've got in main()
          QApplication* a = new QApplication(argc, argv);
          QTranslator t;
          qDebug() << t.load(":/ts/en_GB.qm");
          qDebug() << QCoreApplication::installTranslator(&t);

          Why are you doing QApplication* a = new QApplication(argc, argv);?
          Don't make sense to me.
          Is this you main() code?

          Yes, I mention in the OP that this is in main(). I'm using new because my application has a headless mode that creates a new QCoreApplication, but I removed it for clarity. If it matters to you, this is the full snippet:

          QCoreApplication* a;
          
          if (!headless) {
            a = new QApplication(argc, argv);
          } else {
            a = new QCoreApplication(argc, argv);
          }
          
          QTranslator t;
          qDebug() << t.load(":/ts/en_US");
          qDebug() << QCoreApplication::installTranslator(&t);
          
          J.HilkJ KroMignonK 2 Replies Last reply
          0
          • T thsprppr

            @J-Hilk said in Translations aren't working:

            hi @thsprppr
            have you tried

            QTranslator t;
            qDebug() << t.load("en_GB.qm", ":/ts/");
            

            does this return true?
            also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)

            Yes, I mention in the OP that both qDebug()s return true and that this is indeed inside main(). I also tried QTranslator* t = new QTranslator(); just to test it and it made no difference.

            @KroMignon said in Translations aren't working:

            @thsprppr said in Translations aren't working:

            Here's the code I've got in main()
            QApplication* a = new QApplication(argc, argv);
            QTranslator t;
            qDebug() << t.load(":/ts/en_GB.qm");
            qDebug() << QCoreApplication::installTranslator(&t);

            Why are you doing QApplication* a = new QApplication(argc, argv);?
            Don't make sense to me.
            Is this you main() code?

            Yes, I mention in the OP that this is in main(). I'm using new because my application has a headless mode that creates a new QCoreApplication, but I removed it for clarity. If it matters to you, this is the full snippet:

            QCoreApplication* a;
            
            if (!headless) {
              a = new QApplication(argc, argv);
            } else {
              a = new QCoreApplication(argc, argv);
            }
            
            QTranslator t;
            qDebug() << t.load(":/ts/en_US");
            qDebug() << QCoreApplication::installTranslator(&t);
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @thsprppr ok, what platform are you targeting?
            IIRC some platforms do not support loading translation files from resources, for what ever reasons.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            T 1 Reply Last reply
            0
            • T thsprppr

              @J-Hilk said in Translations aren't working:

              hi @thsprppr
              have you tried

              QTranslator t;
              qDebug() << t.load("en_GB.qm", ":/ts/");
              

              does this return true?
              also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)

              Yes, I mention in the OP that both qDebug()s return true and that this is indeed inside main(). I also tried QTranslator* t = new QTranslator(); just to test it and it made no difference.

              @KroMignon said in Translations aren't working:

              @thsprppr said in Translations aren't working:

              Here's the code I've got in main()
              QApplication* a = new QApplication(argc, argv);
              QTranslator t;
              qDebug() << t.load(":/ts/en_GB.qm");
              qDebug() << QCoreApplication::installTranslator(&t);

              Why are you doing QApplication* a = new QApplication(argc, argv);?
              Don't make sense to me.
              Is this you main() code?

              Yes, I mention in the OP that this is in main(). I'm using new because my application has a headless mode that creates a new QCoreApplication, but I removed it for clarity. If it matters to you, this is the full snippet:

              QCoreApplication* a;
              
              if (!headless) {
                a = new QApplication(argc, argv);
              } else {
                a = new QCoreApplication(argc, argv);
              }
              
              QTranslator t;
              qDebug() << t.load(":/ts/en_US");
              qDebug() << QCoreApplication::installTranslator(&t);
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @thsprppr said in Translations aren't working:

              QTranslator t;
              qDebug() << t.load(":/ts/en_US");
              qDebug() << QCoreApplication::installTranslator(&t);

              According to documenation:

              Usually, it is better to use the QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) function instead, because it uses QLocale::uiLanguages() and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.

              I think you should change the load to:

              QTranslator t;
              qDebug() << t.load(QLocale(), ":/ts/en_US");
              qDebug() << QCoreApplication::installTranslator(&t);
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @thsprppr ok, what platform are you targeting?
                IIRC some platforms do not support loading translation files from resources, for what ever reasons.

                T Offline
                T Offline
                thsprppr
                wrote on last edited by
                #7

                @J-Hilk So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.

                It there something else I'm supposed to do in Linguist? I've been setting them to "complete" (ctrl+enter) and lrelease counts them as "finished", but is there something else I could have missed?

                @KroMignon said in Translations aren't working:

                @thsprppr said in Translations aren't working:

                QTranslator t;
                qDebug() << t.load(":/ts/en_US");
                qDebug() << QCoreApplication::installTranslator(&t);

                According to documenation:

                Usually, it is better to use the QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) function instead, because it uses QLocale::uiLanguages() and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.

                I think you should change the load to:

                QTranslator t;
                qDebug() << t.load(QLocale(), ":/ts/en_US");
                qDebug() << QCoreApplication::installTranslator(&t);
                

                I switched it to t.load(QLocale(), ":/ts/en_US"); and both qDebug()s came out false and nothing was translated.

                I also tried t->load(QLocale::system(), QString(), QString(), ":/ts"); as an experiment (my system locale is en_US), and they were both true again, but still didn't work.

                J.HilkJ 1 Reply Last reply
                0
                • T thsprppr

                  @J-Hilk So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.

                  It there something else I'm supposed to do in Linguist? I've been setting them to "complete" (ctrl+enter) and lrelease counts them as "finished", but is there something else I could have missed?

                  @KroMignon said in Translations aren't working:

                  @thsprppr said in Translations aren't working:

                  QTranslator t;
                  qDebug() << t.load(":/ts/en_US");
                  qDebug() << QCoreApplication::installTranslator(&t);

                  According to documenation:

                  Usually, it is better to use the QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) function instead, because it uses QLocale::uiLanguages() and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.

                  I think you should change the load to:

                  QTranslator t;
                  qDebug() << t.load(QLocale(), ":/ts/en_US");
                  qDebug() << QCoreApplication::installTranslator(&t);
                  

                  I switched it to t.load(QLocale(), ":/ts/en_US"); and both qDebug()s came out false and nothing was translated.

                  I also tried t->load(QLocale::system(), QString(), QString(), ":/ts"); as an experiment (my system locale is en_US), and they were both true again, but still didn't work.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @thsprppr said in Translations aren't working:

                  So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.

                  I don't know about Linux, but MacOS and iOS are 2 systems, where I explicitly pack those translation files into the app bundle and load them from there

                      APP_Language.files = $$PWD/translations/myApp_de.qm \
                                           $$PWD/translations/myApp_en.qm
                  
                      APP_Language.path = Contents/MacOS/translations
                      QMAKE_BUNDLE_DATA += APP_Language
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  T 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @thsprppr said in Translations aren't working:

                    So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.

                    I don't know about Linux, but MacOS and iOS are 2 systems, where I explicitly pack those translation files into the app bundle and load them from there

                        APP_Language.files = $$PWD/translations/myApp_de.qm \
                                             $$PWD/translations/myApp_en.qm
                    
                        APP_Language.path = Contents/MacOS/translations
                        QMAKE_BUNDLE_DATA += APP_Language
                    
                    T Offline
                    T Offline
                    thsprppr
                    wrote on last edited by
                    #9

                    I've tried loading QM files from the file system too with no luck unfortunately.

                    I did discover something interesting though. In a brand new Qt Widgets project on CMake, the following code works (in the MainWindow constructor before constructing any widgets):

                    QTranslator* t = new QTranslator(this);
                    qDebug() << t->load("/Users/thsprppr/translation.qm");
                    qDebug() << QCoreApplication::installTranslator(t);
                    

                    Yet that same exact code (with a different QM file) in the same place (MainWindow constructor) in my other project does not. There must be something in my code that's interfering with the translation.

                    e.sinohehE 1 Reply Last reply
                    1
                    • T thsprppr

                      I've tried loading QM files from the file system too with no luck unfortunately.

                      I did discover something interesting though. In a brand new Qt Widgets project on CMake, the following code works (in the MainWindow constructor before constructing any widgets):

                      QTranslator* t = new QTranslator(this);
                      qDebug() << t->load("/Users/thsprppr/translation.qm");
                      qDebug() << QCoreApplication::installTranslator(t);
                      

                      Yet that same exact code (with a different QM file) in the same place (MainWindow constructor) in my other project does not. There must be something in my code that's interfering with the translation.

                      e.sinohehE Offline
                      e.sinohehE Offline
                      e.sinoheh
                      wrote on last edited by
                      #10

                      @thsprppr That works for me as well, the translator should be a pointer!
                      For me

                      QTranslator translator;
                      

                      this works in qml but doesn't work in c++

                      But when I used

                       QTranslator *translator = new QTranslator(this);
                      

                      start working well.

                      E.Sinoheh

                      1 Reply Last reply
                      0
                      • J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        You too should learn about object life time in c++

                        will make your life much easier in the future :D


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1

                        • Login

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