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

时间:2014-05-31 16:00:01

标签: qt internationalization qml qt5 qt-linguist

运行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()
            }
        }
    }
}

3 个答案:

答案 0 :(得分:8)

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

lupdate main.qml -ts main.ts

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

  

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

     

但是,SOURCES变量适用于C ++源文件。如果你   列出那里的QML或JavaScript源文件,编译器尝试构建   它们好像是C ++文件。作为一种解决方法,您可以使用   lupdate_only {...}条件语句,所以lupdate工具看到了   .qml文件,但C ++编译器忽略它们。

如果您在应用程序中指定.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
}

答案 1 :(得分:2)

从Qt 5.8.0 开始,.pro文件中不再需要任何技巧。

QML文件可以在.qrc资源容器中列出一次,它们将通过以下方式正确选取:

  1. 编译
  2. 运行时的应用程序
  3. lupdate for translation
  4. .pro文件:

    RESOURCES += application.qrc
    

    .qrc是一个XML文件,通常通过qtcreator进行管理而不查看其内容。有关qrc文件的更多信息,请访问: http://doc.qt.io/qt-5/qtquick-deployment.html#managing-resource-files-with-the-qt-resource-system

    .qrc支持在2016-10-25添加到lupdate: http://code.qt.io/cgit/qt/qttools.git/commit/?id=f2ebd51d96ad49eb826a4e37e67d506fffcbd40c 它没有进入Qt 5.7.1版本,但它将在5.7.2中提供(如果有的话)。

答案 2 :(得分:0)

使用lupdate_only似乎是一个可行的解决方案。 但请注意,QtCreator还会将qml文件作为源文件提取,因此现在导航项目时所有qml文件都会重复列出。

为了避免这种情况,我使用了一种不同的方法,我通过shell脚本更新翻译:

#!/bin/bash
../../5.5/gcc_64/bin/lupdate *.pro
for tr in *.ts
do
  ../../5.5/gvv_64/bin/lupdate *.qml -ts $ts
done
相关问题