diff --git a/modules/TDEMacros.cmake b/modules/TDEMacros.cmake index 438f97c..9e02263 100644 --- a/modules/TDEMacros.cmake +++ b/modules/TDEMacros.cmake @@ -6,6 +6,9 @@ # (C) 2011-2012 Timothy Pearson # kb9vqf (AT) pearsoncomputing.net # +# (C) 2012-2020 Slávek Banko +# slavek (DOT) banko (AT) axis.cz +# # Improvements and feedback are welcome # # This file is released under GPL >= 2 @@ -1377,6 +1380,204 @@ macro( tde_create_translation ) endmacro( ) +################################################# +##### +##### tde_create_translated_desktop +##### +##### Macro is used to merge translations into desktop file +##### +##### Syntax: +##### tde_create_translated_desktop( +##### [SOURCE] file_name +##### [KEYWORDS keyword [keyword]] +##### [PO_DIR po_directory] +##### [DESTINATION directory] +##### [OUTPUT_NAME file_name] +##### ) + +macro( tde_create_translated_desktop ) + + unset( _src ) + unset( _keywords_add ) + unset( _po_dir ) + unset( _dest ) + unset( _out_name ) + unset( _directive ) + set( _var _src ) + set( _keywords_desktop_default + "Name" "GenericName" "Comment" "Keywords" + "Description" "ExtraNames" "X-TDE-Submenu" ) + + foreach( _arg ${ARGN} ) + + # found directive "SOURCE" + if( "+${_arg}" STREQUAL "+SOURCE" ) + unset( _src ) + set( _var _src ) + set( _directive 1 ) + endif( ) + + # found directive "KEYWORDS" + if( "+${_arg}" STREQUAL "+KEYWORDS" ) + unset( _keywords_add ) + set( _var _keywords_add ) + set( _directive 1 ) + endif( ) + + # found directive "PO_DIR" + if( "+${_arg}" STREQUAL "+PO_DIR" ) + unset( _po_dir ) + set( _var _po_dir ) + set( _directive 1 ) + endif( ) + + # found directive "DESTINATION" + if( "+${_arg}" STREQUAL "+DESTINATION" ) + unset( _dest ) + set( _var _dest ) + set( _directive 1 ) + endif( ) + + # found directive "OUTPUT_NAME" + if( "+${_arg}" STREQUAL "+OUTPUT_NAME" ) + unset( _out_name ) + set( _var _out_name ) + set( _directive 1 ) + endif( ) + + # collect data + if( _directive ) + unset( _directive ) + elseif( _var ) + list( APPEND ${_var} ${_arg} ) + endif() + + endforeach( ) + + # select a tool for merging desktop file translations + if( NOT MSGFMT_EXECUTABLE OR NOT MSGFMT_VERSION ) + tde_setup_msgfmt( ) + endif( ) + if( "${MSGFMT_VERSION}" VERSION_LESS "0.19" ) + if( NOT INTLTOOL_MERGE_EXECUTABLE ) + find_program( INTLTOOL_MERGE_EXECUTABLE + NAMES intltool-merge + HINTS "${TDE_PREFIX}/bin" + ) + if( "${INTLTOOL_MERGE_EXECUTABLE}" STREQUAL "INTLTOOL_MERGE_EXECUTABLE-NOTFOUND" ) + tde_message_fatal( "xgettext >= 0.19 or intltool-merge is required but not found" ) + endif( ) + endif( ) + set( DESKTOP_MERGE_INTLTOOL 1 ) + else( ) + set( DESKTOP_MERGE_MSGFMT 1 ) + endif( ) + + # pick keywords + unset( _keywords_desktop ) + foreach( _keyword ${_keywords_desktop_default} ${_keywords_add} ) + if( "${_keyword}" STREQUAL "-" ) + unset( _keywords_desktop ) + unset( _keyword ) + endif( ) + if( _keyword ) + list( APPEND _keywords_desktop "${_keyword}" ) + endif( ) + endforeach( ) + + # no source file specified! + if( NOT _src ) + tde_message_fatal( "no source desktop file specified" ) + endif( ) + get_filename_component( _basename ${_src} NAME ) + + # if no output name specified, use the same as source + if( NOT _out_name ) + get_filename_component( _out_name ${_src} NAME ) + endif( ) + + # if no po directory specified, try to determine it + if( NOT _po_dir ) + if( EXISTS ${CMAKE_SOURCE_DIR}/translations/desktop_files/${_basename} AND + IS_DIRECTORY ${CMAKE_SOURCE_DIR}/translations/desktop_files/${_basename} ) + set( _po_dir ${CMAKE_SOURCE_DIR}/translations/desktop_files/${_basename} ) + + elseif( EXISTS ${CMAKE_SOURCE_DIR}/po/desktop_files/${_basename} AND + IS_DIRECTORY ${CMAKE_SOURCE_DIR}/po/desktop_files/${_basename} ) + set( _po_dir ${CMAKE_SOURCE_DIR}/po/desktop_files/${_basename} ) + + endif( ) + endif( ) + + # if no destination directory specified, install as application link + if( NOT _dest ) + set( _dest ${XDG_APPS_INSTALL_DIR} ) + endif( ) + + # are there any translations available? + unset( _translations ) + if( EXISTS "${_po_dir}" AND IS_DIRECTORY "${_po_dir}" ) + file( GLOB _translations RELATIVE "${_po_dir}" "${_po_dir}/*.po" ) + endif( ) + if( _translations ) + if( DESKTOP_MERGE_MSGFMT ) + + # create LINGUAS file for msgfmt + string( REPLACE ".po;" "\n" _linguas "${_translations};" ) + file( WRITE ${_po_dir}/LINGUAS "${_linguas}" ) + + # prepare keywords for msgfmt + set( _keywords_arg "--keyword=" ) + foreach( _keyword ${_keywords_desktop} ) + list( APPEND _keywords_arg "--keyword=\"${_keyword}\"" ) + endforeach( ) + + # merge translations + get_filename_component( _src ${_src} ABSOLUTE ) + add_custom_command( + OUTPUT ${_out_name} + COMMAND ${MSGFMT_EXECUTABLE} --desktop --template ${_src} -d ${_po_dir} -o ${_out_name} ${_keywords_arg} + DEPENDS ${_src} + ) + add_custom_target( "${_out_name}-translated" ALL DEPENDS ${_out_name} ) + add_custom_command( + TARGET "${_out_name}-translated" + POST_BUILD COMMAND ${CMAKE_COMMAND} -E remove ${_po_dir}/LINGUAS + ) + install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${_out_name} DESTINATION ${_dest} ) + + else( ) + + # create template for intltool-merge + file( READ ${_src} _src_data ) + string( REPLACE ";" "|" _keywords_match "(${_keywords_desktop})" ) + string( REGEX REPLACE "(^|\n)${_keywords_match}=" "\\1_\\2=" _src_data "${_src_data}" ) + file( WRITE ${CMAKE_CURRENT_BINARY_DIR}/${_out_name}.in "${_src_data}" ) + + # merge translations + add_custom_command( + OUTPUT ${_out_name} + COMMAND ${INTLTOOL_MERGE_EXECUTABLE} -d ${_po_dir} ${_out_name}.in ${_out_name} + DEPENDS ${_src} + ) + add_custom_target( "${_out_name}-translated" ALL DEPENDS ${_out_name} ) + install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${_out_name} DESTINATION ${_dest} ) + + endif( ) + else( ) + + # just install the original file without translations + install( + FILES ${CMAKE_CURRENT_SOURCE_DIR}/${_src} + RENAME ${_out_name} + DESTINATION ${_dest} + ) + + endif( ) + +endmacro( ) + + ################################################# ##### ##### tde_create_handbook @@ -1857,12 +2058,27 @@ macro( tde_setup_msgfmt ) if( GETTEXT_FOUND ) set( MSGFMT_EXECUTABLE ${GETTEXT_MSGFMT_EXECUTABLE} CACHE FILEPATH "path to msgfmt executable" ) + if( GETTEXT_VERSION_STRING ) + set( MSGFMT_VERSION ${GETTEXT_VERSION_STRING} + CACHE STRING "version of msgfmt executable" ) + endif( ) endif( GETTEXT_FOUND ) if( NOT MSGFMT_EXECUTABLE ) tde_message_fatal( "msgfmt is required but was not found on your system." ) endif( NOT MSGFMT_EXECUTABLE ) endif( ) + + if( NOT MSGFMT_VERSION ) + execute_process( + COMMAND ${MSGFMT_EXECUTABLE} --version + OUTPUT_VARIABLE _msgfmt_version + ERROR_VARIABLE _msgfmt_version + ) + string( REGEX REPLACE "^msgfmt[^\n]* ([^ ]*)\n.*" "\\1" _msgfmt_version ${_msgfmt_version} ) + set( MSGFMT_VERSION ${_msgfmt_version} + CACHE STRING "version of msgfmt executable" ) + endif( ) endmacro( )