QT多语言实现详细讲解

前言

废话不多说,本文章详细完整介绍QT的多语言切换实现,以中英文为例。

创建项目

在这里插入图片描述
工程名称取名为:qt_multilang_demo,一路默认点击下一步,最终生成工程如下所示。
在这里插入图片描述

布局界面

简单搞下切换菜单,并添加一个Label,如图所示:
在这里插入图片描述

<?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>800</width>
    <height>600</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>350</x>
      <y>200</y>
      <width>191</width>
      <height>61</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Times New Roman</family>
      <pointsize>16</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Hello Multilanguage!</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
   <widget class="QMenu" name="menu">
    <property name="title">
     <string>切换语言</string>
    </property>
    <addaction name="action_CN"/>
    <addaction name="action_EN"/>
   </widget>
   <addaction name="menu"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <action name="action_CN">
   <property name="text">
    <string>中文</string>
   </property>
  </action>
  <action name="action_EN">
   <property name="text">
    <string>英文</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

生成语言文件

  1. 修改工程文件.pro,增加语言文件生成脚本,如下面所示:
TRANSLATIONS += \
    cn.ts \
    en.ts
  1. 生成语言文件.ts,操作如下:
    在这里插入图片描述
    底部概要信息窗口会输入相关信息:
    在这里插入图片描述
    重新加载工程,也可以看到生成的语言文件:
    在这里插入图片描述
  2. 翻译语言文件:
    从QT安装目录启动自带的翻译工具Linguist
    在这里插入图片描述
    打开之前生成的cn.ts语言文件,准备翻译成中文:
    在这里插入图片描述
    然后将每条英文信息翻译成中文:
    在这里插入图片描述
    翻译完成后,将每条信息标记为已经翻译,然后保存:
    在这里插入图片描述
    未翻译的cn.ts文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<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="33"/>
        <source>Hello Multilanguage!</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="48"/>
        <source>Switch Language</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="58"/>
        <source>Chinese</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="63"/>
        <source>English</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

已翻译的cn.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>
        <translation>主窗口</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="33"/>
        <source>Hello Multilanguage!</source>
        <translation>你好,多语言!</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="48"/>
        <source>Switch Language</source>
        <translation>切换语言</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="58"/>
        <source>Chinese</source>
        <translation>中文</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="63"/>
        <source>English</source>
        <translation>英文</translation>
    </message>
</context>
</TS>
  1. 发布语言文件,操作如下:
    在这里插入图片描述
    操作后会生成cn.qm、en.qm二进制语言文件,这是最终需要使用的文件:
    在这里插入图片描述

加载语言文件

  1. 添加菜单事件:
    在这里插入图片描述
    在菜单事件函数中添加以下代码,注意调用retranslateUi函数,否则不生效。另外,为方便理解这里就不封装复用代码了,大家想继续完善的话,可以把共同代码抽离出来封装独立函数。
void MainWindow::on_action_CN_triggered()
{
    QTranslator translator;
    translator.load("../qt_multilang_demo/cn.qm");
    qApp->installTranslator(&translator);
    ui->retranslateUi(this);
}

void MainWindow::on_action_EN_triggered()
{
    QTranslator translator;
    translator.load("../qt_multilang_demo/en.qm");
    qApp->installTranslator(&translator);
    ui->retranslateUi(this);
}

程序看效果

在这里插入图片描述

各位也可以直接下载示例:https://download.csdn.net/download/sdft06/87701464

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值