2

What is the best (proper) way to organize compiled translations (*.qm) into resources? *.qm files referred in qrc file and generated by two (three) extra targets this way:

trans_update.commands = lupdate $$_PRO_FILE_
trans_update.depends = $$_PRO_FILE_

trans_release.commands = lrelease $$_PRO_FILE_
trans_release.depends = trans_update $$TRANSLATIONS

translate.depends = trans_release

QMAKE_EXTRA_TARGETS += trans_update trans_release translate deploy

CONFIG(release, debug|release) {
   DESTDIR=release
   PRE_TARGETDEPS += translate
}

but the problem is at the moment qmake runs first time, there're no qm files generated yet and make prints errors like:

    RCC: Error in 'qml.qrc': Cannot find file ...

I don't like an idea of saving compiled qm files into VSC.

Is there a way to organize it nicely?

greybeard
  • 2,249
  • 8
  • 30
  • 66
mbg033
  • 501
  • 5
  • 18
  • First thing which comes to my mind: what about ignoring `*.qm` files in regard of VCS? I might come up with a better solutions in some minutes. – maxik Jul 26 '16 at 17:03
  • Thanks, seems to be a solution if qrc file refers *.qm files – mbg033 Jul 27 '16 at 19:33

2 Answers2

4

I like to point out a solution which I use in some projects. It might be far from perfect, but it works out nicely.

CONFIG(release, debug|release) {
    TRANSLATION_TARGET_DIR = $${OUT_PWD}/release/translations
    LANGUPD_OPTIONS = -locations relative -no-ui-lines
    LANGREL_OPTIONS = -compress -nounfinished -removeidentical
} else {
    TRANSLATION_TARGET_DIR = $${OUT_PWD}/debug/translations
    LANGUPD_OPTIONS =
    LANGREL_OPTIONS = -markuntranslated "MISS_TR "
}

isEmpty(QMAKE_LUPDATE) {
    win32:LANGUPD = $$[QT_INSTALL_BINS]\lupdate.exe
    else:LANGUPD = $$[QT_INSTALL_BINS]/lupdate
}

isEmpty(QMAKE_LRELEASE) {
    win32:LANGREL = $$[QT_INSTALL_BINS]\lrelease.exe
    else:LANGREL = $$[QT_INSTALL_BINS]/lrelease
}

langupd.command = \
    $$LANGUPD $$LANGUPD_OPTIONS $$shell_path($$_PRO_FILE_) -ts $$_PRO_FILE_PWD_/$$TRANSLATIONS

langrel.depends = langupd
langrel.input = TRANSLATIONS
langrel.output = $$TRANSLATION_TARGET_DIR/${QMAKE_FILE_BASE}.qm
langrel.commands = \
    $$LANGREL $$LANGREL_OPTIONS ${QMAKE_FILE_IN} -qm $$TRANSLATION_TARGET_DIR/${QMAKE_FILE_BASE}.qm
langrel.CONFIG += no_link

QMAKE_EXTRA_TARGETS += langupd
QMAKE_EXTRA_COMPILERS += langrel
PRE_TARGETDEPS += langupd compiler_langrel_make_all

There might be a sensful tweak to lupdate options because the various builds (release and debug) generate different *.ts files which then trigger a change in the used VCS.

I also like to guide the tended reader to an example where experts use it.

maxik
  • 1,053
  • 13
  • 34
  • Thanks, while it's not explicitly the answer, it was helpful – mbg033 Jul 27 '16 at 19:47
  • @IlyaKitaev You have to read it clearly: look at the parameter of the `lrelease` call which contains the option `-qm `. This might help in organization. And I do not organize them into any resource file as they are (`*.qm` files) a sort of build product which is created every (release) build. – maxik Jul 28 '16 at 17:53
  • I notice that the `_PRO_FILE_` and the `_PRO_FILE_PWD_` variables are mistyped – Piroxiljin Dec 08 '17 at 10:01
  • @Piroxiljin Thanks, fixed now. – maxik Dec 08 '17 at 10:14
2

The recommended way -- which may not have been available at the time this question was originally asked would be to use

TRANSLATIONS += <your *.ts files>
CONFIG += lrelease embed_translations

If you really need/want to build the qm files separately, I'd point to what qmake does with the above config and adapt it according to your needs. See https://github.com/qt/qtbase/blob/5.15.2/mkspecs/features/lrelease.prf (Basically, it creates and adds a list of resources to RESOURCES).

HVennekate
  • 59
  • 4
  • Thanks for this underated solution. I will just add the following, that generated translation binary are then loaded under the prefix "i18n". Best regards – Romain Cendre May 27 '21 at 15:03