Automating generation of qm files: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
h1. Automating Generation of QM Files
h1. Automating Generation of QM Files


The "Qt Linguist Manual":http://doc.qt.io/qt-5/qtlinguist-index.html and "Qt reference documentation":http://doc.qt.io/qt-5/internationalization.html 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.
The "Qt Linguist Manual":http://doc.qt.io/qt-5/qtlinguist-index.html and "Qt reference documentation":http://doc.qt.io/qt-5/internationalization.html 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:
First, you specify the languages as values of the LANGUAGES variable using the language codes:
Line 9: Line 9:
Second, you add a function to generate a TS file for each language. In the following code example, the application name is ''QuickForecast'':
Second, you add a function to generate a TS file for each language. In the following code example, the application name is ''QuickForecast'':


</code><br />defineReplace(prependAll) {<br /> for(a,$$1):result ''= $$2$${a}$$3<br /> return($$result)<br />}
</code>
<br />TRANSLATIONS = $$prependAll(LANGUAGES, $$PWD/translations/QuickForecast_, .ts)<br /><code>
defineReplace(prependAll) {
<br />Third, you add the TRANSLATIONS_FILES variable and run lrelease to generate the QM files and to embed them in the application resources:
for(a,$$1):result ''= $$2$${a}$$3
<br /></code><br />TRANSLATIONS_FILES =
return($$result)
<br />qtPrepareTool(LRELEASE, lrelease)<br />for(tsfile, TRANSLATIONS) {<br /> qmfile = $$shadowed($$tsfile)<br /> qmfile ~= s,.ts$,.qm,<br /> qmdir = $$dirname(qmfile)<br /> !exists($$qmdir) {<br /> mkpath($$qmdir)|error(&quot;Aborting.&quot;)<br /> }<br /> command = $$LRELEASE -removeidentical $$tsfile -qm $$qmfile<br /> system&amp;amp;#40;$$command&amp;amp;#41;|error(&quot;Failed to run: $$command&amp;quot;)<br /> TRANSLATIONS_FILES''= $$qmfile<br />}<br /><code>
}
 
TRANSLATIONS = $$prependAll(LANGUAGES, $$PWD/translations/QuickForecast_, .ts)
<code>
 
Third, you add the TRANSLATIONS_FILES variable and run lrelease to generate the QM files and to embed them in the application resources:
 
</code>
TRANSLATIONS_FILES =
 
qtPrepareTool(LRELEASE, lrelease)
for(tsfile, TRANSLATIONS) {
qmfile = $$shadowed($$tsfile)
qmfile ~= s,.ts$,.qm,
qmdir = $$dirname(qmfile)
!exists($$qmdir) {
mkpath($$qmdir)|error("Aborting.")
}
command = $$LRELEASE -removeidentical $$tsfile -qm $$qmfile
system($$command)|error("Failed to run: $$command")
TRANSLATIONS_FILES''= $$qmfile
}
<code>


== Adding qmake Targets for Convenience ==
== Adding qmake Targets for Convenience ==
Line 19: Line 41:
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.
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.


</code><br />wd = $$replace(PWD, /, $$QMAKE_DIR_SEP)
</code>
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 &amp;&amp; $$LUPDATE $$SOURCES $$APP_FILES -ts $$file
QMAKE_EXTRA_TARGETS''= ts-$$lang
}
ts-all.commands = cd $$PWD &amp;&amp; $$LUPDATE $$SOURCES $$APP_FILES -ts $$TSFILES
QMAKE_EXTRA_TARGETS ''= ts-all
<code>
 
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(LUPDATE, lupdate)<br />LUPDATE ''= -locations relative -no-ui-lines<br />TSFILES = $$files($$PWD/translations/QuickForecast_'''.ts) $$PWD/translations/QuickForecast_untranslated.ts<br />for(file, TSFILES) {<br /> lang = $$replace(file, .'''_([^/]*).ts, 1)<br /> v = ts-$${lang}.commands<br /> $$v = cd $$wd &amp;&amp; $$LUPDATE $$SOURCES $$APP_FILES -ts $$file<br /> QMAKE_EXTRA_TARGETS''= ts-$$lang<br />}<br />ts-all.commands = cd $$PWD &amp;&amp; $$LUPDATE $$SOURCES $$APP_FILES -ts $$TSFILES<br />QMAKE_EXTRA_TARGETS ''= ts-all<br /><code>
</code>
<br />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)
<br /></code><br />qtPrepareTool(LCONVERT, lconvert)<br />LCONVERT''= -locations none<br />isEqual(QMAKE_DIR_SEP, /) {<br /> commit-ts.commands =  cd $$wd;  git add -N translations/*''.ts &amp;&amp;  for f in `git diff-files —name-only translations/*''.ts`; do  $$LCONVERT -i f -o f;  done;  git add translations/*_.ts &amp;&amp; git commit<br />} else {<br /> commit-ts.commands =  cd $$wd &amp;&amp;  git add -N translations/*''.ts &amp;&amp;  for /f usebackq %%f in (`git diff-files —name-only — translations/*''.ts`) do  $$LCONVERT -i %f -o%f $$escape_expand(nt)  cd $$wd &amp;&amp; git add translations/*_.ts &amp;&amp; git commit<br />}<br />QMAKE_EXTRA_TARGETS += commit-ts
LCONVERT''= -locations none
isEqual(QMAKE_DIR_SEP, /) {
commit-ts.commands =  cd $$wd;  git add -N translations/*''.ts &amp;&amp;  for f in `git diff-files —name-only translations/*''.ts`; do  $$LCONVERT -i f -o f;  done;  git add translations/*_.ts &amp;&amp; git commit
} else {
commit-ts.commands =  cd $$wd &amp;&amp;  git add -N translations/*''.ts &amp;&amp;  for /f usebackq %%f in (`git diff-files —name-only — translations/*''.ts`) do  $$LCONVERT -i %f -o%f $$escape_expand(nt)  cd $$wd &amp;&amp; git add translations/*_.ts &amp;&amp; git commit
}
QMAKE_EXTRA_TARGETS += commit-ts

Revision as of 11:16, 25 February 2015

h1. Automating Generation of QM Files

The "Qt Linguist Manual":http://doc.qt.io/qt-5/qtlinguist-index.html and "Qt reference documentation":http://doc.qt.io/qt-5/internationalization.html 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:

LANGUAGES = de<code>

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

defineReplace(prependAll) {

for(a,$$1):result = $$2$${a}$$3
return($$result)

}

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:

TRANSLATIONS_FILES =

qtPrepareTool(LRELEASE, lrelease) for(tsfile, TRANSLATIONS) {

qmfile = $$shadowed($$tsfile)
qmfile ~= s,.ts$,.qm,
qmdir = $$dirname(qmfile)
!exists($$qmdir) {
mkpath($$qmdir)|error("Aborting.")
}
command = $$LRELEASE -removeidentical $$tsfile -qm $$qmfile
system($$command)|error("Failed to run: $$command")
TRANSLATIONS_FILES= $$qmfile

}

== 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
QMAKE_EXTRA_TARGETS= ts-$$lang

} ts-all.commands = cd $$PWD && $$LUPDATE $$SOURCES $$APP_FILES -ts $$TSFILES QMAKE_EXTRA_TARGETS = ts-all

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

} QMAKE_EXTRA_TARGETS += commit-ts