How to specify output name for qt5_add_translation?

511 Views Asked by At

I want to generate a plenty *.qm for plenty *.ts files for different languages using qt5_add_translation. All the *.ts files are named using *.de_DE.ts/*.fr_FR.ts/etc convention. But qt5_add_translation produce output, using only basename until first ., not the last one.

There is no possibility to pass options to lrelease using qt5_add_translation(QM_FILES "${PROJECT_NAME}.de_DE.ts" OPTIONS -qm "${PROJECT_NAME}.de_DE.qm") syntax.

Also setting OUTPUT_NAME property for source *.ts file is not working:

set_source_files_properties(
    "${PROJECT_NAME}.de_DE.ts" PROPERTIES
    OUTPUT_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}"
    OUTPUT_NAME "${PROJECT_NAME}.de_DE.qm"
    )

Producing filename in the case is still "${PROJECT_NAME}.qm", not "${PROJECT_NAME}.de_DE.qm"

How to override producing name for resulting *.qm file?

Surely I can make custom command and use it for my purposes, but I prefer to use ready qt5_add_translation.

EDIT:

Looking at /usr/local/Qt-5.9.2/lib/cmake/Qt5LinguistTools/Qt5LinguistToolsMacros.cmake I conclude, that there is no way to achieve desired using ready to use qt5_add_translation, because of using get_filename_component(qm ${_abs_FILE} NAME_WE) to get filename:

NAME_WE = File name without directory or longest extension

For my purposes there is need to use combination of ABSOLUTE (to get filename w/ full suffix), then to apply multiple times EXT in combination with NAME_WE to extract filename w/o shortest extension.

2

There are 2 best solutions below

0
On

I ended up with the below custom function add_translation to replace qt5_add_translation:

function(ADD_TRANSLATION _qm_files)
    foreach(_basename ${ARGN})
    set(qm "${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.qm")
    add_custom_command(
        OUTPUT "${qm}"
        COMMAND "${Qt5_LRELEASE_EXECUTABLE}"
        ARGS -markuntranslated "Not translated!" -nounfinished -removeidentical -compress "${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.ts" -qm "${qm}"
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.ts" VERBATIM
    )
    list(APPEND ${_qm_files} "${qm}")
    endforeach()
    set(${_qm_files} ${${_qm_files}} PARENT_SCOPE)
endfunction()

It accepts basenames of *.ts files and produces list of resulting *.qm files: both in current source directory.

0
On

Please upgrade to Qt 5.9.4 or newer. The handling of .ts files with dots in the name has been fixed there, see also https://bugreports.qt.io/browse/QTBUG-64317 .