QT-自定义本地翻译模块,简单实用
- 前言
- 一、演示效果
- 二、核心模块
- 1.本地xml的操作过程
- 2、翻译模块
- 三、程序链接
前言
QT的国际语言每次如果有字符串变动,都需要重新再编译一下,真的是顶不住,说实话,用起来真的是不习惯。
为了解决这个问题,我们设计基本的逻辑:
1、将控件名称保存到本地xml,也就是先初始化一个需要我们要翻译的文本。
2、在xml填入翻译的内容。
3、软件显示我们翻译的内容,这里就是有技巧,我们希望是自动去翻译这些控件,而不是程序四处都看到我们翻译的过程。
一、演示效果
1、本地xml格式内容如下,启动source的属性是我们程序自动生成的,我们当然是希望没翻译之前,这些需要我们翻译的字段先填好。
2、界面显示效果如下:
二、核心模块
1.本地xml的操作过程
代码如下:
#include "LanguageXml.h"
#include <QMutex>
#include <QFile>
LanguageXml::LanguageXml(QObject *parent)
: QObject(parent)
{
}
LanguageXml::~LanguageXml()
{
}
//插入链表
void LanguageXml::insertItem(QString strSource, QString strTranslate)
{
m_itemsHash.insert(strSource,strTranslate);
}
//读取本地链表
bool LanguageXml::readItems(QString strFilePath)
{
if (strFilePath.isEmpty())
return false;
QMutex mutex;
mutex.lock();
QFile file(strFilePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
mutex.unlock();
return false;
}
QXmlStreamReader reader(&file);
while (!reader.atEnd())
{
reader.readNext();
QString strElementName = reader.name().toString();
if (reader.isStartElement() && strElementName == QString("Item"))
{
QString strSource;
QString strTranslation;
auto attr = reader.attributes();
if (attr.hasAttribute("Source"))
strSource = attr.value("Source").toString();
if (attr.hasAttribute("Translation"))
strTranslation = attr.value("Translation").toString();
m_itemsHash[strSource] = strTranslation;
}
}
file.close();
mutex.unlock();
return true;
}
//保存本地
bool LanguageXml::saveItems(QString strFilePath)
{
if (strFilePath.isEmpty())
return false;
static QMutex mutex;
mutex.lock();
QFile file(strFilePath);
file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
QXmlStreamWriter writer(&file);
writer.setAutoFormatting(true);
writer.writeStartDocument();
if (true)
{
writer.writeStartElement("LanguageItems");
auto keys = m_itemsHash.keys();
for (int j = 0; j < keys.size(); j++)
{
if (true)
{
writer.writeStartElement("Item");
writer.writeAttribute("Source", keys[j]);
writer.writeAttribute("Translation", m_itemsHash[keys[j]]);
writer.writeEndElement();
}
}
writer.writeEndElement();
}
writer.writeEndDocument();
file.close();
mutex.unlock();
return true;
}
//返回链表
QHash<QString, QString> LanguageXml::getItemsHash()
{
return m_itemsHash;
}
2、翻译模块
代码如下:
#include "ControlsTranslate.h"
#include "LanguageXml.h"
#include <QPushButton>
#include <QToolButton>
#include <QGroupBox>
#include <QCheckBox>
#include <QRadioButton>
#include <QLineEdit>
#include <QDateTimeEdit>
#include <QLocale>
#include <QTableWidget>
#include <QTreeWidget>
#include <QLabel>
#include <QComboBox>
#include <QApplication>
#include <QFile>
#include <QMainWindow>
ControlsTranslate::ControlsTranslate(QObject *parent)
: QObject(parent)
{
m_pLanguageXml = new LanguageXml(parent);
m_objectList = getAllObject(parent);
initialTranslateFile();
}
ControlsTranslate::~ControlsTranslate()
{
}
//获取所有控件
QObjectList ControlsTranslate::getAllObject(QObject * parent)
{
QObjectList childrenList, tempList;
if (parent)
childrenList = parent->children();
if (childrenList.isEmpty())
return childrenList;
tempList = childrenList;
foreach(QObject* obj, tempList)
{
QObjectList lst = getAllObject(obj);
if (!lst.isEmpty())
childrenList.append(lst);
}
return childrenList;
}
//翻译控件
void ControlsTranslate::translateAllControls(const QString &strLanguage)
{
if (!m_objectList.isEmpty())
{
foreach(QObject *obj, m_objectList)
{
auto pPushButton = qobject_cast<QPushButton *>(obj);
if (pPushButton != nullptr)
pPushButton->setText(translate(pPushButton->text()));
auto pToolButton = qobject_cast<QToolButton *>(obj);
if (pToolButton != nullptr)
pToolButton->setText(translate(pToolButton->text()));
auto pWidget = qobject_cast<QWidget *>(obj);
if (pWidget != nullptr)
pWidget->setWindowTitle(translate(pWidget->windowTitle()));
auto pGroupBox = qobject_cast<QGroupBox *>(obj);
if (pGroupBox != nullptr)
pGroupBox->setTitle(translate(pGroupBox->title()));
auto pLabel = qobject_cast<QLabel *>(obj);
if (pLabel != nullptr)
pLabel->setText(translate(pLabel->text()));
auto pCheckBox = qobject_cast<QCheckBox *>(obj);
if (pCheckBox != nullptr)
pCheckBox->setText(translate(pCheckBox->text()));
auto pRadioButton = qobject_cast<QRadioButton *>(obj);
if (pRadioButton != nullptr)
pRadioButton->setText(translate(pRadioButton->text()));
auto pLineEdit = qobject_cast<QLineEdit *>(obj);
if (pLineEdit != nullptr)
{
pLineEdit->setText(translate(pLineEdit->text()));
pLineEdit->setPlaceholderText(translate(pLineEdit->placeholderText()));
}
auto pDateTimeEdit = qobject_cast<QDateTimeEdit *>(obj);
if (pDateTimeEdit != nullptr)
{
if (strLanguage.compare("ChineseT") == 0)
pDateTimeEdit->setLocale(QLocale(QLocale::Chinese, QLocale::Taiwan));
else if (strLanguage.compare("English") == 0)
pDateTimeEdit->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
else if (strLanguage.compare("Korea") == 0)
pDateTimeEdit->setLocale(QLocale(QLocale::Korean, QLocale::SouthKorea));
}
auto pDateEdit = qobject_cast<QDateEdit *>(obj);
if (pDateEdit != nullptr)
{
if (strLanguage.compare("ChineseT") == 0)
pDateEdit->setLocale(QLocale(QLocale::Chinese, QLocale::Taiwan));
else if (strLanguage.compare("English") == 0)
pDateEdit->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
else if (strLanguage.compare("Korea") == 0)
pDateEdit->setLocale(QLocale(QLocale::Korean, QLocale::SouthKorea));
}
auto pTableWidget = qobject_cast<QTableWidget *>(obj);
if (pTableWidget != nullptr)
{
for (int i = 0; i < pTableWidget->columnCount(); i++)
{
if (pTableWidget->horizontalHeaderItem(i))
pTableWidget->horizontalHeaderItem(i)->setText(translate(pTableWidget->horizontalHeaderItem(i)->text()));
}
}
auto pTabWidget = qobject_cast<QTabWidget *>(obj);
if (pTabWidget != nullptr)
{
for (int i = 0; i < pTabWidget->count(); i++)
pTabWidget->setTabText(i, translate(pTabWidget->tabText(i)));
}
auto pTreeWidget = qobject_cast<QTreeWidget *>(obj);
if (pTreeWidget != nullptr)
{
QTreeWidgetItemIterator item(pTreeWidget);
while (*item)
{
(*item)->setText(0, translate((*item)->text(0)));
QTreeWidgetItem *parent = (*item)->parent();
if (NULL == parent)
(*item)->setHidden(true);
++item;
}
}
auto pComboBox = qobject_cast<QComboBox *>(obj);
if (pComboBox != nullptr)
{
for (int i = 0; i < pComboBox->count(); i++)
pComboBox->setItemText(i, translate(pComboBox->itemText(i)));
}
}
}
}
//翻译
QString ControlsTranslate::translate(QString strName)
{
auto hash = m_pLanguageXml->getItemsHash();
auto itFind = hash.find(strName);
if (itFind != hash.end())
return hash[strName];
else
return strName;
return "";
}
//初始化
void ControlsTranslate::initialTranslateFile()
{
QString strFilePath = QApplication::applicationDirPath() + "/transalte.xml";
// 先初始化
if (!m_objectList.isEmpty())
{
foreach(QObject *obj, m_objectList)
{
auto pPushButton = qobject_cast<QPushButton *>(obj);
if (pPushButton != nullptr)
m_pLanguageXml->insertItem(pPushButton->text(), "");
auto pToolButton = qobject_cast<QToolButton *>(obj);
if (pToolButton != nullptr)
m_pLanguageXml->insertItem(pToolButton->text(), "");
auto pWidget = qobject_cast<QWidget *>(obj);
if (pWidget != nullptr)
m_pLanguageXml->insertItem(pWidget->windowTitle(), "");
auto pGroupBox = qobject_cast<QGroupBox *>(obj);
if (pGroupBox != nullptr)
m_pLanguageXml->insertItem(pGroupBox->title(), "");
auto pLabel = qobject_cast<QLabel *>(obj);
if (pLabel != nullptr)
m_pLanguageXml->insertItem(pLabel->text(), "");
auto pCheckBox = qobject_cast<QCheckBox *>(obj);
if (pCheckBox != nullptr)
m_pLanguageXml->insertItem(pCheckBox->text(), "");
auto pRadioButton = qobject_cast<QRadioButton *>(obj);
if (pRadioButton != nullptr)
m_pLanguageXml->insertItem(pRadioButton->text(), "");
auto pLineEdit = qobject_cast<QLineEdit *>(obj);
if (pLineEdit != nullptr)
{
m_pLanguageXml->insertItem(pLineEdit->text(), "");
m_pLanguageXml->insertItem(pLineEdit->placeholderText(), "");
}
auto pTableWidget = qobject_cast<QTableWidget *>(obj);
if (pTableWidget != nullptr)
{
for (int i = 0; i < pTableWidget->columnCount(); i++)
{
if (pTableWidget->horizontalHeaderItem(i))
m_pLanguageXml->insertItem(pTableWidget->horizontalHeaderItem(i)->text(), "");
}
}
auto pTabWidget = qobject_cast<QTabWidget *>(obj);
if (pTabWidget != nullptr)
{
for (int i = 0; i < pTabWidget->count(); i++)
m_pLanguageXml->insertItem(pTabWidget->tabText(i), "");
}
auto pTreeWidget = qobject_cast<QTreeWidget *>(obj);
if (pTreeWidget != nullptr)
{
QTreeWidgetItemIterator item(pTreeWidget);
while (*item)
{
m_pLanguageXml->insertItem((*item)->text(0), "");
QTreeWidgetItem *parent = (*item)->parent();
if (NULL == parent)
(*item)->setHidden(true);
++item;
}
}
auto pComboBox = qobject_cast<QComboBox *>(obj);
if (pComboBox != nullptr)
{
for (int i = 0; i < pComboBox->count(); i++)
m_pLanguageXml->insertItem(pComboBox->itemText(i), "");
}
}
}
// 再赋值
m_pLanguageXml->readItems(strFilePath);
if (m_objectList.size() > 0)
m_pLanguageXml->saveItems(strFilePath);
}
三、程序链接
https://download.csdn.net/download/u013083044/87262164