Frage

I am using Qt linguist to translate a ui file. I obtained its ts file using lupdate, and translated those words and phrases. Now I want to add it to my code, but I found from its tutorial that seems like I have to add tr() to each and every single word and phrase in my code. Did I understand it correctly? Can I just load the translated qm file once to do the translation? If so, how can I do it in this ui file?

Thanks in advance.

Update

After changing the properties as Jens mentioned, in my ui header file I can see sentences like:

buttonSetupData->setText(QApplication::translate("MainWindow", "Set Data", 0, QApplication::UnicodeUTF8));

After Ajith's answer, I've changed my code, and now my retranslateUi function in ui_main.h file looks like this:

void retranslateUi ( QMainWindow *MainWindow )
{
    QTranslator translator;
    translator.load ( " ...\Test.ts " );
    QApplication::installTranslator ( &translator );

    MainWindow->setWindowTitle ( QApplication::translate ("MainWindow",  "The Sentioscope - Beta v2.0 for Soccer", 0, QApplication::UnicodeUTF8));

    ...
}

But it still didn't work.

latest update

My current function for reading ".qm" file is like this:

void MainWindow::translate()
{
     QTranslator translator;
     translator.load ( "Test.qm", "D:\\" );
     qApp->installTranslator ( &translator );
     retranslateUi ( this );
 }

Then I call this function like this:

MainWindow::MainWindow ( QWidget *parent ) : QMainWindow ( parent )
{
    setupUi(this);
    translate();
}

After Ajith's latest answer, I changed each string for warning/message boxes in this form (for example):

QMessageBox::warning( this, QMessageBox::tr("ERROR"), QMessageBox::tr("Invalid IP adress") );

By lupdate I can read all these strings, translate, and save them in a qm file. The problem now is, it can translate those strings for ui, but not for warning/message boxes after execution.

War es hilfreich?

Lösung

I am not entirely sure about what you want to accomplish from the question. I think you are looking for a way to load the "qm" files at runtime from C++ code. If so, what you will need to do is use the QTranslator class..

QTranslator translator;
translator.load("file.qm", "dir");
qApp->installTranslator(&translator);

Update

This was working for me..

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    isChinese(false)
{
    ui->setupUi(this);
    msgbox = new QMessageBox(QMessageBox::NoIcon, "", "", QMessageBox::NoButton, this);
    pbtn = msgbox->addButton("Ok", QMessageBox::AcceptRole);

    doRetranslate();

    QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(doTranslate()));
    QObject::connect(ui->pushButton, SIGNAL(clicked()), msgbox, SLOT(show()));
}

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

void MainWindow::doTranslate()
{
    QTranslator translator;
    if (!isChinese) {
        translator.load("test_zh_TW.qm", "../TestTranslator");
        qApp->installTranslator(&translator);
        isChinese = true;
    } else {
        translator.load("", ""); // Default is english
        qApp->installTranslator(&translator);
        isChinese = false;
    }
    ui->retranslateUi(this);
    doRetranslate();
}

void MainWindow::doRetranslate()
{
    msgbox->setWindowTitle(QObject::tr("ERROR"));
    msgbox->setText(QObject::tr("Invalid IP adress"));
    pbtn->setText(QObject::tr("Ok"));
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QPushButton>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void doRetranslate();
public slots:
    void doTranslate();

private:
    Ui::MainWindow *ui;
    bool isChinese;
    QMessageBox *msgbox;
    QPushButton *pbtn;
};

#endif // MAINWINDOW_H

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>70</y>
      <width>181</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Hello World!</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>120</y>
      <width>93</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>translate</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections>
  <connection>
   <sender>pushButton</sender>
   <signal>clicked()</signal>
   <receiver>label</receiver>
   <slot>update()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>199</x>
     <y>165</y>
    </hint>
    <hint type="destinationlabel">
     <x>206</x>
     <y>114</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

Andere Tipps

Pretty much correct what you describe. The QtDesigner/QtCreator allows for each ui-Element to select whether its texts should be translated or not (see the properties of each ui-Element). If translation is selected, then the tr() is put into the generated ui_xxx.h file and from there it is presented like any other translatable word.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top