Qt语言学家lupdate忽略了qml文件

App*_*ell 5 qt internationalization qml qt5 qt-linguist

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

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

项目应正确设置:

OTHER_FILES += \
    content/main.qml

TRANSLATIONS += \
    translations/en.ts
Run Code Online (Sandbox Code Playgroud)

在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()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Nej*_*jat 9

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

lupdate main.qml -ts main.ts
Run Code Online (Sandbox Code Playgroud)

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

lupdate工具从应用程序中提取用户界面字符串.lupdate读取应用程序的.pro文件,以识别哪些源文件包含要翻译的文本.这意味着您的源文件必须列在.pro文件的SOURCES或HEADERS条目中.如果未列出您的文件,则无法找到其中的文本.

但是,SOURCES变量适用于C++源文件.如果在那里列出QML或JavaScript源文件,编译器会尝试将它们构建为C++文件.作为一种变通方法,您可以使用lupdate_only {...}条件语句,以便lupdate工具查看.qml文件,但C++编译器会忽略它们.

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

lupdate_only{
SOURCES = content/main.qml
}
Run Code Online (Sandbox Code Playgroud)

当您运行lupdate项目.pro时,生成的.ts文件将包含QML转换上下文.

请注意,您必须坚持使用替代样式(如例如)所示的支撑样式

# DON'T USE, FAILS!
lupdate_only
{
SOURCES = content/main.qml
}
Run Code Online (Sandbox Code Playgroud)

失败(至少在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'
Run Code Online (Sandbox Code Playgroud)

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

lupdate_only \
{
SOURCES = content/main.qml
}
Run Code Online (Sandbox Code Playgroud)