Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem using QTranslator in simple example



  • 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?;)


  • Moderators

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

    qApp->installTranslator(&translator);
    

    Does this call return true?



  • 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


  • Moderators

    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.



  • 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;)


  • Moderators

    @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.


Log in to reply