From 99b11208f93598b67a54968acc9c011481a4e908 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Fri, 28 Jun 2019 09:04:45 -0400 Subject: [PATCH] ENH: i18n: Add support for generating translation templates This commit adds GenerateSlicerTranslationTemplates target allowing to generate "_untranslated.ts" files. To facilitate their distribution, the files are generated in the directory "/TranslationTemplates". --- CMake/SlicerMacroTranslation.cmake | 73 +++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/CMake/SlicerMacroTranslation.cmake b/CMake/SlicerMacroTranslation.cmake index e90f9f02b70..d02c4b76364 100644 --- a/CMake/SlicerMacroTranslation.cmake +++ b/CMake/SlicerMacroTranslation.cmake @@ -67,7 +67,7 @@ function(SlicerMacroTranslation) foreach(language ${MY_TS_LANGUAGES}) set(ts_file "${MY_TS_DIR}/${MY_TS_BASEFILENAME}_${language}.ts") - if(NOT Slicer_UPDATE_TRANSLATION AND NOT EXISTS ${ts_file}) + if(NOT EXISTS ${ts_file}) continue() endif() @@ -79,6 +79,7 @@ function(SlicerMacroTranslation) # --------------------------------------------------------------------------------- set_property(GLOBAL APPEND PROPERTY Slicer_TS_BASEFILENAMES ${MY_TS_BASEFILENAME}) set_property(GLOBAL PROPERTY Slicer_${MY_TS_BASEFILENAME}_FILES_TO_TRANSLATE "${FILES_TO_TRANSLATE}") + set_property(GLOBAL PROPERTY Slicer_${MY_TS_BASEFILENAME}_TS_DIR "${MY_TS_DIR}") set_property(GLOBAL PROPERTY Slicer_${MY_TS_BASEFILENAME}_TS_FILES "${TS_FILES}") set_property(GLOBAL PROPERTY Slicer_${MY_TS_BASEFILENAME}_QM_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}") @@ -108,6 +109,7 @@ function(SlicerFunctionAddGenerateSlicerTranslationQMFilesTarget) # --------------------------------------------------------------------------------- set(qm_output_dirs) + set(untranslated_ts_files) # --------------------------------------------------------------------------------- # UPDATE or ADD translation @@ -115,6 +117,7 @@ function(SlicerFunctionAddGenerateSlicerTranslationQMFilesTarget) get_property(ts_basefilenames GLOBAL PROPERTY Slicer_TS_BASEFILENAMES) foreach(ts_basefilename IN LISTS ts_basefilenames) get_property(files_to_translate GLOBAL PROPERTY Slicer_${ts_basefilename}_FILES_TO_TRANSLATE) + get_property(ts_dir GLOBAL PROPERTY Slicer_${ts_basefilename}_TS_DIR) get_property(ts_files GLOBAL PROPERTY Slicer_${ts_basefilename}_TS_FILES) get_property(qm_output_dir GLOBAL PROPERTY Slicer_${ts_basefilename}_QM_OUTPUT_DIR) @@ -147,8 +150,15 @@ function(SlicerFunctionAddGenerateSlicerTranslationQMFilesTarget) endforeach() endif() - set_source_files_properties(${ts_files} PROPERTIES OUTPUT_LOCATION ${qm_output_dir}) + # Add custom commands for creating or updating "*_untranslated.ts" files. + set(untranslated_ts_dir "${CMAKE_BINARY_DIR}/TranslationTemplates") + set(untranslated_ts_file ${untranslated_ts_dir}/${ts_basefilename}_untranslated.ts) + SLICER_CREATE_TRANSLATION_ONLY( + ${srcs_to_translate} ${rewritten_scripts_to_translate_for_context} ${untranslated_ts_file} + OPTIONS -source-language en_US -no-ui-lines + ) + set_source_files_properties(${ts_files} PROPERTIES OUTPUT_LOCATION ${qm_output_dir}) if(Slicer_UPDATE_TRANSLATION) # Add custom command for creating or updating "*_.ts" translation files, and for # generating the associated "*_.qm" files. @@ -161,6 +171,7 @@ function(SlicerFunctionAddGenerateSlicerTranslationQMFilesTarget) QT5_ADD_TRANSLATION(QM_OUTPUT_FILES ${ts_files}) endif() + list(APPEND untranslated_ts_files ${untranslated_ts_file}) list(APPEND qm_output_dirs ${qm_output_dir}) endforeach() @@ -168,6 +179,14 @@ function(SlicerFunctionAddGenerateSlicerTranslationQMFilesTarget) # Targets # --------------------------------------------------------------------------------- + set(_all) + if(Slicer_UPDATE_TRANSLATION) + set(_all ALL) + endif() + add_custom_target(GenerateSlicerTranslationTemplates ${_all} DEPENDS + ${untranslated_ts_files} + ) + add_custom_target(GenerateSlicerTranslationQMFiles ALL DEPENDS ${QM_OUTPUT_FILES} ) @@ -187,3 +206,53 @@ function(SlicerFunctionAddGenerateSlicerTranslationQMFilesTarget) set(${MY_QM_OUTPUT_DIRS_VAR} ${qm_output_dirs} PARENT_SCOPE) endfunction() + + +function(SLICER_CREATE_TRANSLATION_ONLY) + # Adapted from QT5_CREATE_TRANSLATION: + # - remove call to "qt5_add_translation" + # - remove setting of "_qm_files" output variable + set(options) + set(oneValueArgs) + set(multiValueArgs OPTIONS) + + cmake_parse_arguments(_LUPDATE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + set(_lupdate_files ${_LUPDATE_UNPARSED_ARGUMENTS}) + set(_lupdate_options ${_LUPDATE_OPTIONS}) + + set(_my_sources) + set(_my_tsfiles) + foreach(_file ${_lupdate_files}) + get_filename_component(_ext ${_file} EXT) + get_filename_component(_abs_FILE ${_file} ABSOLUTE) + if(_ext MATCHES "ts") + list(APPEND _my_tsfiles ${_abs_FILE}) + else() + list(APPEND _my_sources ${_abs_FILE}) + endif() + endforeach() + foreach(_ts_file ${_my_tsfiles}) + if(_my_sources) + # make a list file to call lupdate on, so we don't make our commands too + # long for some systems + get_filename_component(_ts_name ${_ts_file} NAME_WE) + set(_ts_lst_file "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lst_file") + set(_lst_file_srcs) + foreach(_lst_file_src ${_my_sources}) + set(_lst_file_srcs "${_lst_file_src}\n${_lst_file_srcs}") + endforeach() + + get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES) + foreach(_pro_include ${_inc_DIRS}) + get_filename_component(_abs_include "${_pro_include}" ABSOLUTE) + set(_lst_file_srcs "-I${_pro_include}\n${_lst_file_srcs}") + endforeach() + + file(WRITE ${_ts_lst_file} "${_lst_file_srcs}") + endif() + add_custom_command(OUTPUT ${_ts_file} + COMMAND ${Qt5_LUPDATE_EXECUTABLE} + ARGS ${_lupdate_options} "@${_ts_lst_file}" -ts ${_ts_file} + DEPENDS ${_my_sources} ${_ts_lst_file} VERBATIM) + endforeach() +endfunction()