是否可以翻新(TR)QSTRING CONST



我有这个挑战。我正在尝试避免#define NEW_LABEL "---New label---"

我想通过define class constant以正确的方式进行。

myclass.h

class MyClass:public QDialog{ 
private:
Ui::MyClassWidget* ui;
const QString NEW_LABEL_TEXT;
}

myclass.cpp

//const init
MyClass::MyClass():ui(new Ui::MyClassWidget),NEW_LABEL_TEXT(tr("---New label---")){
some stuff..
}

我的问题是:

当我想翻译到其他语言时,这是正确的方法吗?QString const可以动态重新翻译吗?

atc

谢谢您的答案。

QWidget派生类中,例如QDialog,您可以收听翻译语言更改。

class MyDialog : public QDialog {
protected:
  virtual void changedEvent(QEvent * event) override {
         if (event->type() == QEvent::LanguageChange) {
             /* call tr again here */
             /* for ui object just call retranslateUi to automatically update translations */
             ui->retranslateUi(this);
          }
          QDialog::changedEvent(e);
   }
};

因此,您可以在每次更改语言更改时在运行时重新启动字符串,但是您不能将结果存储在const QString中,只需将结果存储在静态QString中,如果您想具有此翻译的类范围。

<</p> <</p>

您需要保持字符串恒定未翻译。但是用QT_TR_NOOP()标记它,以便语言学家可以看到它,并将其存储为普通的char const*

然后,当您实际使用它时,像往常一样将其翻译,使用QApplication::translate或类的tr()

// MyClass: Hello! -> Dobrý deň
static const uchar translations[] = {
  60, 184, 100,  24, 202, 239, 156, 149, 205,  33,  28, 191,  96, 161, 189, 221,
  66,   0,   0,   0,   8,   4, 236,  51,  17,   0,   0,   0,   0, 105,   0,   0,
   0,  52,   3,   0,   0,   0,  18,   0,  68,   0, 111,   0,  98,   0, 114,   0,
 253,   0,  32,   0, 100,   0, 101,   1,  72,   8,   0,   0,   0,   0,   6,   0,
   0,   0,   6,  72, 101, 108, 108, 111,  33,   7,   0,   0,   0,   7,  77, 121,
  67, 108,  97, 115, 115,   1,  47,   0,   0,   1,  58,   0, 151,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   7,  77, 121,
  67, 108,  97, 115, 115, 136,   0,   0,   0,   6,   1,   1, 255,   4,   2,   4,
};
#include <QCoreApplication>
#include <QDebug>
#include <QString>
#include <QTranslator>
int main(int argc, char **argv)
{
    static const char *const message = QT_TR_NOOP("Hello!");
    QCoreApplication app{argc, argv};
    QTranslator translator;
    if (!translator.load(translations, sizeof translations))
        qFatal("Failed to load translations");
    app.installTranslator(&translator);
    qInfo() << app.translate("MyClass", message);
    // or tr(message) in your own MyClass object
}

最新更新