QML 中文支持

QML 中文支持

在 QML 中使用中文,目前似乎只有两种方式

  • 使用 Qt 的国际化功能(与QtScript完全一样,使用 qsTr())

  • 使用 unicode 的转义字符("\uxxxx")
例子

main.cpp

  • 加载翻译文件
  • 加载qml文件
#include <QtCore/QTranslator>
#include <QtGui/QApplication>
#include <QtDeclarative/QDeclarativeView>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QTranslator translator;
translator.load("first_zh_CN.qm", "i18n");
app.installTranslator(&translator);

QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("first.qml"));
view.show();

return app.exec();
}

first.qml

  • 待翻译字符串 qsTr("text")
  • "我是中文"的unicode表示 "\u6211\u662f\u4e2d\u6587"

Rectangle {
id: page
width: 100
height: 100
color: "red"
Text {
id: s1
x: 35
y: 15
text: qsTr("text")
}
Text {
id: s2
x: 35
y: 63
text: "\u6211\u662f\u4e2d\u6587"
}
}
  • 提取或更新待翻译字符串

lupdate first.qml -ts i18n/first_zh_CN.ts
  • 调用 linguist 翻译文本,并发布 first_zh_CN.qm 文件
截图 QML 中文支持_第1张图片
QML 中文支持_第2张图片

你可能感兴趣的:(QML 中文支持)