gpt4 book ai didi

Qt linguist lupdate 忽略 qml 文件

转载 作者:行者123 更新时间:2023-12-02 06:56:51 28 4
gpt4 key购买 nike

运行lupdate时,qml 文件中的任何qsTr都无法识别。生成的 .ts 文件不包含任何翻译上下文。

$ lupdate -verbose App.pro
Updating 'translations/en.ts'...
Found 0 source text(s) (0 new and 0 already existing)

项目应该正确设置:

OTHER_FILES += \
content/main.qml

TRANSLATIONS += \
translations/en.ts

在 main.qml 中:

menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Open")
onTriggered: Qt.quit();
}
}
Menu {
title: qsTr("...")
MenuItem {
text: qsTr("About")
onTriggered: {
aboutApplicationDialog.open()
}
}
}
}

最佳答案

您可以通过在 QML 文件上运行 lupdate 来生成翻译文件:

lupdate main.qml -ts main.ts

要通过在项目 .pro 文件上运行 lupdate 来获取 .ts 文件,您可以使用解决方法。来自 Qt 文档:

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 the lupdate tool sees the .qml files but the C++ compiler ignores them.

如果您在应用程序中指定 .qml 文件,例如:

lupdate_only{
SOURCES = content/main.qml
}

当您在项目 .pro 上运行 lupdate 时,生成的 .ts 文件将包含 QML 翻译上下文。

请注意,您必须坚持使用所示的大括号样式,使用替代样式,例如

# DON'T USE, FAILS!
lupdate_only
{
SOURCES = content/main.qml
}

失败(至少在 OS X 10.12/Qt 5.7 上),并出现大量编译器警告和错误,这些警告和错误远未给出实际问题的任何提示,例如

clang: warning: <qml source file>: 'linker' input unused
clang: warning: argument unused during compilation: '-g'
clang: warning: argument unused during compilation: '-isysroot /Applications/Xcode_7.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk'
...
clang: error: no such file or directory: 'Page1.o'
clang: error: no such file or directory: 'Page1Form.ui.o'

或者,您可以使用连续字符:

lupdate_only \
{
SOURCES = content/main.qml
}

关于Qt linguist lupdate 忽略 qml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23972000/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com