Qt on Visual Studio and QML translation files
Develop a Qt application using Visual Studio is possible through the Qt plugin. This plugin manage the basic settings for use Qt libraries inside a VS project. With a limited support is possible to develop QML application but it miss the syntax highlight of QML code and some other features available using Qt Creator.
However, accepting these limits, it can be done to use VS for develop a QML application. The real problem came when you want to generate a language translation file (.ts). Qt VS plugin is able to generate the translation file for the current project files but, looking into the plugin source code (the check refer to 1.2.5 version), it seem the generation process cover only .cpp, .h and .ui files. All the .qml files are ignored and no translation is generated. The Qt translation process is divided in three steps. At first a special utility called lupdate is launched by the plugin with the list of all files to scan (that, as already said, cover only .cpp, .h and .ui extensions). This tool check inside the source files and collect all the string "signed" as to be translated (specified by tr("") for C++ and qsTr("") for QML). Once finished generate a .ts file that is an XML file containing the info regarding all the strings. Second step is to use another utility called Qt Linguist allowing to translate all the strings just collected. Final launch another tool called lrelease that make a sort of "compilation" of .ts file (converting in a .qm file) that is the compact version to load for allow Qt translation engine working. All these steps can be easily done using Qt Visual Studio plugin but, as said, the automatic file scan of lupdate doesn't cover .qml files. Fortunately in the plugin settings panel is possible to add some options to use with lupdate utility. The field to use is the following:
Here you have to enter the following additonal options:
-extensions qml -recursive qml_files_path
The first option instruct lupdate to scan for .qml files only and the second specify the path where .qml files are stored (change qml_file_path with your path). All folder and subfolders will be scanned. Once add these options the .ts file generated will contain the full list of strings to be translated from .cpp and .qml files available in the project. At the end of scanning process the plugin will automatically create a new filter into Visual Studio project tree called "Translation Files". Inside this filter a translation file will be added in the format appname_xx.ts where instead of xx there is the language code selected for the translation. Another step that can be automatized is the compiling of this .ts file (using the lrelease tool) when the file .ts itself is changed. For instruct Visual Studio to make this task open the .ts file property window as follow:
In the Element Type field select Custom Build Tool and you'll get the fields as screenshot above. Now insert the following strings into the fields as follow:
|Command Line||"$(QTDIR)\bin\lrelease.exe" "%(FullPath)"|
|Description||Compiling translation file|
Click over Apply and the lrelease tool will be launched every time the .ts will be detected as modified. Please note you have to change the qm_file_path with the path where you want to be saved the generated .qm file. The result will be a file with same name but extension .qm that is the file to be used by the Qt translator engine.