是否可以重新翻译 (tr) QString const?

Is it possible to retranslate (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 可以动态重新翻译吗?

空中交通管制

感谢您的回答和提示。

QWidget派生class中,如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 中,如果您想要 class,只需将结果存储在静态 QString 中此翻译的范围。

您需要保持未翻译的字符串常量。但是用 QT_TR_NOOP() 标记它以便 Linguist 可以看到它,并将其存储为普通的 char const*.

然后当你实际使用它时,像往常一样翻译它,用QApplication::translate,或者用你的class的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
}