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. Translate a single QString
Qt 6.11 is out! See what's new in the release blog

Translate a single QString

Scheduled Pinned Locked Moved Solved General and Desktop
8 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.
  • D Offline
    D Offline
    d.rein
    wrote on last edited by
    #1

    Hello,

    how can i translate a single QString in an Qt Console application. I just can find examples with gui. But it doesnt work....
    I am working on an embedded linux systeem and some error messages have to be translated.
    for example (just an primitive example)

    #include "translate.h"
    #include <QCoreApplication>
    #include <QDebug>
    #include <QTranslator>
    
    
    Translate::Translate(QObject *parent) : QObject(parent)
    {
        translate();
    }
    
    void Translate::translate()
    {
        QString message = tr("Hello");
    
        QTranslator translator;
        translator.load("ConsoleTranslationExample_de_DE");
        qApp->installTranslator(&translator);
    
        QTextStream o(stdout);
        o << message;
    }
    
    

    Can anybody please help me. Wasted yesterday the whole day to find a solution :-(

    eyllanescE 1 Reply Last reply
    0
    • D d.rein

      Hello,

      how can i translate a single QString in an Qt Console application. I just can find examples with gui. But it doesnt work....
      I am working on an embedded linux systeem and some error messages have to be translated.
      for example (just an primitive example)

      #include "translate.h"
      #include <QCoreApplication>
      #include <QDebug>
      #include <QTranslator>
      
      
      Translate::Translate(QObject *parent) : QObject(parent)
      {
          translate();
      }
      
      void Translate::translate()
      {
          QString message = tr("Hello");
      
          QTranslator translator;
          translator.load("ConsoleTranslationExample_de_DE");
          qApp->installTranslator(&translator);
      
          QTextStream o(stdout);
          o << message;
      }
      
      

      Can anybody please help me. Wasted yesterday the whole day to find a solution :-(

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @d-rein move QString message = tr("Hello"); after qApp->installTranslator(&translator);

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • D Offline
        D Offline
        d.rein
        wrote on last edited by
        #3

        No, still doent work...

        TranslationFile:

        <?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE TS>
        <TS version="2.1" language="de_DE">
        <context>
            <name>Translate</name>
            <message>
                <source>Hello</source>
                <translation>Hallo</translation>
            </message>
        </context>
        </TS>
        

        cpp:

        void Translate::translate()
        {
            QTranslator translator;
            translator.load("ConsoleTranslationExample_de_DE");
            qApp->installTranslator(&translator);
            
            QString message = tr("Hello");
            QTextStream o(stdout);
            o << message;
        }
        
        

        Output: Hello

        eyllanescE 1 Reply Last reply
        0
        • D d.rein

          No, still doent work...

          TranslationFile:

          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE TS>
          <TS version="2.1" language="de_DE">
          <context>
              <name>Translate</name>
              <message>
                  <source>Hello</source>
                  <translation>Hallo</translation>
              </message>
          </context>
          </TS>
          

          cpp:

          void Translate::translate()
          {
              QTranslator translator;
              translator.load("ConsoleTranslationExample_de_DE");
              qApp->installTranslator(&translator);
              
              QString message = tr("Hello");
              QTextStream o(stdout);
              o << message;
          }
          
          

          Output: Hello

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by eyllanesc
          #4

          @d-rein what is the output of qDebug() << translator.load("ConsoleTranslationExample_de_DE");?

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            d.rein
            wrote on last edited by
            #5

            @eyllanesc said in Translate a single QString:

            qDebug() << translator.load("ConsoleTranslationExample_de_DE");

            It is false. Mh creator cant fiond the translation file
            the ts file is located in the src folder of my project and the qm files are located inside the .qm dir of the build dir

            eyllanescE J.HilkJ 2 Replies Last reply
            0
            • D d.rein

              @eyllanesc said in Translate a single QString:

              qDebug() << translator.load("ConsoleTranslationExample_de_DE");

              It is false. Mh creator cant fiond the translation file
              the ts file is located in the src folder of my project and the qm files are located inside the .qm dir of the build dir

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by
              #6

              @d-rein Just for testing use the fullpath: translator.load("/path/of/ConsoleTranslationExample_de_DE.qm");

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              D 1 Reply Last reply
              1
              • D d.rein

                @eyllanesc said in Translate a single QString:

                qDebug() << translator.load("ConsoleTranslationExample_de_DE");

                It is false. Mh creator cant fiond the translation file
                the ts file is located in the src folder of my project and the qm files are located inside the .qm dir of the build dir

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

                @d-rein said in Translate a single QString:

                It is false. Mh creator cant fiond the translation file
                the ts file is located in the src folder of my project and the qm files are located inside the .qm dir of the build dir

                so the qm file is inside a folder, and not on the same lvl as your executable ?

                you'll have to specify that to the load command


                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
                • eyllanescE eyllanesc

                  @d-rein Just for testing use the fullpath: translator.load("/path/of/ConsoleTranslationExample_de_DE.qm");

                  D Offline
                  D Offline
                  d.rein
                  wrote on last edited by
                  #8

                  @eyllanesc Thank you. I got it. With the full path it works

                  void Translate::translate()
                  {
                      QTranslator translator;
                  
                      if(!translator.load(qApp->applicationDirPath() + "/ConsoleTranslationExample_de_DE.qm")){
                          qDebug() << "Cant find translation file";
                          return;
                      }
                      qApp->installTranslator(&translator);
                  
                      QString message = tr("Hello");
                      QTextStream o(stdout);
                      o << message;
                  
                  }
                  
                  
                  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