5

When running lupdate none of the qsTr in the qml files are recognized. The resulting .ts file doesn't contain any translation context.

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

The project should be set up correctly:

OTHER_FILES += \
    content/main.qml

TRANSLATIONS += \
    translations/en.ts

In the main.qml among other things:

menuBar: MenuBar {
    Menu {
        title: qsTr("File")
        MenuItem {
            text: qsTr("Open")
            onTriggered: Qt.quit();
        }
    }
    Menu {
        title: qsTr("...")
        MenuItem {
            text: qsTr("About")
            onTriggered: {
                aboutApplicationDialog.open()
            }
        }
    }
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
Appleshell
  • 7,088
  • 6
  • 47
  • 96

3 Answers3

12

You can generate the translation file by running lupdate on QML file :

lupdate main.qml -ts main.ts

To get the .ts file by running lupdate on the project .pro file you can use a workaround. From the Qt documentation :

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.

If you specify your .qml files in the application like :

lupdate_only{
SOURCES = content/main.qml
}

When you run lupdate on the project .pro, the resulting .ts file will contain the QML translation context.

Note that you must stick to the brace style shown, using an alternate style such as e.g.

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

fails (at least on OS X 10.12 / Qt 5.7) with large amounts of compiler warnings and errors that are far from giving any hint at the actual problem, e.g.

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'

Alternately, you can use a continuation character:

lupdate_only \
{
SOURCES = content/main.qml
}
ssc
  • 9,528
  • 10
  • 64
  • 94
Nejat
  • 31,784
  • 12
  • 106
  • 138
3

As of Qt 5.8.0 no tricks are required in the .pro file anymore.

QML files can be listed once inside a .qrc Resource Container, and they will be picked up correctly by:

  1. compiler
  2. application at run-time
  3. lupdate for translation

.pro file:

RESOURCES += application.qrc

.qrc is an XML file, usually managed via qtcreator without looking at its contents. More info about qrc files can be found here: http://doc.qt.io/qt-5/qtquick-deployment.html#managing-resource-files-with-the-qt-resource-system

.qrc support was added to lupdate on 2016-10-25: http://code.qt.io/cgit/qt/qttools.git/commit/?id=f2ebd51d96ad49eb826a4e37e67d506fffcbd40c It didn't make it into Qt 5.7.1 release, but it will be available in 5.7.2 (if there ever is one).

Mark Ch
  • 2,840
  • 1
  • 19
  • 31
0

Using lupdate_only seems like a viable solution. However note that QtCreator will also pickup the qml files as source files, so now all qml files are listed duplicate when you navigate the project.

To avoid this I used a different approach where I update the translations through a shell script:

#!/bin/bash
../../5.5/gcc_64/bin/lupdate *.pro
for tr in *.ts
do
  ../../5.5/gvv_64/bin/lupdate *.qml -ts $ts
done
Geoffrey VL
  • 161
  • 8