现在的位置: 首页 > 综合 > 正文

QML 中文支持

2012年03月30日 ⁄ 综合 ⁄ 共 856字 ⁄ 字号 评论关闭

有三种方式:

1,国际化

2:写uincode吗

3:直接写文字再QML文件中,和symbian一样,文件保存成utf8

 

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

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

#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 文件

抱歉!评论已关闭.