Automating generation of qm files

From Qt Wiki
Jump to: navigation, search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

Automating Generation of QM Files

The Qt Linguist Manual and Qt reference documentation describe the localization process in detail. However, you can use an alternative (unofficial) way to register TS files for the languages listed in the .pro file and to generate the QM files from them. This method is used by the Qt Creator project and the Quick Forecast demo application, for example.

First, you specify the languages as values of the LANGUAGES variable using the language codes:


Second, you add a function to generate a TS file for each language. In the following code example, the application name is QuickForecast:

# parameters: var, prepend, append
defineReplace(prependAll) {
 for(a,$$1):result += $$2$${a}$$3

TRANSLATIONS = $$prependAll(LANGUAGES, $$PWD/translations/QuickForecast_, .ts)

Third, you add the TRANSLATIONS_FILES variable and run lrelease to generate the QM files and to embed them in the application resources:


qtPrepareTool(LRELEASE, lrelease)
for(tsfile, TRANSLATIONS) {
 qmfile = $$shadowed($$tsfile)
 qmfile ~= s,.ts$,.qm,
 qmdir = $$dirname(qmfile)
 !exists($$qmdir) {
 command = $$LRELEASE -removeidentical $$tsfile -qm $$qmfile
 system($$command)|error("Failed to run: $$command")

Adding qmake Targets for Convenience

You can further automate the process for creating TS files and committing them to the Git version control system by adding qmake extra targets. The following code generates a generic TS file, using the QuickForecast application as an example. You need to rename the generated QuickForecast_untranslated.ts using the appropriate language code (for example, as QuickForecast_de.ts) and add the language code as a value of the LANGUAGES variable.

wd = $$replace(PWD, /, $$QMAKE_DIR_SEP)

qtPrepareTool(LUPDATE, lupdate)
LUPDATE += -locations relative -no-ui-lines
TSFILES = $$files($$PWD/translations/QuickForecast_'''.ts) $$PWD/translations/QuickForecast_untranslated.ts
for(file, TSFILES) {
 lang = $$replace(file, .'''_([^/]*).ts, 1)
 v = ts-$${lang}.commands
 $$v = cd $$wd && $$LUPDATE $$SOURCES $$APP_FILES -ts $$file
ts-all.commands = cd $$PWD && $$LUPDATE $$SOURCES $$APP_FILES -ts $$TSFILES

The following code makes a new target for lconvert and commits the TS files. Use lconvert to remove information about the location of strings in the TS files and thus save space:

qtPrepareTool(LCONVERT, lconvert)
LCONVERT += -locations none
isEqual(QMAKE_DIR_SEP, /) {
 commit-ts.commands =  cd $$wd;  git add -N translations/*''.ts &&  for f in `git diff-files —name-only translations/*''.ts`; do  $$LCONVERT -i f -o f;  done;  git add translations/*_.ts && git commit
} else {
 commit-ts.commands =  cd $$wd &&  git add -N translations/*''.ts &&  for /f usebackq %%f in (`git diff-files —name-only — translations/*''.ts`) do  $$LCONVERT -i %f -o%f $$escape_expand(nt)  cd $$wd && git add translations/*_.ts && git commit