在Qt开发时,如何实现多语言翻译功能?

2021-01-29  本文已影响0人  Aliven888

文档声明:
以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正。并且该文档在后期会随着学习的深入不断补充完善。感谢各位的参考查看。


笔记资料仅供学习交流使用,转载请标明出处,谢谢配合。
如果存在相关知识点的遗漏,可以在评论区留言,看到后将在第一时间更新。
作者:Aliven888

如何配置开发环境,可以参考这边文章《VS2015 配置 Qt 开发编译环境》。

创建一个Qt 工程

  首先,我们创建一个Qt的项目工程。这里我选择的是Qt GUI Application,对话框继承 QMainWindows 类型(因为个人觉得菜单栏切换语言语言比较方便)。

【创建工程】 继承QMainWindow类

添加多语言翻译文件

  创建两个多语言文件 Aliven_zh.ts(中文) 和 Aliven_en.ts(英文)。


添加多语言翻译文件

编辑翻译文件

  编辑多语言文件,并且发布,生成 Aliven_xx.qm 文件。


编辑多语言文件
发布多语言文件

实现源码

QtGuiApplication.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication.h"
#include <QTranslator>
#include <QCoreApplication>
#include <QMenu>
#include <QAction>
#include <QDir>
#include <QString>
#include <QMap>

class QtGuiApplication : public QMainWindow
{
    Q_OBJECT

public:
    QtGuiApplication(QWidget *parent = Q_NULLPTR);
    ~QtGuiApplication();
    void init();
    void Uninit();
    
private:
    bool loadSysConfigFile();  //加载系统配置文件
    bool saveSysConfigFile();  //保存系统配置文件
    
private:
    //多语言翻译
    QTranslator *m_Translation;  //加载翻译文件对象
    QString m_strLanguageType;   //翻译文件的类型,zh:中文   en:英文

    //菜单
    QMenu *m_LanguageMenu;
    QAction *m_LanguageEnAction;  //英文
    QAction *m_LanguageZhAction;  //中文

private:
    Ui::QtGuiApplicationClass ui;
};

QtGuiApplication.cpp

#include "QtGuiApplication.h"

//多语言文本字符串对象
static const char *c_strQtGuiApplication = "QtGuiApplication";
static const char *c_strTitle = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.title"); 
static const char *c_strCaption = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.caption");
static const char *c_strLanguage = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.language");
static const char *c_strChinese = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.chinese");
static const char *c_strEnglish = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.english");


QtGuiApplication::QtGuiApplication(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    m_strLanguageType = "zh";   //默认值
}
QtGuiApplication::~QtGuiApplication()
{
    Uninit();
}

void QtGuiApplication::init()
{
    //加载配置文件
    if(!loadSysConfigFile())
    {
        qDebug() << "read config file err.";
    }

    //加载多语言文件
    m_Translation = new QTranslator(this);
    QString strPath = QDir::currentPath() + "/TranslationFile/Aliven_" + m_strLanguageType + ".qm";
    m_Translation->load(strPath);
    qApp->installTranslator(m_Translation);
    
    //初始化菜单 - 绑定信号与槽
    m_LanguageMenu = new QMenu(qApp->translate(c_strQtGuiApplication, c_strLanguage), this);
    m_LanguageEnAction = new QAction(qApp->translate(c_strQtGuiApplication, c_strEnglish), this);
    m_LanguageZhAction = new QAction(qApp->translate(c_strQtGuiApplication, c_strChinese), this);
    m_LanguageMenu->addAction(m_LanguageEnAction);
    m_LanguageMenu->addAction(m_LanguageZhAction);
    ui.menuBar->addMenu(m_LanguageMenu);

    //设置属性为可选中
    m_LanguageEnAction->setCheckable(true);
    m_LanguageZhAction->setCheckable(true);
    if ("zh" != m_strLanguageType) //设置当前选中状态
    {
        m_LanguageEnAction->setChecked(true);
    }
    else
    {
        m_LanguageZhAction->setChecked(true);
    }

    connect(m_LanguageZhAction, &QAction::triggered, this, [=] {
        m_strLanguageType = "zh";
        m_LanguageEnAction->setChecked(false);
    });
    connect(m_LanguageEnAction, &QAction::triggered, this, [=] {
        m_strLanguageType = "en";
        m_LanguageZhAction->setChecked(false);
    });

}

void QtGuiApplication::Uninit()
{
    //退出前,保存配置参数到文件中
    SaveSysConfigFile();

    //释放资源
    delete m_LanguageZhAction;
    m_LanguageZhAction = nullptr;

    delete m_LanguageEnAction;
    m_LanguageEnAction = nullptr;

    delete m_LanguageMenu;
    m_LanguageMenu = nullptr;

    delete m_Translation;
    m_Translation = nullptr;
}

//加载系统配置文件
bool QtGuiApplication::loadSysConfigFile()  
{
    QString strPath = QDir::currentPath() + "/systemConfig.Aliven";
    QMap<QString, QString> mapData;
    QString strLine = "";
    QString strKey = "";
    QString strValue = "";
    QStringList strList;

    QFile file(strPath);
    if (!file.open(QIODevice::ReadOnly))
    {
        return false;
    }
    QTextStream in(&file);
    while (!in.atEnd())
    {
        strLine = in.readLine();
        strList.clear();
        strList = strLine.split("=");
        mapData.insert(strList.value(0), strList.value(1));
    }

    file.close();

    //获取配置参数
    if (!mapData.isEmpty())
    {
        m_strLanguageType = mapData.value("languageType");
    }
    return true;
}

//保存系统配置文件
bool QtGuiApplication::saveSysConfigFile()  
{
    QString strPath = QDir::currentPath() + "/systemConfig.Aliven";
    QMap<QString, QString> mapData;
    mapData.insert("languageType", m_strLanguageType);

    QFile file(strPath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        return false;
    }

    QTextStream out(&file);
    QMapIterator<QString, QString> it(mapData);
    while (it.hasNext())
    {
        it.next();
        out << it.key() << "=" << it.value() << "\n";
    }
    file.close();
    return true;
}

多语言配置文件

systemConfig.Aliven

languageType=zh

效果图

  初次打开,默认的是中文


选中中文语言

  选择英文,关闭后软件后,再次打开,系统语言就成功切换成英文了。


选中英文语言
上一篇 下一篇

猜你喜欢

热点阅读