PDA

View Full Version : Usage of QT_TR_NOOP()



kichi
4th May 2009, 15:35
Hi everybody,

I would like to know usage of QT_TR_NOOP() (not QT_TRANSLATE_NOOP()).

I use the Qt SDK 4.5.1 in Linux/X11 and work in Qt Creator 1.1.0.

I wrote a sample code as follows.


#include <QApplication>
#include <QMainWindow>
#include <QTranslator>
#include <QString>
#include <QtGlobal>

class MyMessage {
Q_DECLARE_TR_FUNCTIONS(MyMessage)
public:
MyMessage(){}
~MyMessage(){}

static QString GetMessage() {
static const char* message = QT_TR_NOOP("Hello");
return QString(QObject::tr(message));
}
};

int main(int argc, char *argv[])
{
QApplication application(argc, argv);
QMainWindow mainWindow;
QTranslator translator;

translator.load("./MyAppMessages.qm");
application.installTranslator(&translator);

mainWindow.setWindowTitle(MyMessage::GetMessage()) ;
mainWindow.show();

return application.exec();
}

And I made "MyAppMessages.qm" with lupdate, Qt Linguist, and lrelease to translate the message "Hello" into Japanese.
And I built and ran the program.
but window title was "Hello" yet.

Although I am studying English hard, my English is not great yet,
and I may make many mistakes. So, please be patient with me.

Thank you very much for any kind of help.
kichi

wysota
4th May 2009, 15:42
QT_TR_NOOP only marks the text as something that may need translation (so that it shows up in Linguist) but it doesn't do any translating. For that you need to use tr().

kichi
4th May 2009, 16:30
Thank you very much.
kichi

kichi
4th May 2009, 18:32
In my understanding, not only QT_TR_NOOP, but also QT_TRANSLATE_NOOP
merely marks the text for extraction.
so, I guess that "Hello" is displayed, in the following code.
but, "Hello" was translated into Japanese.
Why?


#include <QApplication>
#include <QMainWindow>
#include <QTranslator>
#include <QtGlobal>

static const char* greetingList[] = {
QT_TRANSLATE_NOOP("Greeting", "Good morning"),
QT_TRANSLATE_NOOP("Greeting", "Goodbye")
};

int main(int argc, char *argv[])
{
QApplication application(argc, argv);
QMainWindow mainWindow;
QTranslator translator;

translator.load("./MyAppMessages.qm");
application.installTranslator(&translator);

mainWindow.setWindowTitle(application.translate("Greeting", greetingList[0]));
mainWindow.show();

return application.exec();
}

wysota
4th May 2009, 18:37
Because you called QCoreApplication::translate().

kichi
4th May 2009, 19:14
I made a mistake.
I'm sorry.
I understand.
Thank you very much.