Skip to content

Commit

Permalink
ENH: i18n: Add support for generating translation templates
Browse files Browse the repository at this point in the history
This commit adds GenerateSlicerTranslationTemplates target allowing to
generate "<BaseName>_untranslated.ts" files.

To facilitate their distribution, the files are generated in the directory
"<CMAKE_BINARY_DIR>/TranslationTemplates".
  • Loading branch information
jcfr committed Jun 28, 2019
1 parent e2bc353 commit 99b1120
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions CMake/SlicerMacroTranslation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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}")

Expand Down Expand Up @@ -108,13 +109,15 @@ function(SlicerFunctionAddGenerateSlicerTranslationQMFilesTarget)

# ---------------------------------------------------------------------------------
set(qm_output_dirs)
set(untranslated_ts_files)

# ---------------------------------------------------------------------------------
# UPDATE or ADD translation
# ---------------------------------------------------------------------------------
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)

Expand Down Expand Up @@ -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 "*_<lang>.ts" translation files, and for
# generating the associated "*_<lang>.qm" files.
Expand All @@ -161,13 +171,22 @@ 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()

# ---------------------------------------------------------------------------------
# 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}
)
Expand All @@ -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()

0 comments on commit 99b1120

Please sign in to comment.