【问题标题】:Qt Linguist - set translator for application Qt *.ui filesQt Linguist - 为应用程序 Qt *.ui 文件设置翻译器
【发布时间】:2013-07-22 16:31:47
【问题描述】:

在菜单中选择语言后,我编写了一个小而简单的示例来更改应用程序语言。尽管连接确实有效(qDebug() 打印出好的消息),但它不会更改我的 QLabel 上的文本。我使用 QtDesigner 创建了 GUI。 注意:所有这些文件都在同一个目录中。我使用的是 Qt5。这是我的代码:

*.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qt_pl_en
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

TRANSLATIONS += ic.ts
TRANSLATIONS += se.ts

ma​​inwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTranslator>
#include <QDebug>

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

    retranslate();

    QObject::connect(ui->action_Icelandic, SIGNAL(triggered()), this, SLOT(speakIcelandic()));
    QObject::connect(ui->action_Swedish, SIGNAL(triggered()), this, SLOT(speakSwedish()));
}

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


void MainWindow::changeEvent(QEvent* event)
{
    if (event->type() == QEvent::LanguageChange)
    {
        // retranslate designer form
        ui->retranslateUi(this);

        // retranslate other widgets which weren't added in designer
        retranslate();
    }

    // remember to call base class implementation
    QWidget::changeEvent(event);
}

void MainWindow::retranslate()
{
    ui->label->setText(QObject::tr("Hello, world! :-)"));
}

void MainWindow::speakSwedish()
{
    QTranslator translator;
    translator.load("se.qm");
    qApp->installTranslator(&translator);

    ui->retranslateUi(this);

    qDebug() << "Swedish";
}

void MainWindow::speakIcelandic()
{
    QTranslator translator;
    translator.load("ic.qm");
    qApp->installTranslator(&translator);

    ui->retranslateUi(this);

    qDebug() << "Icelandic";
}

ma​​inwindow.h

    #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;

    void retranslate();
    void changeEvent(QEvent* event);


private slots:

    void speakSwedish();
    void speakIcelandic();
};

#endif // MAINWINDOW_H

ma​​in.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

ic.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_EN">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <source>MainWindow</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <location filename="mainwindow.cpp" line="13"/>
        <source>Hello, world! :-)</source>
        <translation type="unfinished">Halló, heimur! :-)</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="42"/>
        <source>File</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="60"/>
        <source>&amp;Swedish</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="65"/>
        <source>&amp;Icelandic</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

se.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_EN">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <source>MainWindow</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <location filename="mainwindow.cpp" line="13"/>
        <source>Hello, world! :-)</source>
        <translation type="unfinished">Hallå, världen! :-)</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="42"/>
        <source>File</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="60"/>
        <source>&amp;Swedish</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="65"/>
        <source>&amp;Icelandic</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

ma​​inwindow.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>120</x>
      <y>80</y>
      <width>181</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Hello, world! :-)</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>25</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="action_Swedish"/>
    <addaction name="action_Icelandic"/>
   </widget>
   <addaction name="menuFile"/>
  </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"/>
  <action name="action_Swedish">
   <property name="text">
    <string>&amp;Swedish</string>
   </property>
  </action>
  <action name="action_Icelandic">
   <property name="text">
    <string>&amp;Icelandic</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

    标签: qt translation linguistics


    【解决方案1】:

    这对我有用。(从英语翻译成葡萄牙语)

    1) 将此添加到 main.cpp

     QApplication app(argc, argv);
    
    //Language Translation
    //change the prefered language 
    QString locale = "pt" // say i am doing Portuguese language translation
    QString file= app.applicationDirPath() +"/translation_"+locale;
    QTranslator translator;
    bool result=translator.load(file);
    if ((locale != "en")  && !result )
    {
        QMessageBox msgBox;
        msgBox.setText("Unable to load language translation files");
        msgBox.exec();  
    }
    app.installTranslator(&translator);
    

    2) 说是否要翻译 MainWindow.ui

    使用 QT 设计器单击 MainWindow.ui 中每个标签中的可翻译复选框。 (除此之外,您在 gui 中无事可做)

    3) 创建translation_pt.ts 文件,如下所述。确保将类名放在名称标签内

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE TS><TS>
    	<context>
    		<name>MainWindow</name>
    		<!-- Dialog Heading -->
    		<message>
    			<source>Glasses Fitment Details</source>
    			<translation>Detalhes de Calibração dos Óculos</translation>
    		</message>
    		<!-- Fitter -->
    		<message>
    			<source>Fitter</source>
    			<translation>Ajustador</translation>
    		</message>
    		<message>
    			<source>Fitter's Name</source>
    			<translation>Nome do Ajustador</translation>
    		</message>
       </context>
    </TS>

    4) 将以下行添加到您的 .pro 文件中

    翻译 = 翻译_pt.ts

    5) 转到qt安装位置并运行以下命令

    lrelease "您在步骤 4 中编辑的 .pro 文件的位置"

    然后您将在该位置获得 translation_pt.qm 文件

    6) 将 translation_pt.qm 文件复制到你的二进制文件正在运行的位置

    然后您在 MainWindow.ui 中的语言会自动从英语翻译成葡萄牙语

    谢谢,

    萨满

    【讨论】:

    • 我是新手,因此我正在查看 qt creator,它没有在 qt 设计器中看到的所有其他 UI 元素的属性属性。对于看到此内容的其他人,您必须展开文本属性的下拉箭头才能看到 translatable 复选框。
    【解决方案2】:

    好吧,伙计们,解决方案比您想象的更简单。添加:

    QTranslator *translator;
    

    进入ma​​inwindow.h

    然后,在speakIcelandic()speakSwedish()(在ma​​inwindow.cpp 中)只需将实现代码更改为:

    bool ok = translator->load("se.qm");
    qDebug("translation %d", ok);
    qApp->installTranslator(translator);
    
    ui->retranslateUi(this);
    
    qDebug() << "Swedish";
    

    别忘了在构造函数中添加translator = new QTranslator();,它终于可以工作了!

    【讨论】:

    • 您好,请问您在哪里定义qApp?在 main.cpp 中?比我们在 mainwindow.cpp 中使用它时它应该是全局的 ...??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多