Qt深入浅出(十六)多国语言国际化

多国语言国际化

​ Qt自己提供了一个种国际化方案, 生成字典文件的方法来翻译Qt应用中的tr()、translate()字符串,字典文件以”.qm”命名。

1 生成一个qm文件

  1. 新建一个GUI工程”TestHello.pro”,在UI界面上添加两个按钮,并分别将文本修改为“hello”, “china”.

  2. 修改”TestHello.pro”文件,添加如下代码:

TRANSLATIONS += TestHello.ts
  1. 编译

  2. 编译完成后,选择开始(或者win键)->  所有应用 -> Qt5.7.0-> Qt5.7.0 for Desktop (MinGW 5.3.0 32 bit)菜单项,打开DOS命令行窗口,进入`TestHello工程目录, 执行命令:


lupdate  TestHello.pro
  1. 选择开始(或者win键)->  所有应用 -> Qt5.7.0-> Linguist 菜单项

  1. 在主界面中选择文件-> 打开, 选择TestHello.ts文件, 单击打开按钮,根据需要设置源语言和目标语言

  1. 选择要翻译的字符串,输入对应的翻译文字,单击上面Mark as done item按钮

  1. 选择文件-> 发布菜单项. 或者在命令行输入lrelease TestHello.pro, 在工程目录里面就生成了一个TestHello.qm文件.

  1. 修改源代码如下:

    main.cpp


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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator * translator =  new QTranslator;        \\生成一个字典对象
    translator->load("D:\\workspace\\TestHello.qm");    \\加载一个字典文件
    a.installTranslator(translator);                    \\安装字典文件
    MainWindow w;
    w.show();
    return a.exec();
}

2 时实切换qm文件

​ 事先我们编好三本字典分别是lang_zh.qm中文字典、lang_en.qm英文字典、lang_yy.qm粤语字典。

​ Widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>
#include <QComboBox>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
    void reflashLabel();
public slots:
    void changeLangSlot(int);
private:
    QLabel* _label;
    QComboBox * _combobox;
};

#endif // WIDGET_H

​ Widget.cpp


#include "widget.h"
#include <QLabel>
#include <QTranslator>
#include <QComboBox>
#include <QVBoxLayout>
#include <QApplication>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout * vBox = new QVBoxLayout;
    _combobox = new QComboBox; //用到一个下拉选择控件
    _label = new QLabel;
    this->setLayout(vBox);
    vBox->addWidget(_label, 5);
    vBox->addWidget(_combobox, 1);
    _combobox->addItem("English", "en");
    _combobox->addItem("Chinese", "zh");
    _combobox->addItem("YueYu", "yy");

    /*下拉选择控件被用户选择某一项后,会发射出一个带某一项索引的信号*/
    connect(_combobox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLangSlot(int)));
    reflashLabel();
}
void Widget::reflashLabel()
{
    _label->setText(tr("LABEL_HELLO", "hello"));
}

void Widget::changeLangSlot(int index)
{
    QString name = _combobox->itemData(index).toString();
    static QTranslator* tran = NULL;

    if(!tran)
    {
        QApplication::instance()->removeTranslator(tran); //移除已经安装的字典文件
        delete tran;
        tran = NULL;
    }
    /*重新安装字典*/
    tran = new QTranslator;
    tran->load("D:/workspace/untitled11/lang_" + name + ".qm");
    QApplication::instance()->installTranslator(tran);
    reflashLabel();
}

Widget::~Widget()
{

}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值