Search code examples
qtinternationalizationtranslationqmakeqt-linguist

How to run QT's `lupdate`-tool with a `qmake CONFIG`?


This question has been posted before in the qt community: https://forum.qt.io/topic/106930/how-to-run-lupdate-with-a-qmake-config

I use such a construct in my project files:

LANGUAGES = de
TRANSLATION_NAME = authorization
include(../../gen_translations.pri)

where gen_translations.pri looks like so:

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

TRANSLATIONS = $$prependAll(LANGUAGES, $$PWD/libs/$$TRANSLATION_NAME/translations/lib$${TRANSLATION_NAME}_, .ts)

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
}

for(qmentry, $$list($$TRANSLATIONS_FILES)) {
  qmpath = $$OUT_PWD/../translations
  qmpathname = $$replace(qmpath,/,)
  qmpathname = $$replace(qmpathname,\.,)
  qmpathname = $$replace(qmpathname,:,)
  qmpathname = $$replace(qmpathname," ",)
  qmentity = qmfiles_$${qmpathname}
  eval($${qmentity}.files += $$qmentry)
  eval($${qmentity}.path = $$qmpath)
  INSTALLS *= $${qmentity}
}

It generates the *.qm files for me and moves them to a defined location with make install.

I do not want qmake to execute that whole stuff for each build on my developing machine. Therefore I want to make it conditional by wrapping it for qmake:

translate{
LANGUAGES = de
TRANSLATION_NAME = authorization
include(../../gen_translations.pri)
}

That way I can decide when I want to get *.qm files and when not. But then I am unable to run lupdate on the project file beforehand because it is blocked by that conditional.

I am sure, that someone has a better idea to accomplish the task.

Thanks in advance.


Solution

  • I'm sharing here my recipe for the vmpk project. I've borrowed it from the Arora project (I think). It is much simpler than yours, and I let qmake to decide if it is necessary regenerate any .qm files when the output has been erased or the input .ts has changed, like any other compiler does.

    updateqm.pri

    # update translations
    
    isEmpty(QMAKE_LRELEASE) {
        win32:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]\\lrelease.exe
        else:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
        !exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease }
    }
    
    updateqm.input = TRANSLATIONS
    updateqm.output = $$OUT_PWD/${QMAKE_FILE_BASE}.qm
    updateqm.commands = $$QMAKE_LRELEASE ${QMAKE_FILE_IN} -qm $$OUT_PWD/${QMAKE_FILE_BASE}.qm
    updateqm.CONFIG += no_link target_predeps
    QMAKE_EXTRA_COMPILERS += updateqm
    

    project.pro:

    TRANSLATIONS +=  \
        translations/project_en.ts \
        translations/project_cs.ts \
        translations/project_de.ts \
        translations/project_es.ts \
        translations/project_fr.ts \
        translations/project_ru.ts 
    
    include(updateqm.pri)
    

    With this project file you can do, as always:

    lupdate project.pro
    

    Anyway, Qt5 has a builtin CONFIG+=lrelease option that makes "updateqm.pri" deprecated.