#9 Add macro to prepare XML files for xgettext

Merged
SlavekB merged 3 commits from feat/add-prepare-xml into master 10 months ago
  1. +206
    -0
      modules/TDEL10n.cmake

+ 206
- 0
modules/TDEL10n.cmake View File

@@ -590,3 +590,209 @@ macro( tde_l10n_preparetips _tips )
endif( )

endmacro( )


#################################################
#####
##### tde_l10n_prepare_xml
#####
##### The function is used to prepare XML file for xgettext.
##### The default settings are identical to extractrc.
#####

function( tde_l10n_prepare_xml )

unset( _source )
unset( _target )
unset( _context )
set( _tags "[tT][eE][xX][tT]|title|string|whatsthis|tooltip|label" )
set( _preserve "line-wrap" "lines-leading" "spaces-leading" "spaces-trailing" "spaces-multi" )
set( _no_c_format 1 )
unset( _directive )
set( _var _source )

foreach( _arg ${ARGN} )

# found directive "SOURCE"
if( "+${_arg}" STREQUAL "+SOURCE" )
unset( _source )
set( _var _source )
set( _directive 1 )
endif( )

# found directive "TARGET"
if( "+${_arg}" STREQUAL "+TARGET" )
unset( _target )
set( _var _target )
set( _directive 1 )
endif( )

# found directive "CONTEXT"
if( "+${_arg}" STREQUAL "+CONTEXT" )
unset( _context )
set( _var _context )
set( _directive 1 )
endif( )

# found directive "TAGS"
if( "+${_arg}" STREQUAL "+TAGS" )
unset( _tags )
set( _var _tags )
set( _directive 1 )
endif( )

# found directive "PRESERVE"
if( "+${_arg}" STREQUAL "+PRESERVE" )
unset( _preserve )
set( _var _preserve )
set( _directive 1 )
endif( )

# found directive "C_FORMAT"
if( "+${_arg}" STREQUAL "+C_FORMAT" )
unset( _no_c_format )
set( _directive 1 )
endif( )

# found directive "NO_C_FORMAT"
if( "+${_arg}" STREQUAL "+NO_C_FORMAT" )
set( _no_c_format 1 )
set( _directive 1 )
endif( )

# collect data
if( _directive )
unset( _directive )
elseif( _var )
list( APPEND ${_var} ${_arg} )
endif( )

endforeach( )

# verify source
if( NOT _source )
tde_message_fatal( "no source XML file" )
endif( )
if( NOT "${_source}" MATCHES "^/" )
set( _source "${CMAKE_CURRENT_SOURCE_DIR}/${_source}" )
endif( )
if( NOT _target )
set( _target "${_source}.tde_l10n" )
endif( )
if( NOT "${_target}" MATCHES "^/" )
set( _target "${CMAKE_CURRENT_SOURCE_DIR}/${_target}" )
endif( )

# prepare tags to regexp
string( REPLACE ";" "|" _tags "${_tags}" )

# read file
file( READ ${_source} _xml_data )
string( REGEX REPLACE "[^\n]" "" _xml_len ${_xml_data} )
string( LENGTH "+${_xml_len}" _xml_len )

# process lines
set( _xml_pos 0 )
unset( _xml_l10n )
unset( _xml_inside )
unset( _xml_tag_empty )
while( _xml_pos LESS ${_xml_len} )
# pick line
string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\1" _xml_line "${_xml_data}" )
string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\2" _xml_data "${_xml_data}" )
math( EXPR _xml_pos "${_xml_pos}+1" )
set( _xml_newline 1 )

# process tags on line
while( _xml_newline OR NOT "${_xml_line}" STREQUAL "" )
unset( _xml_newline )
unset( _xml_line_prefix )
unset( _xml_line_suffix )
unset( _xml_line_rest )
if( NOT _xml_inside )
if( "${_xml_line}" MATCHES "<(${_tags})[^>]*>" )
set( _xml_inside 1 )
set( _xml_context "${_context}" )
string( REGEX MATCH "<(${_tags})[^>]*>(.*)" _xml_line "${_xml_line}" )
string( REGEX MATCH "^<(${_tags})( [^>]*)+>" _xml_attr "${_xml_line}" )
if( _xml_attr AND "${_xml_attr}" MATCHES " context=\"([^\"]*)\"" )
string( REGEX REPLACE ".* context=\"([^\"]*)\".*" "\\1" _xml_context "${_xml_attr}" )
endif( )
string( REGEX REPLACE "^<(${_tags})[^>]*>(.*)" "\\2" _xml_line "${_xml_line}" )
set( _xml_line_prefix "i18n(" )
if( _no_c_format )
set( _xml_line_prefix "${_xml_line_prefix}/* xgettext: no-c-format */" )
endif( )
if( _xml_context )
set( _xml_line_prefix "${_xml_line_prefix}\"${_xml_context}\", " )
endif( )
set( _xml_tag_empty 1 )
else( )
set( _xml_line "" )
endif( )
endif( )

if( _xml_inside )
if( "${_xml_line}" MATCHES "</(${_tags})>" )
unset( _xml_inside )
string( REGEX REPLACE "</(${_tags})>(.*)" "\\2" _xml_line_rest "${_xml_line}" )
string( REGEX REPLACE "</(${_tags})>(.*)" "" _xml_line "${_xml_line}" )
set( _xml_line_suffix ");" )
endif( )

string( REGEX REPLACE "\\\\" "\\\\\\\\" _xml_line "${_xml_line}" )
string( REGEX REPLACE "\\\"" "\\\\\"" _xml_line "${_xml_line}" )
string( REGEX REPLACE "\t" "\\\\t" _xml_line "${_xml_line}" )
if( NOT ";${_preserve};" MATCHES ";entities;" )
string( REGEX REPLACE "&lt;" "<" _xml_line "${_xml_line}" )
string( REGEX REPLACE "&gt;" ">" _xml_line "${_xml_line}" )
string( REGEX REPLACE "&amp;" "&" _xml_line "${_xml_line}" )
endif( )
if( NOT ";${_preserve};" MATCHES ";spaces-leading;" )
string( REGEX REPLACE "^ +" "" _xml_line "${_xml_line}" )
endif( )
if( NOT ";${_preserve};" MATCHES ";spaces-trailing;" )
string( REGEX REPLACE " +$" "" _xml_line "${_xml_line}" )
endif( )
if( NOT ";${_preserve};" MATCHES ";spaces-multi;" )
string( REGEX REPLACE " +" " " _xml_line "${_xml_line}" )
endif( )

if( _xml_inside )
if( ";${_preserve};" MATCHES ";line-wrap;" )
if( ";${_preserve};" MATCHES ";lines-leading;"
OR NOT "${_xml_line}" STREQUAL "" OR NOT _xml_tag_empty )
set( _xml_line "${_xml_line}\\n" )
endif( )
elseif( NOT "${_xml_line}" STREQUAL "" AND NOT _xml_tag_empty )
set( _xml_line " ${_xml_line}" )
endif( )
endif( )
if( NOT "${_xml_line}" STREQUAL "" )
unset( _xml_tag_empty )
endif( )
endif( )

# drop empty tag on single line
if( _xml_line_prefix AND _xml_line_suffix AND _xml_tag_empty )
# skip empty string for translation

# add current tag to output
else( )
set( _xml_l10n "${_xml_l10n}${_xml_line_prefix}" )
if( NOT "${_xml_line}" STREQUAL "" OR ( _xml_line_suffix AND _xml_tag_empty ) )
set( _xml_l10n "${_xml_l10n}\"${_xml_line}\"" )
endif( )
set( _xml_l10n "${_xml_l10n}${_xml_line_suffix}" )
endif( )

# take the rest of the line for processing
set( _xml_line "${_xml_line_rest}" )
endwhile( )
set( _xml_l10n "${_xml_l10n}\n" )
endwhile( )

# write file
file( WRITE ${_target} "${_xml_l10n}" )

endfunction( )

Loading…
Cancel
Save