Not able to get strings in Qt Linguist if outside of a class with the Q_OBJECT macro

Viewed 104

I have a file in my project with some functions not members of any class. I have some strings to be translated there, therefore, not being in any class derived from a Qt QObject or similar with the Q_OBJECT macro within the class declaration, I have used the following syntax:

#define CONTEXT_STRING "Context string here"
...
... QCoreApplication::translate(CONTEXT_STRING, "String to be translated") ...

instead of the usual:

... tr("String to be translated") ...

I am using Qt Creator 3.0.1 based on Qt 5.2.1 (MSVC 2010, 32 bit).

I have run lupdate but the strings still do not appear in Qt Linguist.

A complete minimal reproducible example is:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QString>
#include <QMessageBox>

#define CONTEXT_STRING "Context string here"

void standalonefun()
{
    QString msg;

    msg = QCoreApplication::translate(CONTEXT_STRING, "String to be translated");

    QMessageBox::information(0, "Message", msg);
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    standalonefun();
}

MainWindow::~MainWindow()
{   
    delete ui;
}
1 Answers

I finally found that I need to write:

... QCoreApplication::translate("Context string here", "String to be translated") ...

and cannot do:

#define CONTEXT_STRING "Context string here"
...
... QCoreApplication::translate(CONTEXT_STRING, "String to be translated") ...

because lupdate does not detect the second form with the #define preprocessor directive.

Related