【问题标题】:Is it possible to retranslate (tr) QString const?是否可以重新翻译(tr)QString const?
【发布时间】:2018-04-04 08:00:16
【问题描述】:

我有这个挑战。我试图避免#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 可以动态重新翻译吗?

空中交通管制

感谢您的回答和提示。

【问题讨论】:

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


    【解决方案1】:

    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 中。

    【讨论】:

    • 谢谢。所以我应该使用static QString NEW_LABEL_TEXT 而不是QString const
    • 制作一个 const QString 会让你在初始化后无法修改它。将其设为静态是可能的,但是当语言更改时,将在您拥有的每个 Dialog 实例上调用 changedEvent。
    • 我认为,它解决了这个问题。非常感谢。
    【解决方案2】:

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

    【讨论】:

    • 谢谢。我要试试。看起来不错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2021-08-24
    相关资源
    最近更新 更多