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. Problem using QTranslator in simple example

Problem using QTranslator in simple example

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.3k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Boris1980
    wrote on last edited by
    #1

    Good morning guys,

    iam trying to use QTranslator with a created qm file from a custom ts file, but i just see the default language (source text).

    My ts file (with i convert to qm file via lrelease):

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE TS>
    <TS version="2.1" language="de_DE">
    <context>
        <name>QLabel</name>
        <message>
            <source>This is some text.</source>
            <translation>Dies ist ein Text.</translation>
        </message>
    </context>
    <context>
        <name>Page</name>
        <message>
            <source>Translations Example</source>
            <translation>Beispiel für Übersetzungen</translation>
        </message>
        <message>
            <source>This is some text.</source>
            <translation>Dies ist ein Text.</translation>
        </message>
    </context>
    <context>
        <name>installscript</name>
        <message>
            <source>This is a dynamically created page.</source>
            <translation>Diese Seite wurde dynamisch erzeugt.</translation>
        </message>
    </context>
    </TS>
    

    and this is my simple App, where i can (successfully) load the generated qm file. But in the QLabel text, is still the default language text, what iam doing wrong?

    QString str = QString("C:\\Users\\admin\\Documents\\visual studio 2015\\Projects\\QtGuiLanguage\\QtGuiLanguage\\LangFiles\\DE.xml.qm");
    	if(translator.load(str))
    		qDebug() << "successfully load qm file.";
    	else
    		qDebug() << "problem in load qm file.";
    
    	qApp->installTranslator(&translator);
    
    	QWidget *widget = new QWidget(this);
    	QGridLayout *layout = new QGridLayout(this);
    	widget->setLayout(layout);
    
    	layout->setRowStretch(0, 1);
    	layout->setRowStretch(1, 1);
    	layout->setRowStretch(2, 1);
    	setCentralWidget(widget);
    	
    	QPushButton *button = new QPushButton("Default Language", this);
    	QObject::connect(button, SIGNAL(clicked()), this, SLOT(Clicked()));
    
    	layout->addWidget(button,0,0,1,1);
    	layout->addWidget(new QLabel(tr("This is some text.","Page"), this),1,0,1,1);
    	layout->addWidget(new QLabel(tr("This is some text."), this), 2, 0, 1, 1);
    

    ia tryed the context with "QLabel type" and with "Page" Context, but it dosnt work..

    can anybody help me?;)

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Was the TS file generated by lupdate? It looks a bit different than normal.

      qApp->installTranslator(&translator);
      

      Does this call return true?

      (Z(:^

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Boris1980
        wrote on last edited by
        #3

        Hey, no i got the ts content form:

        https://doc.qt.io/qtinstallerframework/qt-installer-framework-translations-packages-com-vendor-product-meta-de-ts.html

        and i modified it for my case.

        I try to load the translator in main (regarding to this post https://stackoverflow.com/questions/20375297/translation-doesnt-work-when-executing-application)

        but it dosnt work at all

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Use lupdate instead of hand-crafting the TS files - it will properly mark all context data so that QTranslator can find the translations at runtime.

          (Z(:^

          1 Reply Last reply
          3
          • B Offline
            B Offline
            Boris1980
            wrote on last edited by Boris1980
            #5

            Ok thanks, this will be the second approach.. but the problem is, that i get a Excel based language table, an i habe to transform it... therefore it will be good, if i could write my own ts file...

            An i habe an Visual Studio solution , no Pro file.. so i can not use lupdate .. right? @Edit: sorry i da a "Create translation file" option in my qt VS addin.. but all of that.. it would be pretty to create the ts file manually:)

            @Edit2: Thank for your help now i can translate texts;) thank you my friend;)

            sierdzioS 1 Reply Last reply
            0
            • B Boris1980

              Ok thanks, this will be the second approach.. but the problem is, that i get a Excel based language table, an i habe to transform it... therefore it will be good, if i could write my own ts file...

              An i habe an Visual Studio solution , no Pro file.. so i can not use lupdate .. right? @Edit: sorry i da a "Create translation file" option in my qt VS addin.. but all of that.. it would be pretty to create the ts file manually:)

              @Edit2: Thank for your help now i can translate texts;) thank you my friend;)

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @Boris1980 said in Problem using QTranslator in simple example:

              Ok thanks, this will be the second approach.. but the problem is, that i get a Excel based language table, an i habe to transform it... therefore it will be good, if i could write my own ts file...

              An i habe an Visual Studio solution , no Pro file.. so i can not use lupdate .. right?

              Looks like you are deliberately trying to make life harder for yourself :-)

              I suggest starting small and going step by step:

              • create a .pro file
              • run lupdate

              Once you get that to work, analyse the .ts file - you should be able to find out how to hand-craft it later to fit your Excel thingy.

              I suspect the key ingredient missing in your TS and c++ is context which needs to match. But I have never tweaked TS files manually so I'm not 100% sure.

              (Z(:^

              1 Reply Last reply
              4

              • Login

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