cmlib/CMLibQtTranslation.cmake

61 lines
2.0 KiB
CMake
Raw Normal View History

2019-02-12 16:31:17 +00:00
function(qt_translation)
find_package(Qt5 COMPONENTS LinguistTools REQUIRED)
set(options)
set(oneValueArgs TARGET TS_DIR)
set(multiValueArgs LANGUAGES)
cmake_parse_arguments(_QTTR "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(_target ${_QTTR_TARGET})
set(_ts_dir ${_QTTR_TS_DIR})
set(_languages ${_QTTR_LANGUAGES})
get_target_property(_sources ${_target} SOURCES)
get_target_property(_source_dir ${_target} SOURCE_DIR)
set(L10N_QRC_BODY "")
make_directory(${_ts_dir})
set(_sources_list "")
foreach(_s ${_sources})
list(APPEND _sources_list ${_source_dir}/${_s})
endforeach()
foreach(_lang ${_languages})
set(_ts "${_target}_${_lang}.ts")
set(_qm "${_target}_${_lang}.qm")
list(APPEND _ts_list ${_ts_dir}/${_ts})
if(NOT EXISTS ${_ts_dir}/${_ts})
file(WRITE ${_ts_dir}/${_ts} "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!DOCTYPE TS>
<TS version=\"2.1\" language=\"${_lang}\"></TS>")
endif()
2019-02-16 10:10:12 +00:00
string(APPEND L10N_QRC_BODY
"<file alias=\"${_qm}\">${CMAKE_BINARY_DIR}/${_qm}</file>\n")
2019-02-12 16:31:17 +00:00
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${_qm}
2019-02-16 10:10:12 +00:00
COMMAND ${Qt5_LRELEASE_EXECUTABLE}
ARGS ${_ts_dir}/${_ts} -qm
${CMAKE_BINARY_DIR}/${_qm}
DEPENDS ${_ts_dir}/${_ts})
2019-02-12 16:31:17 +00:00
endforeach()
add_custom_command(OUTPUT ${_ts_list}
2019-02-16 10:10:12 +00:00
COMMAND ${Qt5_LUPDATE_EXECUTABLE}
ARGS ${_sources_list} -ts ${_ts_list}
DEPENDS ${_sources_list})
2019-02-12 16:31:17 +00:00
2019-02-16 10:10:12 +00:00
configure_file(${CMLIB_MODULE_DIR}/qrc/l10n.qrc.in
${CMAKE_BINARY_DIR}/${_target}_l10n.qrc)
2019-02-12 16:31:17 +00:00
qt5_add_resources(_qrc ${CMAKE_BINARY_DIR}/${_target}_l10n.qrc)
add_custom_target(_qrc_target DEPENDS ${_qrc})
foreach(_lang ${_languages})
set(_ts "${_target}_${_lang}.ts")
set(_qm "${_target}_${_lang}.qm")
endforeach()
add_dependencies(${_target} _qrc_target)
target_sources(${_target} PUBLIC ${_qrc})
endfunction()