开发者

Qt localization: loading the localizable string

I want to load the localized string in a Qt application. For this, I am following a few steps. Correct me if I am wrong.

Note: it works fine for QString but not for const char*

  1. Update the pro file with translation language

  2. Generate .ts & edit using Qt linguist. Generate .qm file using lupdate and lrelease.

  3. Load the .qm file from a particular location.

Here is how my const char* looks:

const char* sayHello =  QT_TRANSLATE_NOOP("FriendlyConversation","hello");

LocalizationWithQT::LocalizationWithQT(QWidget *parent)
    : QMainWindow(parent)
{
 //ui.setupUi(this);
 QString str = tr("say hello");
 QPushButton *pushbutton = new QPushButton(tr(sayHello));
 setCentralWidget(pushbutton)

}

And here's how I am loading the .qm file:

QApplication a(argc, argv);

    QTranslator translator;
 开发者_开发问答   bool val = translator.load("c:\\Data\\test\\hellotr_la");
    a.installTranslator(&translator);

    LocalizationWithQT w;
    w.showMaximized();
    return a.exec();

The problem is, if I provide any alternate Latin string to "sayhello", it's not loading at all.

I have no idea where the mistake is.


If you use tr(sayHellow), Qt will look for the sayHellow translation in the current context (=LocalizationWithQT class).

You must give Qt the text context explicitly:

QPushButton *pushbutton 
    = new QPushButton(qApp->translate("FriendlyConversation", sayHellow));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜