QML翻译
我尝试在 QML 中使用翻译。我打开了一个新项目QtQuick项目,我选择QtQuick Componenets for Symbian作为QtQuick应用程序类型。 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 ,我使用语言学家翻译了 POSIX 表达式“Hello world!”对于其他东西,只需测试它是否正在翻译。然后我与语言学家创建了 inter_en.qm 文件。
但是当我在模拟器上运行该应用程序时,我没有得到“Hello world!”已翻译,尽管翻译器已成功加载(我在 qDebug 中得到“翻译:true”)。
我确信翻译器工作正常,因为当我用 QPushButton 取消部分代码的注释时(为此目的再次重复 lupdate 和语言学家的事情),然后是“Hello world!” QPushButton 中的表达式已正确翻译。
只有 QmlApplicationViewer 和 QML 文件未正确执行转换。 有什么疑问吗????
谢谢
更新,
我发现了以下内容:MainPage.qml 作为可重用组件导入到 main.qml 中。如果我在 main.qml 中使用 qsTr() ,那么文本会在 main.qml 中正确翻译。但是 MainPage.qml 中的文本未正确翻译。我想是因为将它作为可重用组件导入。有什么意见吗?经验?
更新2 - 解决方案
需要区分大小写创建翻译文件:
lupdate mainpage.qml -ts myapp_sk.ts is wrong
lupdate MainPage.qml -ts myapp_sk.ts is correct
I try to use translation in QML. I opened a new project QtQuick project, I chose QtQuick Componenets for Symbian as a QtQuick Application Type. Qt Creator created a application source tree with all standard files (main.cpp, main.qml, mainpage.qml...)
MainPage.qml is very simple:
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
}
}
My main.cpp file looks after implementation of QTranslator like this:
#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();
}
Then I run lupdate mainpage.qml -ts inter_en.ts , I have used linguist to translate the POSIX expression "Hello world!" to something else just test that it is translating. Then I created inter_en.qm file with linguist.
But when I run the application on simulator I dont get "Hello world!" translated, although the translator is loaded successfully (I get "Translating: true" in qDebug).
Translator is working correctly I am sure, because when I deremark part of the code with QPushButton , (again I repeat the lupdate and linguist things for this purpose) , then the "Hello world!" expression in QPushButton is translated correctly.
It is only the QmlApplicationViewer and QML file that is not performing translation correctly.
Any quesses?????
Thanks
UPDATE
I found out the following: MainPage.qml is imported as reusable component into main.qml. If I use qsTr() in main.qml then the text is translated correctly in main.qml. However text in MainPage.qml is not tranlated correcty. I guess due to importing it as reusable component. Any comments? Experiences?
UPDATE2 - SOLUTION
Translation files need to be created case sensitively:
lupdate mainpage.qml -ts myapp_sk.ts is wrong
lupdate MainPage.qml -ts myapp_sk.ts is correct
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当问题不是翻译本身,而是在运行时更改语言时,这可能会对您有所帮助。
如果您使用 app->installTranslator(translator); 加载新的 QTranslator;
它(QApplication)将触发一个更改事件。在您的 Qt 类中,您必须
在加载“main.qml”后使用 Somewhere 来捕获它:
QML 方面:
这对于 MS Windows 平台非常有效。
When the issue is not the translation per se, but changing language during runtime this may help you.
If you load a new QTranslator with app->installTranslator(translator);
it (QApplication) will fire a change event. In your Qt class you have to catch it with
Somewher after loading your "main.qml":
QML side:
This works very good for MS Windows Platform.
我还使用 QML 文件作为可重用组件,并且翻译完全没有问题。所以我想以下内容也可以帮助你。
我还猜想您不想为每个文件手动调用 lupdate 。因此,您应该在 .pro 文件中使用以下行,使其自动查找 QML 和 JS 文件中的所有可翻译短语(更正您的路径)...
它不是来自我的头脑,所以这里是源代码(有还有关于动态翻译的注释): https://forum.qt.io/topic/30076/is-there-a-way-to-use-linguist-in-global-context
I also use QML files as reusable components and I have no problem with translations at all. So I guess the following can help you as well.
I also guess you don't want to call lupdate manually for every file. So you should use the following lines in .pro file to make it look for all translatable phrases in QML and JS files automatically (correct your paths)...
It doesn't come from my head, so here is the source (there is also a note about dynamic translation): https://forum.qt.io/topic/30076/is-there-a-way-to-use-linguist-in-global-context