是否可以重新转换(tr)QString const?

时间:2018-04-04 08:00:16

标签: c++ qt const qstring language-translation

我有这个挑战。我试图避免#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

感谢您的回答和提示。

2 个答案:

答案 0 :(得分:2)

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>

答案 1 :(得分:0)

您需要保持字符串常量不翻译。但请使用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
}