Qt 国际化-多种语言方式

13 篇文章 0 订阅

1在工程中,新建资源文件,命名为translator

 

 

在工程目录中的资源文件结构

2修改pro
在pro文件中添加

TRANSLATIONS  += \
    tr_zh.ts  \
    tr_en.ts

3代码中所有需要翻译的字符串用tr("")包含 eg: tr(“中文内容”)

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton->setText(tr("button1"));
    ui->pushButton_2->setText(tr("button2"));

    ui->label->setText(tr("label"));
}

4qt点击更新翻译(lupdate)

此时会在工程目录新建生成了 tr_en.ts 和 tr_zh.ts 两个文件

5打开tr_zh.ts (翻译成中文)

方法一、双击tr_zh.ts文件,自动打开Qt Linguist软件,进行编辑,写入翻译内容,后点击对勾,保存即可

方法二、文本编辑tr_zh.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <source>MainWindow</source>
        <translatorcomment>主对话框AAA啊</translatorcomment>
        <translation>主对话框AAA</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <location filename="mainwindow.ui" line="40"/>
        <source>PushButton</source>
        <translatorcomment>按钮AAA</translatorcomment>
        <translation>按钮</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="53"/>
        <source>TextLabel</source>
        <translatorcomment>内容111啊</translatorcomment>
        <translation>内容111</translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="9"/>
        <source>button1</source>
        <translatorcomment>第一个按钮111</translatorcomment>
        <translation>第一个按钮</translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="10"/>
        <source>button2</source>
        <translatorcomment>第二个按钮222</translatorcomment>
        <translation>第二个按钮</translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="12"/>
        <source>label</source>
        <translatorcomment>第一个文本111</translatorcomment>
        <translation>第一个文本</translation>
    </message>
</context>
</TS>

 

6打开tr_en.ts (翻译成英文)

方法一、双击tr_en.ts文件,自动打开Qt Linguist软件,进行编辑,写入翻译内容,后点击对勾,保存即可

方法二、文本编辑

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <source>MainWindow</source>
        <translatorcomment>English main window-comments</translatorcomment>
        <translation>English main window</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <location filename="mainwindow.ui" line="40"/>
        <source>PushButton</source>
        <translatorcomment>button</translatorcomment>
        <translation>button</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="53"/>
        <source>TextLabel</source>
        <translation>textaaa</translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="9"/>
        <source>button1</source>
        <translatorcomment>first button1111</translatorcomment>
        <translation>first button</translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="10"/>
        <source>button2</source>
        <translatorcomment>second button222</translatorcomment>
        <translation>second button</translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="12"/>
        <source>label</source>
        <translatorcomment>textaaa1111</translatorcomment>
        <translation>textaaa</translation>
    </message>
</context>
</TS>

7如下图,点击发布翻译(lrelease)

会在项目中生成 tr_en.qm 和 tr_zh.qm 两个文件

8将qm文件添加到工程中

右键添加现有文件

选择文件

结果:

 

9使用语言

找到Path

代码

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTranslator translator;
    if(translator.load(":/tr_zh.qm")){
        qApp->installTranslator(&translator);
    }else{
        qDebug()<<"load  Chinese file error";
    }

    MainWindow w;
    w.show();

    return a.exec();
}

10运行结果

 

注:翻译文件加载的位置必须在界面实例化之前完成,否则是没有效果的

完整实例下载地址https://download.csdn.net/download/w09103419/15482481

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yongwuzhijing800

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值