当前位置:网站首页>QT学习:使用JSON/XML等非ts文件实现多语言国际化
QT学习:使用JSON/XML等非ts文件实现多语言国际化
2022-07-29 05:07:00 【上官宏竹】
注意:如果不是CSDN网站显示本篇文章,请于底部点击“阅读原文”来阅读本篇文章!
QT学习:使用JSON/XML等非ts文件实现多语言国际化
qt默认的国际化是使用
Linguist
+ .ts
,来实现国际化的。但一般公司内部可能存在翻译部门使用其他格式的语言文件,或者自己搭建了一个翻译网站,你能下载到的肯定不会是ts文件,而是比较常见的文件格式,比如xml/json等等。 现在我们使用JSON文件来存放翻译的key和翻译后的值value,但项目源码内部还是使用qt提供的
QObject::tr("key")
以及qml使用的 qsTr("key")
方法来使用翻译。 主要的方法也比较简单:
- 1、使用一个map类型,存储JSON解析后的key-value值
- 2、新建一个类
MyTranslator
继承自QTranslator
,然后重载QTranslator::translate
方法 - 3、使用
QApplication::instance()->installTranslator
注册MyTranslator
的实例 - 4、代码内部使用
QObject::tr("key")
方式实现国际化
下面说下MyTranslator
的实现
解析Json
主要是将JSON文件中的key-value,解析后存放在map表中,供后面的translate
使用
void MyTranslator::LoadLocalCachedInfo(QString langCode)
{
QString fileName = (QApplication::applicationDirPath() +
TRANS_DIR + "/" + LOCAL_TRANS_FILE_NAME).arg(langCode);
QFile langFile(fileName);
if(!langFile.exists()) {
return;
}
langFile.open(QIODevice::ReadOnly);
QByteArray byteInfo = langFile.readAll();
if (!LoadJsonTransInfo(byteInfo)) {
return;
}
emit LanguageChanged();
}
bool MyTranslator::LoadJsonTransInfo(QByteArray& byteinfo)
{
QJsonObject jsonObj;
QJsonParseError error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(byteinfo, &error));
if (jsonDoc.isEmpty() || jsonDoc.isNull()) {
QString errorStr = error.errorString();
return false;
}
m_loadedTransInfo.clear();
for(auto& transKey : jsonDoc.object().keys()) {
m_loadedTransInfo[transKey.toStdString()] =
jsonDoc.object()[transKey].toString().toStdString();
}
return true;
}
重载translate
按照[virtual] QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const
的原型,重载一个即可。主要是通过传入的sourceText
,在前面的map表中找到对应的value,即翻译字符串,返回即可。
QString MyTranslator::translate(const char *context,
const char *sourceText,
const char *disambiguation, int n) const
{
// if no result return original
if(m_loadedTransInfo.count(sourceText) <= 0) {
return sourceText;
}
// from loaded value
return QString::fromStdString(m_loadedTransInfo.at(std::string(sourceText)));
}
注册MyTranslator
实例
注册实例,一定要在主线程,另一个是要在使用QObject::tr("key")
之前注册。
QApplication::instance()->installTranslator(&MyTranslator::getInstance());
运行结果
中文JSON文件:
{
"button_name": "按钮名字",
"window_name": "主窗口名字"
}
完整实现可以微信搜索公众号:“上官宏竹”,关注并回复:“qt_language”,获取资源链接。有任何疑问也可以公众号里留言咨询。
微信公众号搜索:“上官宏竹”,关注并留言咨询,可接各类需求。
边栏推荐
- Office提示系统配置无法运行怎么办?
- Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
- P5714 [deep foundation 3. Case 7] obesity
- Deep learning brush a bunch of tricks of SOTA
- The method and detailed code of automatically pop-up and QQ group when players visit the website
- Operator operation list of spark
- Do you remember the process analysis and function implementation of post notification?
- 关于thymeleaf的配置与使用
- AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
- Button for QT custom switch effect
猜你喜欢
Scikit learn -- steps and understanding of machine learning application development
Numpy Foundation
Unity metaverse (III), protobuf & socket realize multi person online
时间序列分析的表示学习时代来了?
Google GTEST event mechanism
How to solve the problem of configuring the progress every time Office2010 is opened?
ARFoundation从零开始3-创建ARFoundation项目
Force deduction ----- sort odd and even subscripts respectively
SparkSql批量插入或更新,保存数据到Mysql中
Do you remember the process analysis and function implementation of post notification?
随机推荐
一文带你搞懂环绕通知@Around与最终通知@After的实现
AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
ARFoundation入门教程10-平面检测和放置
Learn the first program of database
AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块
Rolabelimg to data format data
How to install Office2010 installation package? How to install Office2010 installation package on computer
浅谈AspectJ框架
Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
开区网站打开自动播放音乐的添加跟修改教程
Pytorch learning notes
ARFoundation从零开始8-Geospatial API(地理空间)开发
【config】配置数组参数
roLabelImg转DATO格式数据
传奇服务端如何添加地图
缓存穿透、缓存击穿、缓存雪崩以及解决方法
Scikit learn -- steps and understanding of machine learning application development
2021-10-11
Jackson parsing JSON detailed tutorial
DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021