美文网首页
Qt 国际语言切换

Qt 国际语言切换

作者: Caiaolun | 来源:发表于2020-02-18 16:15 被阅读0次

1.建立一个QWidget 工程


  1. 修改 .pro 文件
    添加了两句, 后面可生成两个 xxx.ts


  2. 编写代码, 对需要转换成国际文字的内容全用\color{red}{tr("xxx")}
QLabel* pTestLabel;
pTestLabel  = new QLabel;
pTestLabel->setText(tr("这里的显示的文字会转换"));

4.生成 xxx.ts文件



我的项目会生成两个 ***.ts

  1. ***.ts去转换成 ***.qm
    打开 linguist,
    这东西在/opt/Qt.x.x.x/Qt.x.x.x/gcc_64/bin里面(windos系统,直接在"开始"找qt就有了)



保存生成 ***.qm文件


6.加载 ***.qm

QTranslator pTranslate = new QTranslator(this);
pTranslate->load("执行文件,当前目录下的 ***.qm");
  1. 安装翻译
qApp->installTranslator(pTranslate);

步骤3.(重新执行一遍,Text的内容就会转到对应 ***.qm指定的文字)
pTestLabel->setText(tr("这里的显示的文字会转换"));

**如果你是有ui的.
直接ui->retranslateUi(this)一键把你的ui里面的文字重新设置一遍,这个操作你也可以自己写个函数封装

Demo:

main.cpp

#include "widget.h"
#include <QApplication>
#include "translatewidget.h"

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

    return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QTranslator>

#include "translatewidget.h"


class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
    ~Widget();
public:
    void CreateControl();
    void InitControl();
    void ConnectControl();
    void UpSetText();
public slots:
    void OnChangeLanguage();
    void OnTranslate();
private:
    QLabel* m_pLoginLabel;
    QLabel* m_pPasswordLabel;

    QLineEdit* m_pLoginLineEdit;
    QLineEdit* m_pPasswordLineEdit;

    QPushButton* m_pLoginButton;
    QPushButton* m_pCancelButton;
    QPushButton* m_pLanguageButton;

    QHBoxLayout* m_pLoginLabelHLayout;
    QHBoxLayout* m_pPasswordLabelHLayout;
    QHBoxLayout* m_pLoginButtonHLayout;
    QVBoxLayout* m_pMainVLayout;

    TranslateWidget* m_ptranslateWidget;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setGeometry(200,200,400, 200);
    CreateControl();
    InitControl();
    ConnectControl();
}

Widget::~Widget()
{

}

void Widget::CreateControl()
{
    m_ptranslateWidget = new TranslateWidget();

    m_pLoginLabel = new QLabel();
    m_pPasswordLabel = new QLabel();

    m_pLoginLineEdit = new QLineEdit();
    m_pPasswordLineEdit = new QLineEdit();

    m_pLoginButton = new QPushButton();
    m_pCancelButton = new QPushButton();
    m_pLanguageButton = new QPushButton();

    m_pLoginLabelHLayout = new QHBoxLayout();
    m_pPasswordLabelHLayout = new QHBoxLayout();
    m_pLoginButtonHLayout = new QHBoxLayout();
    m_pMainVLayout = new QVBoxLayout(this);
}
void Widget::InitControl()
{
    m_ptranslateWidget->setGeometry(0,0,300,80);
    m_ptranslateWidget->hide();

    m_pLoginLabel->setText(tr("用户: "));
    m_pPasswordLabel->setText(tr("密码: "));
    m_pLoginButton->setText(tr("登录"));
    m_pCancelButton->setText(tr("取消"));
    m_pLanguageButton->setText(tr("语言切换"));

    m_pLoginLabelHLayout->addWidget(m_pLoginLabel);
    m_pLoginLabelHLayout->addWidget(m_pLoginLineEdit);

    m_pPasswordLabelHLayout->addWidget(m_pPasswordLabel);
    m_pPasswordLabelHLayout->addWidget(m_pPasswordLineEdit);

    m_pLoginButtonHLayout->addStretch();
    m_pLoginButtonHLayout->addWidget(m_pLoginButton);
    m_pLoginButtonHLayout->addWidget(m_pCancelButton);

    m_pMainVLayout->addWidget(m_pLanguageButton);
    m_pMainVLayout->addLayout(m_pLoginLabelHLayout);
    m_pMainVLayout->addLayout(m_pPasswordLabelHLayout);
    m_pMainVLayout->addLayout(m_pLoginButtonHLayout);

    setLayout(m_pMainVLayout);
}
void Widget::ConnectControl()
{
    connect(m_pLanguageButton, SIGNAL(clicked()), this, SLOT(OnChangeLanguage()));
    connect(m_ptranslateWidget, SIGNAL(SignalTranslate()), this, SLOT(OnTranslate()));
}
void Widget::UpSetText()
{
    m_pLoginLabel->setText(tr("用户: "));
    m_pPasswordLabel->setText(tr("密码: "));
    m_pLoginButton->setText(tr("登录"));
    m_pCancelButton->setText(tr("取消"));
    m_pLanguageButton->setText(tr("语言切换"));
    qDebug("UpSetText");
}
void Widget::OnChangeLanguage()
{
    m_ptranslateWidget->show();
}
void Widget::OnTranslate()
{
    UpSetText();
}

tranlatewidget.h

#ifndef TRANSLATEWIDGET_H
#define TRANSLATEWIDGET_H

#include <QWidget>
#include <QApplication>
#include <QTranslator>
#include <QRadioButton>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QLabel>

class TranslateWidget : public QWidget
{
    Q_OBJECT
public:
    explicit TranslateWidget(QWidget *parent = nullptr);

public:
    void CreateControl();
    void InitControl();
    void ConnectControl();
    void UpSetText();
signals:
    void SignalTranslate();
public slots:
    void OnTranslate(int p_pID);
private:
    QTranslator* m_pTranslate;

    QLabel* m_pLanguageLabel;

    QRadioButton* m_pEnglishButton;
    QRadioButton* m_pChineseBUtton;
    QButtonGroup* m_pLanguageButtonGroup;

    QHBoxLayout* m_pLanguageLabelHLayout;
};

#endif // TRANSLATEWIDGET_H

translatewidget.cpp

#include "translatewidget.h"

TranslateWidget::TranslateWidget(QWidget *parent) : QWidget(parent)
{
//    setGeometry(0,0,400, 80);
    CreateControl();
    InitControl();
    ConnectControl();
}
void TranslateWidget::CreateControl()
{
    m_pTranslate = new QTranslator(this);

    m_pLanguageLabel = new QLabel();

    m_pEnglishButton = new QRadioButton();
    m_pChineseBUtton = new QRadioButton();
    m_pLanguageButtonGroup = new QButtonGroup();

    m_pLanguageLabelHLayout = new QHBoxLayout(this);
}
void TranslateWidget::InitControl()
{
    m_pLanguageLabel->setText(tr("语言: "));
    m_pEnglishButton->setText("Englisg");
    m_pChineseBUtton->setText("中文");
    m_pChineseBUtton->setChecked(true);

    m_pLanguageButtonGroup->addButton(m_pChineseBUtton, 0);
    m_pLanguageButtonGroup->addButton(m_pEnglishButton, 1);

    m_pLanguageLabelHLayout->addStretch();
    m_pLanguageLabelHLayout->addWidget(m_pLanguageLabel);
    m_pLanguageLabelHLayout->addWidget(m_pChineseBUtton);
    m_pLanguageLabelHLayout->addWidget(m_pEnglishButton);

    hide();
    setLayout(m_pLanguageLabelHLayout);
}
void TranslateWidget::ConnectControl()
{
    connect(m_pLanguageButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(OnTranslate(int)));
}
void TranslateWidget::UpSetText()
{
    m_pLanguageLabel->setText(tr("语言: "));
    m_pEnglishButton->setText("Englisg");
    m_pChineseBUtton->setText("中文");
}

void TranslateWidget::OnTranslate(int p_pID)
{
    switch (p_pID) {
    case 0:
        m_pTranslate->load("Translate_CN.qm");
        break;
    case 1:
        m_pTranslate->load("Translate_EN.qm");
        break;
    default:
        break;
    }
    qApp->installTranslator(m_pTranslate);
    SignalTranslate();
    UpSetText();
}

相关文章

  • Qt 国际语言切换

    1.建立一个QWidget 工程 修改 .pro 文件添加了两句, 后面可生成两个 xxx.ts 编写代码, 对需...

  • iOS 常用的知识点(持续更新)

    1.模拟器语言切换 我们用到国际化的时候,需要调试不同语言的版本,所以切换语言的话模拟器来回切换很麻烦。简单方法:...

  • iOS-常用的知识点(持续更新)

    1.模拟器语言切换 我们用到国际化的时候,需要调试不同语言的版本,所以切换语言的话模拟器来回切换很麻烦。简单方法:...

  • SpringBoot系列—国际化(八)

    个人博客:haichenyi.com。感谢关注   所谓的国际化就是语言切换。中文切换到其他语种。   国际化的信...

  • iOS 实现国际语言本地化功能

    实现国际语言本地化切换功能,1、app名称跟随系统语言变化,2、应用程序内部可自行切换语言。 步骤: 2、新建st...

  • iOS 国际化多语言(下)

    前言 上一片文章介绍的是根据系统语言自动切换,现在我们来说说怎么在APP中手动切换多语言。iOS 国际化多语言(上...

  • iOS 国际化(App内语言切换)

    想要实现App内的语言切换功能,那么首先要创建国际化语言文件,用于存储对应语言显示的文本。1、创建国际化语言文件c...

  • iOS国际化简单操作

    iOS国际化简单操作 国际化后的App可以根据手机当前系统语言来切换不同的语言来适应不同的语言使用人群, 所以iO...

  • Android国际化(多语言)实现,支持8.0

    前言 最近因为项目中使用了国际化,所以正好研究了下实现方法;首先说下项目需求: 可以随着系统切换语言而切换语言,不...

  • iOS国际化支持

    iOS国际化支持 一、背景 最近一个项目中需要使用到不同国家语言的切换,包括UI控件的语言切换,最好是能够实现动态...

网友评论

      本文标题:Qt 国际语言切换

      本文链接:https://www.haomeiwen.com/subject/ievgfhtx.html