Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt linguist lupdate ignores qml files

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()
            }
        }
    }
}
like image 595
Appleshell Avatar asked May 31 '14 16:05

Appleshell


People also ask

How to generate TS file in Qt?

Select Extensions > Qt VS Tools > Create New Translation File. In the Language field, select a language from the list of supported languages. In the Filename field, enter a filename for the translation file. Select OK to create the file and have it listed in Translation Files in Visual Studio's Solution Explorer.

What is qsTr in Qml?

1. Use qsTr() for all Literal User Interface Strings. Strings in QML can be marked for translation using the qsTr(), qsTranslate(), qsTrId(), QT_TR_NOOP(), QT_TRANSLATE_NOOP(), and QT_TRID_NOOP() functions. The most common way of marking strings is with the qsTr() function.

How do you run Qt Linguist?

Run Qt Linguist from the taskbar menu, or by double clicking the desktop icon, or by entering the command linguist at the command line. Once Qt Linguist has started, choose File|Open from the menu bar and select a translation source (TS file) to load.

What is Qt Linguist?

Qt Linguist is a tool for adding translations to Qt applications. Once you have installed Qt, you can start Qt Linguist in the same way as any other application on the development host.


2 Answers

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
}
like image 97
Nejat Avatar answered Oct 10 '22 23:10

Nejat


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).

like image 32
Mark Ch Avatar answered Oct 11 '22 00:10

Mark Ch