qml如何进行动态翻译
来源:互联网 发布:xlsx 导入 java 编辑:程序博客网 时间:2024/06/14 03:02
Qt已经提供了语言家(linguist)生成语言库文件(*qm)和自带翻译的类,很方便的实现语言的切换。
功能概述:点击按钮,实现对应语言切换,关闭应用再次启动,保留上一次的设置。
一.qml实现国际化的过程:
在项目中加载翻译文件,在文本中利用标识需要翻译的文本,当程序接收到切换语言的信号时,应用会在加载的翻译文件查找对应的翻译,显示新的语言。
二、qml国际化的实现
2.1在pro文件添加TRANSLATION +=translate_cn.ts
打开qt命令行,执行lupdate XXX/*** -ts translate_cn.ts 生成翻译文件,在这个文件里面会包含所有标识过的文本,
但是还没有翻译。
注意:The lupdate
tool extracts user interface strings from your application. lupdate reads your application's .pro file to identify which source files contain texts to be translated. This means your source files must be listed in the SOURCES
or HEADERS
entry in the .pro file. If your files are not listed the texts in them will not be found.However, the SOURCES variable is intended for C++ source files. If you list QML or JavaScript source files there, the compiler tries to build them as though they are C++ files. As a workaround, you can use an lupdate_only{...}
conditional statement so thelupdate
tool sees the .qml files but the C++ compiler ignores them.
For example, the following .pro file snippet specifies two .qml files in the application.
lupdate_only{SOURCES = main.qml \ MainPage.qml}截取自 Qt助手中的:internationlization and with quick 使用lupdate_only 可以文本匹配文件,自动识别qml中需要翻译的文本,点击菜单栏的工具—》外部—》qt语言家-》更新翻译翻译(lupdate)自动生成tanslate_cn.ts文件,编辑对应的翻译语言,翻译完成,保存,点击发布(或者使用lrelease translate_cn.ts)生成对应的translate_cn.qm文件,为了方便加载,语言库文件添加到资源里面。
3,语言库的加载,通过C++实现语言动态切换,保存当前语言设置,再次运行,保持最后一次设置的语言
构造自己的类,继承qt中的Qtranslation类
在translate.h的文件中
classlanguageTranslator:publicQObject
{
Q_OBJECT//实现C++与qml之间的通信
Q_PROPERTY (QString emptyString READ getEmptyString NOTIFY languageChanged) public :
languageTranslator ();
QString getEmptyString();
Q_INVOKABLE void setlectLanguage(int language);
signals:
void languageChanged();
private:
QTranslator *translator1;
};
在translate.cpp 中languageTranslator::languageTranslator()
{
translator1 = new QTranslator(this);
}
QString languageTranslator::getEmptyString()
{
return "";
}
void languageTranslator::setlectLanguage(int language)
{
QSettings *readconfig = new QSettings(".config.ini",QSettings::IniFormat);
if(language == 1)
{
translator1->load(":/lang.qm");
qApp->installTranslator(translator1);
}
if(language == 2)
{
qApp->removeTranslator(translator1);
}
readconfig->setValue("Language/languageflag",language);
readconfig->value("Language/languageflag").toFloat();
emit languageChanged();
}qApp一个指针
通过每一次启动读取配置文本存储语言标志量(languageflag)实现显示最后一次设置的语言
当每次修改语言时候,main.cpp会对配置文本中变量languageflag做出修改,记录当前语言。
languageTranslatormyObj;
QSettings *readconfig = new QSettings(".config.ini",QSettings::IniFormat);
int flag = readconfig->value("Language/languageflag").toFloat();
myObj.setlectLanguage(flag);
view->rootContext()->setContextProperty("rootItem",(QObject *)&myObj);
将C++中的类注册到qml中使用,这样在qml就可以会用rootItem了import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Window 2.2
import SceneGraphRendering 1.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQml 2.2
import QmlMinAdjust 1.0
import languageTranslator 1.0
import QtGraphicalEffects 1.0
import "."
Rectangle{
id: root
width:1280
height:720
Row{
Rectangle{
id:directory
x:0
y:0
width: root.width
height: root.height
color: "black"
Column{
Text {
id:language
x:root.width*1/2
y:root.height*1/6
font.pixelSize: 40
width:root.width
height:root.height*1/4
text:qsTr("language") +rootItem.emptyString
color: "green"
}
Button{
id: chinese
x: 0
y: root.height*2/6
width: root.width
height: root.height*1/6
text:"中文"
style:ButtonFlatStyle
{
font.family: "FontAwesome"
font.pixelSize: 20
}
onClicked: rootItem.setlectLanguage(1)
}
Button{
id: english
x: 0
y: root.height*3/6
width: root.width
height: root.height*1/6
text:"English"
style:ButtonFlatStyle
{
font.family: "FontAwesome"
font.pixelSize: 20
}
onClicked: {
rootItem.setlectLanguage(2)
}
}
}
}
}
}
Qt助手中的:internationlization and Localization with quick 可供参考
nternationalization and Localization with Qt Quick
Internationalization and Localization with Qt Quick
Internationalization and Localization with Qt Quick
参考连接
https://wiki.qt.io/How_to_do_dynamic_translation_in_QML
- qml如何进行动态翻译
- qml如何进行动态翻译
- QML如何创建动态组件
- Qml TabView 如何动态创建Tab
- QML ListView翻译
- 关于QML Book翻译
- ChartView QML 类型翻译
- Legend QML 类型翻译
- AbstractAxis QML 类型翻译
- 如何在Ubuntu QML应用中进行语言录音
- Qt/Qml 多语言翻译
- Jsp如何进行动态分页
- 动态创建qml
- QML - 动态柱状图
- qml 动态创建TableView
- Qml动态语言切换
- QML之动态加载
- 如何使用QML动态产生Component来完成我们的气球游戏 (1)
- 使用Lucene词频统计与d3.cloud展示的中文英文词云系统
- 数据库(3)---基本操作
- Kotlin 实践项目(密码本)
- Spring和它的AOP
- 首发博客,记录心情
- qml如何进行动态翻译
- 2017年8月18日提高组T2 队伍统计
- 文本三剑客之sed
- 大量数据如何排序
- 康托展开(八数码问题)
- struts2 二
- 多视图学习
- 2017年9月1日-2017年9月3日训练总结
- bzoj 1620: [Usaco2008 Nov]Time Management 时间管理(贪心)