summaryrefslogtreecommitdiffstats
path: root/kiosktool/extractxml
blob: 629ab1da00240b7d7c1c1cecc1a924cdaaa87ff5 (plain)
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
#! /usr/bin/perl
#
# This script extracts messages from kiosk_data.xml
# and writes on standard output (usually redirected to rc.cpp)
# the equivalent i18n() calls so that xgettext can parse them.
#
# It is based on extractrc but differs in the following ways:
# *) Extracts <caption> and <description> tags
# *) Performs equivalent of QString::simplifyWhiteSpace on all strings
#
# known flags:
# --tag=name : extract also the tag name
# --context=name : give all i18n calls a context name: i18n( "name",...)

$filename = "";
@filenames = ();

sub writeoutstring
{
   print STDOUT "i18n(\"";
   if (@_[0])
   {
      # We have a I18N context
      print STDOUT @_[0];
      print STDOUT "\",\"";
   }
   print STDOUT @_[1];
   print STDOUT "\"); // $filename \n";
}

$extratags = "";
$context = ""; # I18N context

ARGUMENTS: while (defined ($ARGV[0]))
{
   $_ = shift;

   if (/^--tag=(\w+)/) # --tag=name
   {
        $extratags .= "|" . $1;
        next ARGUMENTS;
   }
   elsif (/^--context=(\w+)/) # --context=name
   {
        $context = $1;
        next ARGUMENTS;
   }

   $filename = $_; # maybe check for more options

if (! $filename) {
   print STDERR "no file to open\n";
   exit 1;
}

$string = "";
$intext = 0;
$linenr = 0;
$inskippedprop = 0;

open(FILE, $filename);

READING: while ( <FILE> ) {
   $linenr++;
   if ($linenr == 1 && ($_ !~ /^<!DOCTYPE/) && ($_ !~ /^<\?xml/)) {
   	last READING;
   }

   $string .= "\\n" . $_;
   chomp($string);

   $textstring = '(caption|description' . $extratags .')>';

   # The 'database' property contains strings that shouldn't be translated
   if ($inskippedprop == 0 && ($string =~ /<property name=\"database\"/ || $string=~ /<property name=\"associations\"/)) {
     $inskippedprop = 1;
   } elsif ($inskippedprop == 1 && ($string =~ /<\/property/)) {
     $inskippedprop = 0;
     $string = "";
   }

   if ($inskippedprop == 0 && $intext == 0) {
     if ($string =~ /<$textstring/) {
       $string =~ s/^.*<$textstring//;
       $intext = 1;
       $starting_linenr = $linenr;
     } else {
       $string = "";
     }
   }

   if (($intext == 1) && ($string =~ /<\/$textstring/)) {
     my $text = $string;
     $text =~ s/<\/$textstring.*$//;
     $text =~ s/&lt;/</g;
     $text =~ s/&gt;/>/g;
     $text =~ s/&amp;/&/g; 
     $text =~ s/\\n/ /g;
     $text =~ s/\\([^n])/\\\\$1/g;
     $text =~ s/\"/\\\"/g;
     $text =~ s/ +/ /g;
     $text =~ s/^ //g;
     $text =~ s/ $//g;
     writeoutstring($context, $text); 
     $string =~ s/^.*<\/$textstring//;
     $intext = 0;
     # Text can be multiline in .ui files (possibly), but we warn about it in XMLGUI .rc files.
     if ($linenr != $starting_linenr && $filename =~ m/\.rc$/) {
       print STDERR "there is <text> floating $filename\n";
     }
   }

}

if ($intext == 1) {
 print STDERR "parsing error in $filename $linenr\n";
 exit 1;
}

}