Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. Translation not working for ui->label->setText(QObject::tr(ui->label->text().toStdString().c_str()));
Forum Updated to NodeBB v4.3 + New Features

Translation not working for ui->label->setText(QObject::tr(ui->label->text().toStdString().c_str()));

Scheduled Pinned Locked Moved Unsolved Chinese
6 Posts 3 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Biplab
    wrote on last edited by
    #1

    Hi All,

    Dynamic translation not working for below.
    Can you kindly help

    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);

    //Create nw object of QTranslator
    trans = new QTranslator;
    
    // Default language
    m_strLang = "English";
    ui->label->setText(QObject::tr(LABEL));
    
    
    connect(ui->butEnglish,SIGNAL(clicked(bool)),this, SLOT(setEnglish()));
    connect(ui->butChinese,SIGNAL(clicked(bool)),this, SLOT(setChinese()));
    

    }

    Widget::~Widget()
    {
    delete ui;
    }

    void Widget::setEnglish()
    {
    //remove old translation
    removeTranslators();

    //load english and install translator
    qDebug() <<"English Load:" <<  trans->load("eng") << endl;
    qDebug() << "English Install:" << qApp->installTranslator(trans) << endl;
    
    // I dont want to hardcode like:  ui->label->setText(QObject::tr(LABEL));
    // get the text and re-set again for new language
    QString str = ui->label->text();
    ui->label->setText(QObject::tr(str.toStdString().c_str()));
    m_strLang = "English";
    

    }

    void Widget::setChinese()
    {
    //remove old translation
    removeTranslators();

    //load chinese and install translator
    qDebug() <<"Load:" <<  trans->load("chi") << endl;
    qDebug() << "Install:" << qApp->installTranslator(trans) << endl;
    
    // I dont want to hardcode like:  ui->label->setText(QObject::tr(LABEL));
    // get the text and re-set again for new language
    QString str = ui->label->text();
    ui->label->setText(QObject::tr(ui->label->text().toStdString().c_str()));
    m_strLang = "Chinese";
    

    }

    void Widget::removeTranslators()
    {
    qDebug() << "remove:" << qApp->removeTranslator(trans) << endl;
    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      That's not how dynamic translation works.

      See the dedicated chapter in Qt's internationalisation documentation.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • jiancaiyangJ Offline
        jiancaiyangJ Offline
        jiancaiyang
        wrote on last edited by
        #3

        Hi administrator why do you throw this threadTo our section?Is it because it has word"chinese"?

        我们自己的论坛:http://qtdream.com
        擅长三维角色仿真动画。

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @jiancaiyang Please calm down.

          This thread hasn't been moved. It was already in this sub-forum. If it had been moved you would see a small arrow beside the title.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          jiancaiyangJ 1 Reply Last reply
          0
          • SGaistS SGaist

            @jiancaiyang Please calm down.

            This thread hasn't been moved. It was already in this sub-forum. If it had been moved you would see a small arrow beside the title.

            jiancaiyangJ Offline
            jiancaiyangJ Offline
            jiancaiyang
            wrote on last edited by
            #5

            @SGaist Alright, It is really a Chinese SPECIFIC question.
            And I can answer that "translation can be better occurs amongst Qt classes only, so do not mix std c++ style strings together."

            ui->label->setText(QObject::tr(ui->label->text().toStdString().c_str()));
            

            Could be replaced by:

            ui->label->setText(QObject::tr(ui->label->text()));
            

            我们自己的论坛:http://qtdream.com
            擅长三维角色仿真动画。

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              No, not at all. It's a translation specific question which in this case concerns both english and Chinese.

              However my answer still applies, overloading the changeEvent like described in the documentation is the correct thing to do:

              void MyWidget::changeEvent(QEvent *event)
              {
                  if (event->type() == QEvent::LanguageChange) {
                      titleLabel->setText(tr("Document Title"));
                      ...
                      okPushButton->setText(tr("&OK"));
                  } else
                      QWidget::changeEvent(event);
              }
              

              It's valid for all languages.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved