#9 Add macro to prepare XML files for xgettext

Merged
SlavekB merged 3 commits from feat/add-prepare-xml into master 5 months ago

+ 206
- 0
modules/TDEL10n.cmake View File

@@ -590,3 +590,209 @@ macro( tde_l10n_preparetips _tips )
590 590
   endif( )
591 591
 
592 592
 endmacro( )
593
+
594
+
595
+#################################################
596
+#####
597
+##### tde_l10n_prepare_xml
598
+#####
599
+##### The function is used to prepare XML file for xgettext.
600
+##### The default settings are identical to extractrc.
601
+#####
602
+
603
+function( tde_l10n_prepare_xml )
604
+
605
+  unset( _source )
606
+  unset( _target )
607
+  unset( _context )
608
+  set( _tags "[tT][eE][xX][tT]|title|string|whatsthis|tooltip|label" )
609
+  set( _preserve "line-wrap" "lines-leading" "spaces-leading" "spaces-trailing" "spaces-multi" )
610
+  set( _no_c_format 1 )
611
+  unset( _directive )
612
+  set( _var _source )
613
+
614
+  foreach( _arg ${ARGN} )
615
+
616
+    # found directive "SOURCE"
617
+    if( "+${_arg}" STREQUAL "+SOURCE" )
618
+      unset( _source )
619
+      set( _var _source )
620
+      set( _directive 1 )
621
+    endif( )
622
+
623
+    # found directive "TARGET"
624
+    if( "+${_arg}" STREQUAL "+TARGET" )
625
+      unset( _target )
626
+      set( _var _target )
627
+      set( _directive 1 )
628
+    endif( )
629
+
630
+    # found directive "CONTEXT"
631
+    if( "+${_arg}" STREQUAL "+CONTEXT" )
632
+      unset( _context )
633
+      set( _var _context )
634
+      set( _directive 1 )
635
+    endif( )
636
+
637
+    # found directive "TAGS"
638
+    if( "+${_arg}" STREQUAL "+TAGS" )
639
+      unset( _tags )
640
+      set( _var _tags )
641
+      set( _directive 1 )
642
+    endif( )
643
+
644
+    # found directive "PRESERVE"
645
+    if( "+${_arg}" STREQUAL "+PRESERVE" )
646
+      unset( _preserve )
647
+      set( _var _preserve )
648
+      set( _directive 1 )
649
+    endif( )
650
+
651
+    # found directive "C_FORMAT"
652
+    if( "+${_arg}" STREQUAL "+C_FORMAT" )
653
+      unset( _no_c_format )
654
+      set( _directive 1 )
655
+    endif( )
656
+
657
+    # found directive "NO_C_FORMAT"
658
+    if( "+${_arg}" STREQUAL "+NO_C_FORMAT" )
659
+      set( _no_c_format 1 )
660
+      set( _directive 1 )
661
+    endif( )
662
+
663
+    # collect data
664
+    if( _directive )
665
+      unset( _directive )
666
+    elseif( _var )
667
+      list( APPEND ${_var} ${_arg} )
668
+    endif( )
669
+
670
+  endforeach( )
671
+
672
+  # verify source
673
+  if( NOT _source )
674
+    tde_message_fatal( "no source XML file" )
675
+  endif( )
676
+  if( NOT "${_source}" MATCHES "^/" )
677
+    set( _source "${CMAKE_CURRENT_SOURCE_DIR}/${_source}" )
678
+  endif( )
679
+  if( NOT _target )
680
+    set( _target "${_source}.tde_l10n" )
681
+  endif( )
682
+  if( NOT "${_target}" MATCHES "^/" )
683
+    set( _target "${CMAKE_CURRENT_SOURCE_DIR}/${_target}" )
684
+  endif( )
685
+
686
+  # prepare tags to regexp
687
+  string( REPLACE ";" "|" _tags "${_tags}" )
688
+
689
+  # read file
690
+  file( READ ${_source} _xml_data )
691
+  string( REGEX REPLACE "[^\n]" "" _xml_len ${_xml_data} )
692
+  string( LENGTH "+${_xml_len}" _xml_len )
693
+
694
+  # process lines
695
+  set( _xml_pos 0 )
696
+  unset( _xml_l10n )
697
+  unset( _xml_inside )
698
+  unset( _xml_tag_empty )
699
+  while( _xml_pos LESS ${_xml_len} )
700
+    # pick line
701
+    string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\1" _xml_line "${_xml_data}" )
702
+    string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\2" _xml_data "${_xml_data}" )
703
+    math( EXPR _xml_pos "${_xml_pos}+1" )
704
+    set( _xml_newline 1 )
705
+
706
+    # process tags on line
707
+    while( _xml_newline OR NOT "${_xml_line}" STREQUAL "" )
708
+      unset( _xml_newline )
709
+      unset( _xml_line_prefix )
710
+      unset( _xml_line_suffix )
711
+      unset( _xml_line_rest )
712
+      if( NOT _xml_inside )
713
+        if( "${_xml_line}" MATCHES "<(${_tags})[^>]*>" )
714
+          set( _xml_inside 1 )
715
+          set( _xml_context "${_context}" )
716
+          string( REGEX MATCH "<(${_tags})[^>]*>(.*)" _xml_line "${_xml_line}" )
717
+          string( REGEX MATCH "^<(${_tags})( [^>]*)+>" _xml_attr "${_xml_line}" )
718
+          if( _xml_attr AND "${_xml_attr}" MATCHES " context=\"([^\"]*)\"" )
719
+            string( REGEX REPLACE ".* context=\"([^\"]*)\".*" "\\1" _xml_context "${_xml_attr}" )
720
+          endif( )
721
+          string( REGEX REPLACE "^<(${_tags})[^>]*>(.*)" "\\2" _xml_line "${_xml_line}" )
722
+          set( _xml_line_prefix "i18n(" )
723
+          if( _no_c_format )
724
+            set( _xml_line_prefix "${_xml_line_prefix}/* xgettext: no-c-format */" )
725
+          endif( )
726
+          if( _xml_context )
727
+            set( _xml_line_prefix "${_xml_line_prefix}\"${_xml_context}\", " )
728
+          endif( )
729
+          set( _xml_tag_empty 1 )
730
+        else( )
731
+          set( _xml_line "" )
732
+        endif( )
733
+      endif( )
734
+
735
+      if( _xml_inside )
736
+        if( "${_xml_line}" MATCHES "</(${_tags})>" )
737
+          unset( _xml_inside )
738
+          string( REGEX REPLACE "</(${_tags})>(.*)" "\\2" _xml_line_rest "${_xml_line}" )
739
+          string( REGEX REPLACE "</(${_tags})>(.*)" "" _xml_line "${_xml_line}" )
740
+          set( _xml_line_suffix ");" )
741
+        endif( )
742
+
743
+        string( REGEX REPLACE "\\\\" "\\\\\\\\" _xml_line "${_xml_line}" )
744
+        string( REGEX REPLACE "\\\"" "\\\\\"" _xml_line "${_xml_line}" )
745
+        string( REGEX REPLACE "\t" "\\\\t" _xml_line "${_xml_line}" )
746
+        if( NOT ";${_preserve};" MATCHES ";entities;" )
747
+          string( REGEX REPLACE "&lt;" "<" _xml_line "${_xml_line}" )
748
+          string( REGEX REPLACE "&gt;" ">" _xml_line "${_xml_line}" )
749
+          string( REGEX REPLACE "&amp;" "&" _xml_line "${_xml_line}" )
750
+        endif( )
751
+        if( NOT ";${_preserve};" MATCHES ";spaces-leading;" )
752
+          string( REGEX REPLACE "^ +" "" _xml_line "${_xml_line}" )
753
+        endif( )
754
+        if( NOT ";${_preserve};" MATCHES ";spaces-trailing;" )
755
+          string( REGEX REPLACE " +$" "" _xml_line "${_xml_line}" )
756
+        endif( )
757
+        if( NOT ";${_preserve};" MATCHES ";spaces-multi;" )
758
+          string( REGEX REPLACE "  +" " " _xml_line "${_xml_line}" )
759
+        endif( )
760
+
761
+        if( _xml_inside )
762
+          if( ";${_preserve};" MATCHES ";line-wrap;" )
763
+            if( ";${_preserve};" MATCHES ";lines-leading;"
764
+                OR NOT "${_xml_line}" STREQUAL "" OR NOT _xml_tag_empty )
765
+              set( _xml_line "${_xml_line}\\n" )
766
+            endif( )
767
+          elseif( NOT "${_xml_line}" STREQUAL "" AND NOT _xml_tag_empty )
768
+            set( _xml_line " ${_xml_line}" )
769
+          endif( )
770
+        endif( )
771
+        if( NOT "${_xml_line}" STREQUAL "" )
772
+          unset( _xml_tag_empty )
773
+        endif( )
774
+      endif( )
775
+
776
+      # drop empty tag on single line
777
+      if( _xml_line_prefix AND _xml_line_suffix AND _xml_tag_empty )
778
+        # skip empty string for translation
779
+
780
+      # add current tag to output
781
+      else( )
782
+        set( _xml_l10n "${_xml_l10n}${_xml_line_prefix}" )
783
+        if( NOT "${_xml_line}" STREQUAL "" OR ( _xml_line_suffix AND _xml_tag_empty ) )
784
+          set( _xml_l10n "${_xml_l10n}\"${_xml_line}\"" )
785
+        endif( )
786
+        set( _xml_l10n "${_xml_l10n}${_xml_line_suffix}" )
787
+      endif( )
788
+
789
+      # take the rest of the line for processing
790
+      set( _xml_line "${_xml_line_rest}" )
791
+    endwhile( )
792
+    set( _xml_l10n "${_xml_l10n}\n" )
793
+  endwhile( )
794
+
795
+  # write file
796
+  file( WRITE ${_target} "${_xml_l10n}" )
797
+
798
+endfunction( )

Loading…
Cancel
Save