[Qt-interest] Maybe a bug using Q_DECLARE_TR_FUNCTIONS

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Thu Mar 25 14:14:48 CET 2010


Berry Octave wrote on Wednesday, March 24, 2010 7:38 PM:

> Hi the list,
> 
> Working on a project involving a lot of non-Qt classes, I tried to
> use Q_DECLARE_TR_FUNCTIONS in one of my base classes. 
> 
> This macro is correctly handled by the compiler (ie : using tr in
> classes is fine). 
> Same about lupdate which correctly extracts the sentences.
> 
> Once translation done, I loaded them as shown in the documentation.
> 
> The fact was that QObject classes (or ones using QObject::tr
> explicitely) were correctly translated, wherhas the ones using
> Q_DECLARE_TR_FUNCTIONS'tr were shown in untranslated version.  

Make sure that you set the context right in the Q_DECLARE_TR_FUNCTIONS, that when you re-run lupdate that your *.ts is up to date with the proper context and that the proper generated *.qm file is picked up!

So this works for me on Win XP, Using Qt 4.6.1, VS 2005 Express:

// MyI18N.h
#include <QtCore/QCoreApplication>

class MyI18N {
    Q_DECLARE_TR_FUNCTIONS(MyI18N)
	
public:
    MyI18N();
    virtual ~MyI18N();
	
    void sayHello();
};

// MyI18N.cpp
#include <QtCore/QObject>

#include "MyI18N.h"

MyI18N::MyI18N() {}
MyI18N::~MyI18N() {}

void MyI18N::sayHello() {
    qDebug(qPrintable(tr("Hello. It is a beautiful day!")));
}

// main.cpp
#include <QtCore/QTranslator>
#include <QtCore/QCoreApplication>

#include "MyI18N.h"

int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    MyI18N myI18N;
	
    QTranslator translator;
    // make sure the *.qm file is in the same directory as the executable
    translator.load("MyI18N_DE_de.qm");
    QCoreApplication::installTranslator(&translator);
  
    myI18N.sayHello();
    return 0;
}

// i18n.pro
TEMPLATE = app
CONFIG += console
CONFIG -= gui
TARGET = 
DEPENDPATH += . src
INCLUDEPATH += .

TRANSLATIONS += MyI18n_DE_de.ts

# Input
HEADERS += src/MyI18N.h
SOURCES += src/main.cpp src/MyI18N.cpp


Note that I did the following observation: testwise I added an additional

void MyI18N::sayHello() {
    qDebug(qPrintable(tr("Hello. It is a beautiful day!")));
    qDebug(qPrintable(QObject::tr("Hello. It is a beautiful day!"))); // NEW
}

WITHOUT re-running lupdate. Simply starting the app again would output:

Hallo. Es ist ein wundervoller Tag!
Hello. It is a beautiful day!

So it is the opposite from what you have observed (the text from QObject::tr() remains untranslated)! So make sure the context, as in

  Q_DECLARE_TR_FUNCTIONS(MyI18N)

"MyI18N" really matches the class name and that the *.qm is up-to-date!

Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list