我尝试在 QML 中使用翻译。我打开了一个新项目 QtQuick 项目,我选择了 QtQuick Componenets for Symbian 作为 QtQuick Application Type。Qt Creator 创建了一个包含所有标准文件(main.cpp、main.qml、mainpage.qml...)的应用程序源代码树
MainPage.qml 非常简单:
import QtQuick 1.1
import com.nokia.symbian 1.1
Page {
id: mainPage
Text {
anchors.centerIn: parent
text: qsTr('Hello world!')
color: platformStyle.colorNormalLight
font.pixelSize: 20
}
}
我的 main.cpp 文件会像这样处理 QTranslator 的实现:
#include "qmlapplicationviewer.h"
#include <QTranslator>
#include <QPushButton>
#include <QDebug>
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QTranslator* translator = new QTranslator;
qDebug()<<"Translating: "<<translator->load(QString("qml/International/inter_en"));
app->installTranslator(translator);
//QPushButton hello(QPushButton::tr("Hello world!"));
// hello.resize(100, 30);
// hello.show();
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
viewer->setMainQmlFile(QLatin1String("qml/International/main.qml"));
viewer->showExpanded();
return app->exec();
}
然后我运行 lupdate mainpage.qml -ts inter_en.ts ,我使用 linguist 翻译了 POSIX 表达式“Hello world!” 其他东西只是测试它正在翻译。然后我用语言学家创建了 inter_en.qm 文件。
但是当我在模拟器上运行应用程序时,我没有得到“Hello world!” 已翻译,尽管翻译器已成功加载(我在 qDebug 中得到“翻译:真”)。
我确信翻译器工作正常,因为当我使用 QPushButton 取消部分代码时,(为此我再次重复 lupdate 和语言学家的事情),然后是“Hello world!” QPushButton 中的表达式被正确翻译。
只有 QmlApplicationViewer 和 QML 文件没有正确执行翻译。有什么问题吗??????
谢谢
更新
我发现以下内容: MainPage.qml 作为可重用组件导入 main.qml。如果我在 main.qml 中使用 qsTr(),那么文本会在 main.qml 中正确翻译。但是 MainPage.qml 中的文本翻译不正确。我猜是因为将其作为可重用组件导入。任何意见?经验?
UPDATE2 - 解决方案
翻译文件需要区分大小写:
lupdate mainpage.qml -ts myapp_sk.ts is wrong
lupdate MainPage.qml -ts myapp_sk.ts is correct