1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
#################################################
#
# (C) 2018 Slávek Banko
# slavek (DOT) banko (AT) axis.cz
#
# Improvements and feedback are welcome
#
# This file is released under GPL >= 2
#
#################################################
##### include essential TDE macros ##############
set( MASTER_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
include( TDEMacros )
##### verify required programs ##################
if( NOT DEFINED TDE_PREFIX AND IS_DIRECTORY /opt/trinity )
set( TDE_PREFIX "/opt/trinity" )
else( )
set( TDE_PREFIX "/usr" )
endif( )
if( NOT DEFINED KDE_XGETTEXT_EXECUTABLE )
find_program( KDE_XGETTEXT_EXECUTABLE
NAMES kde-xgettext
HINTS "${TDE_PREFIX}/bin"
)
if( NOT KDE_XGETTEXT_EXECUTABLE )
tde_message_fatal( "kde-xgettext is required but not found" )
endif( )
endif( )
if( NOT DEFINED EXTRACTRC_EXECUTABLE )
find_program( EXTRACTRC_EXECUTABLE
NAMES extractrc
HINTS "${TDE_PREFIX}/bin"
)
if( NOT EXTRACTRC_EXECUTABLE )
tde_message_fatal( "extractrc is required but not found" )
endif( )
endif( )
if( NOT DEFINED EXTRACTATTR_EXECUTABLE )
find_program( EXTRACTATTR_EXECUTABLE
NAMES extractattr
HINTS "${TDE_PREFIX}/bin"
)
if( NOT EXTRACTATTR_EXECUTABLE )
tde_message_fatal( "extractattr is required but not found" )
endif( )
endif( )
if( NOT DEFINED XGETTEXT_EXECUTABLE )
find_program( XGETTEXT_EXECUTABLE
NAMES xgettext
HINTS "${TDE_PREFIX}/bin"
)
if( NOT XGETTEXT_EXECUTABLE )
tde_message_fatal( "xgettext is required but not found" )
endif( )
execute_process(
COMMAND ${XGETTEXT_EXECUTABLE} --version
OUTPUT_VARIABLE _xgettext_version
ERROR_VARIABLE _xgettext_version
)
string( REGEX REPLACE "^xgettext[^\n]* ([^ ]*)\n.*" "\\1" _xgettext_version ${_xgettext_version} )
if( "${_xgettext_version}" VERSION_LESS "0.19" )
tde_message_fatal( "xgettext version >= 0.19 is required but found only ${_xgettext_version}" )
endif( )
endif( )
if( NOT DEFINED MSGUNIQ_EXECUTABLE )
find_program( MSGUNIQ_EXECUTABLE
NAMES msguniq
HINTS "${TDE_PREFIX}/bin"
)
if( NOT MSGUNIQ_EXECUTABLE )
tde_message_fatal( "msguniq is required but not found" )
endif( )
endif( )
if( NOT DEFINED MSGCAT_EXECUTABLE )
find_program( MSGCAT_EXECUTABLE
NAMES msgcat
HINTS "${TDE_PREFIX}/bin"
)
if( NOT MSGCAT_EXECUTABLE )
tde_message_fatal( "msgcat is required but not found" )
endif( )
endif( )
#################################################
#####
##### tde_l10n_add_subdirectory
#####
##### The function simulates the add_subdirectory() behavior, but
##### the CMakeL10n.txt file is used instead of CMakeLists.txt.
#####
function( tde_l10n_add_subdirectory _dir )
set( CMAKE_CURRENT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${_dir}" )
set( CMAKE_CURRENT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_dir}" )
include( ${CMAKE_CURRENT_SOURCE_DIR}/CMakeL10n.txt )
endfunction( )
#################################################
#####
##### tde_l10n_auto_add_subdirectories
#####
##### The function is equivalent to tde_auto_add_subdirectories, but
##### the CMakeL10n.txt file is used instead of CMakeLists.txt.
#####
function( tde_l10n_auto_add_subdirectories )
file( GLOB _dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/*" )
foreach( _dir ${_dirs} )
if( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${_dir}
AND NOT ${_dir} STREQUAL ".svn"
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_dir}/CMakeL10n.txt )
tde_l10n_add_subdirectory( ${_dir} )
endif( )
endforeach( )
endfunction( )
#################################################
#####
##### tde_l10n_create_template
#####
##### Macro is used to generate a translation template - POT file.
#####
##### Syntax:
##### tde_l10n_create_template(
##### [CATALOG] file_name
##### [SOURCES source_spec [source_spec]]
##### [EXCLUDES regex [regex]]
##### [KEYWORDS keyword [keyword]]
##### [ATTRIBUTES attrib_spec [attrib_spec]]
##### [DESTINATION directory]
##### )
#####
##### Where:
##### CATALOG determines the target file name (without pot suffix).
##### SOURCES can be specified by several options:
##### a) Do not specify anything
##### - all usual source files will be automatically searched.
##### b) Enter the directory name - for example, '.' or 'src'
##### - all the usual source files in the specified directory
##### and subdirectories will be searched.
##### c) Enter the mask - for example '*.cpp'
##### - all files with the specified mask will be searched.
##### d) Specify the name of the individual file.
##### The methods from b) to d) can be combined.
##### EXCLUDES determines which files are to be excluded from processing
##### KEYWORDS determines additional keywords for xgettext.
##### ATTRIBUTES determines files and specification for extractattr:
##### source_spec:element,attribute[,context]
##### DESTINATION determines directory to save translation template.
##### The destination directory is determined as follows:
##### a) Directory is specified as an argument.
##### b) The variable POT_SOURCE_DIR is set.
##### c) There is a 'translations' directory.
##### d) There is a 'po' directory.
#####
##### Note:
##### Editing the _files list inside foreach( ${_files} ) below in the
##### code is safe, because in CMake foreach parameters are evaluated
##### before the loop starts. Therefore, the changes in the list inside
##### the loop do not have an unwanted impact on the loop processing.
#####
macro( tde_l10n_create_template )
unset( _catalog )
unset( _sources )
unset( _excludes )
unset( _files )
unset( _rcs )
unset( _desktops )
unset( _pots )
unset( _dest )
unset( _keywords )
unset( _attributes )
unset( _pot )
unset( _directive )
set( _var _catalog )
foreach( _arg ${ARGN} )
# found directive "CATALOG"
if( "+${_arg}" STREQUAL "+CATALOG" )
unset( _catalog )
set( _var _catalog )
set( _directive 1 )
endif( )
# found directive "SOURCES"
if( "+${_arg}" STREQUAL "+SOURCES" )
unset( _sources )
set( _var _sources )
set( _directive 1 )
endif( )
# found directive "EXCLUDES"
if( "+${_arg}" STREQUAL "+EXCLUDES" )
unset( _excludes )
set( _var _excludes )
set( _directive 1 )
endif( )
# found directive "DESTINATION"
if( "+${_arg}" STREQUAL "+DESTINATION" )
unset( _dest )
set( _var _dest )
set( _directive 1 )
endif( )
# found directive "KEYWORDS"
if( "+${_arg}" STREQUAL "+KEYWORDS" )
unset( _keywords )
set( _var _keywords )
set( _directive 1 )
endif( )
# found directive "ATTRIBUTES"
if( "+${_arg}" STREQUAL "+ATTRIBUTES" )
unset( _attributes )
set( _var _attributes )
set( _directive 1 )
endif( )
# collect data
if( _directive )
unset( _directive )
elseif( _var )
list( APPEND ${_var} ${_arg} )
endif( )
endforeach( )
# verify catalog
if( NOT _catalog )
tde_message_fatal( "the name of the translation catalog is not defined" )
endif( )
# determine the destination directory
if( NOT _dest )
if( POT_SOURCE_DIR )
set( _dest ${POT_SOURCE_DIR} )
elseif( EXISTS "${MASTER_SOURCE_DIR}/translations" )
set( _dest "${MASTER_SOURCE_DIR}/translations/" )
elseif( EXISTS "${MASTER_SOURCE_DIR}/po" )
set( _dest "${MASTER_SOURCE_DIR}/po/" )
else( )
tde_message_fatal( "cannot determine destination directory" )
endif( )
endif( )
if( ${_dest} MATCHES "[^/]$" )
set( _dest "${_dest}/" )
endif( )
if( ${_dest} MATCHES "^[^/]" )
set( _dest "${CMAKE_CURRENT_SOURCE_DIR}/${_dest}" )
endif( )
get_filename_component( _potFilename "${_dest}${_catalog}.pot" ABSOLUTE )
file( RELATIVE_PATH _potFilename ${CMAKE_SOURCE_DIR} ${_potFilename} )
message( STATUS "Create translation template ${_potFilename}" )
# verify sources
if( NOT _sources AND NOT _attributes )
# add current directory
list( APPEND _sources "." )
endif( )
foreach( _src ${_sources} )
# add all source files from directory
if( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${_src} )
if( ${_src} STREQUAL "." )
set( _dir "${CMAKE_CURRENT_SOURCE_DIR}" )
else( )
set( _dir "${CMAKE_CURRENT_SOURCE_DIR}/${_src}" )
endif( )
file( GLOB_RECURSE _add_files
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${_dir}/*.c
${_dir}/*.cc
${_dir}/*.cpp
${_dir}/*.h
${_dir}/*.kcfg
${_dir}/*.rc
${_dir}/*.ui
)
list( SORT _add_files )
list( APPEND _files ${_add_files} )
# add files by the specified mask
elseif( ${_src} MATCHES "(\\*|\\?)" )
file( GLOB_RECURSE _add_files
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/${_src}
)
list( SORT _add_files )
list( APPEND _files ${_add_files} )
# add a individual file
elseif( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_src} )
list( APPEND _files ${_src} )
endif( )
endforeach( )
# add common translator info
if( _files )
set( _translatorinfo
"i18n(\"NAME OF TRANSLATORS\", \"Your names\")\n"
"i18n(\"EMAIL OF TRANSLATORS\", \"Your emails\")\n"
)
file( WRITE ${CMAKE_CURRENT_SOURCE_DIR}/_translatorinfo.tde_l10n ${_translatorinfo} )
list( INSERT _files 0 "_translatorinfo.tde_l10n" )
endif( )
# filter files by excludes
if( _excludes )
foreach( _src ${_files} )
foreach( _exclude ${_excludes} )
if( ${_src} MATCHES ${_exclude} )
list( REMOVE_ITEM _files ${_src} )
endif( )
endforeach( )
endforeach( )
endif( )
if( NOT _files AND NOT _attributes )
tde_message_fatal( "no source files found" )
endif( )
# prepare additional keywords
unset( _add_keywords )
foreach( _keyword ${_keywords} )
list( APPEND _add_keywords "-k${_keyword}" )
endforeach( )
# pick resource files *.kcfg, *.rc and *.ui
foreach( _src ${_files} )
if( ${_src} MATCHES "\\.(kcfg|rc|ui)(\\.cmake)?(\\.tde_l10n)?$" )
list( APPEND _rcs ${_src} )
list( REMOVE_ITEM _files ${_src} )
endif( )
endforeach( )
# prepare extracted-rc.tde_l10n
if( _rcs OR _attributes )
file( WRITE ${CMAKE_CURRENT_SOURCE_DIR}/extracted-rc.tde_l10n "" )
list( APPEND _files extracted-rc.tde_l10n )
endif( )
# process resource files
if( _rcs )
execute_process(
COMMAND ${EXTRACTRC_EXECUTABLE} ${_rcs}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _sources_rc
)
file( APPEND ${CMAKE_CURRENT_SOURCE_DIR}/extracted-rc.tde_l10n "${_sources_rc}" )
endif( )
# extract attributes
if( _attributes )
foreach( _attrib ${_attributes} )
if( ${_attrib} MATCHES "^([^:]+):(.+)$" )
string( REGEX REPLACE "^([^:]+):(.+)$" "\\1" _attrib_glob ${_attrib} )
string( REGEX REPLACE "^([^:]+):(.+)$" "\\2" _attrib_spec ${_attrib} )
file( GLOB_RECURSE _attrib_files
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/${_attrib_glob}
)
if( _attrib_files )
list( SORT _attrib_files )
execute_process(
COMMAND ${EXTRACTATTR_EXECUTABLE}
--attr=${_attrib_spec} ${_attrib_files}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _attrib_rc
)
file( APPEND ${CMAKE_CURRENT_SOURCE_DIR}/extracted-rc.tde_l10n "${_attrib_rc}" )
endif( )
endif( )
endforeach( )
endif( )
# prepare tips
foreach( _src ${_files} )
if( ${_src} MATCHES "(^|/)tips$" )
tde_l10n_preparetips( ${_src} )
list( REMOVE_ITEM _files ${_src} )
list( APPEND _files "${_src}.tde_l10n" )
endif( )
endforeach( )
# pick desktop files *.desktop and *.protocol
foreach( _src ${_files} )
if( ${_src} MATCHES "\\.(desktop|protocol)(\\.cmake)?(\\.tde_l10n)?$" )
list( APPEND _desktops ${_src} )
list( REMOVE_ITEM _files ${_src} )
endif( )
endforeach( )
# pick pot files *.pot
foreach( _src ${_files} )
if( ${_src} MATCHES "\\.pot(\\.cmake)?(\\.tde_l10n)?$" )
list( APPEND _pots ${_src} )
list( REMOVE_ITEM _files ${_src} )
endif( )
endforeach( )
# create translation template
if( _files )
execute_process(
COMMAND ${KDE_XGETTEXT_EXECUTABLE} --foreign-user -C
-ki18n -ki18n:1,2 -ktr2i18n -ktr2i18n:1,2
-kI18N_NOOP -kI18N_NOOP2 ${_add_keywords}
-o - ${_files}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _pot
)
# set charset and encoding headers
if( _pot )
string( REPLACE "Content-Type: text/plain; charset=CHARSET" "Content-Type: text/plain; charset=UTF-8" _pot "${_pot}" )
string( REPLACE "Content-Transfer-Encoding: ENCODING" "Content-Transfer-Encoding: 8bit" _pot "${_pot}" )
endif( )
endif( )
# process desktop files
if( _desktops )
# create translation template for desktop files
if( _pot )
set( _withPotHeader "--omit-header" )
else( )
set( _withPotHeader "--foreign-user" )
endif( )
execute_process(
COMMAND ${XGETTEXT_EXECUTABLE} ${_withPotHeader} -L Desktop
-kDescription -kExtraNames -kX-TDE-Submenu ${_add_keywords}
-o - ${_desktops}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _potDesktop
)
# merge translation templates
if( _potDesktop )
if( _pot )
file( WRITE ${CMAKE_CURRENT_SOURCE_DIR}/extracted-pot.tmp "${_pot}" )
file( APPEND ${CMAKE_CURRENT_SOURCE_DIR}/extracted-pot.tmp "${_potDesktop}" )
execute_process(
COMMAND ${MSGUNIQ_EXECUTABLE} --use-first
INPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/extracted-pot.tmp
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _pot
)
file( REMOVE ${CMAKE_CURRENT_SOURCE_DIR}/extracted-pot.tmp )
else( )
set( _pot "${_potDesktop}" )
# set charset and encoding headers
string( REPLACE "Content-Type: text/plain; charset=CHARSET" "Content-Type: text/plain; charset=UTF-8" _pot "${_pot}" )
string( REPLACE "Content-Transfer-Encoding: ENCODING" "Content-Transfer-Encoding: 8bit" _pot "${_pot}" )
endif( )
endif( )
endif( )
# merge additional pot files
if( _pots )
file( WRITE ${CMAKE_CURRENT_SOURCE_DIR}/extracted-pot.tmp "${_pot}" )
execute_process(
COMMAND ${MSGCAT_EXECUTABLE} --use-first extracted-pot.tmp ${_pots}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _pot
)
file( REMOVE ${CMAKE_CURRENT_SOURCE_DIR}/extracted-pot.tmp )
# set charset and encoding headers
string( REPLACE "Content-Type: text/plain; charset=CHARSET" "Content-Type: text/plain; charset=UTF-8" _pot "${_pot}" )
string( REPLACE "Content-Transfer-Encoding: ENCODING" "Content-Transfer-Encoding: 8bit" _pot "${_pot}" )
endif( )
# finalize translation template
if( _pot )
# update references for resources to original files and line numbers
if( _rcs OR _attributes )
file( READ "${CMAKE_CURRENT_SOURCE_DIR}/extracted-rc.tde_l10n" _extractedRC )
string( REGEX REPLACE "[^\n]" "" _extractedRC_len "${_extractedRC}" )
string( LENGTH "+${_extractedRC_len}" _extractedRC_len )
set( _rcPos 0 )
while( _rcPos LESS ${_extractedRC_len} )
string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\1" _rcLine "${_extractedRC}" )
string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\2" _extractedRC "${_extractedRC}" )
math( EXPR _rcPos "${_rcPos}+1" )
if( "${_rcLine}" MATCHES "^//i18n: file .* line [0-9]*$" )
string( REGEX REPLACE "^//i18n: file (.*) line ([0-9]*)$" "\\1:\\2" _rcOrig ${_rcLine} )
endif( )
if( "${_rcLine}" MATCHES "^i18n\\(" AND _rcOrig )
string( REGEX REPLACE "(^|\n)(#:.*) extracted-rc.tde_l10n:${_rcPos}( |\n)" "\\1\\2 ${_rcOrig}\\3" _pot "${_pot}" )
unset( _rcOrig )
endif( )
endwhile( )
endif( )
# update references for modified source files (".tde_l10n" extension)
string( REGEX REPLACE "\\.tde_l10n(:[0-9]+)" "\\1" _pot "${_pot}" )
# save translation template
if( EXISTS "${_dest}${_catalog}.pot" )
file( READ "${_dest}${_catalog}.pot" _potOrig )
else( )
unset( _potOrig )
endif( )
if( _potOrig )
string( REGEX REPLACE "\n\"POT-Creation-Date: [^\"]*\"\n" "" _potOrig "${_potOrig}" )
string( REGEX REPLACE "\n\"POT-Creation-Date: [^\"]*\"\n" "" _potNew "${_pot}" )
endif( )
if( NOT _potOrig OR NOT "${_potNew}" STREQUAL "${_potOrig}" )
file( WRITE "${_dest}${_catalog}.pot" "${_pot}" )
endif( )
endif( _pot )
# cleanup
foreach( _file ${_files} ${_rcs} ${_desktops} )
if( "${_file}" MATCHES "\\.tde_l10n$" )
file( REMOVE ${CMAKE_CURRENT_SOURCE_DIR}/${_file} )
endif( )
endforeach( )
endmacro( )
#################################################
#####
##### tde_l10n_preparetips
#####
##### Macro is used to prepare tips file for xgettext
#####
macro( tde_l10n_preparetips _tips )
if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_tips} )
file( READ ${CMAKE_CURRENT_SOURCE_DIR}/${_tips} _tips_html )
string( REGEX REPLACE "[^\n]" "" _tips_len ${_tips_html} )
string( LENGTH "+${_tips_len}" _tips_len )
unset( _tips_inside )
unset( _tips_l10n )
set( _tips_pos 0 )
while( _tips_pos LESS ${_tips_len} )
string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\1" _tips_line "${_tips_html}" )
string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\2" _tips_html "${_tips_html}" )
math( EXPR _tips_pos "${_tips_pos}+1" )
string( REGEX REPLACE "\\\\" "\\\\\\\\" _tips_line "${_tips_line}" )
string( REGEX REPLACE "\\\"" "\\\\\"" _tips_line "${_tips_line}" )
if( NOT _tips_inside )
if( "${_tips_line}" MATCHES "<html>" )
set( _tips_inside 1 )
string( REGEX REPLACE ".*<html>" "" _tips_line "${_tips_line}" )
string( REGEX REPLACE "(.+)" "\"\\1\\\\n\"" _tips_line "${_tips_line}" )
set( _tips_line "i18n(${_tips_line}" )
else( )
set( _tips_line "" )
endif( )
else( )
if( "${_tips_line}" MATCHES "</html>" )
unset( _tips_inside )
string( REGEX REPLACE "</html>.*" "" _tips_line "${_tips_line}" )
string( REGEX REPLACE "(.+)" "\"\\1\\\\n\"" _tips_line "${_tips_line}" )
set( _tips_line "${_tips_line});" )
else( )
set( _tips_line "\"${_tips_line}\\n\"" )
endif( )
endif( )
set( _tips_l10n "${_tips_l10n}${_tips_line}\n" )
endwhile( )
file( WRITE ${CMAKE_CURRENT_SOURCE_DIR}/${_tips}.tde_l10n "${_tips_l10n}" )
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 "<" "<" _xml_line "${_xml_line}" )
string( REGEX REPLACE ">" ">" _xml_line "${_xml_line}" )
string( REGEX REPLACE "&" "&" _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 )
set( _xml_l10n "${_xml_l10n}" )
# add current tag to output
else( )
set( _xml_l10n "${_xml_l10n}${_xml_line_prefix}" )
if( _xml_line 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( )
|