aboutsummaryrefslogtreecommitdiffstats
path: root/po/ui-extract.pl
blob: 602649c8dee7e77b59ba256144056681c939c0ae (plain) (blame)
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
#!/usr/bin/perl -w 

#
#  The XML UI Translation Extractor
#
#  Copyright (C) 2000 Free Software Foundation.
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This script is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this library; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  Authors: Kenneth Christiansen <kenneth@gnu.org>
#



use strict;
use File::Basename;
use Getopt::Long;

#---------------------------

my $VERSION     = "0.8.5";

#---------------------------

my $LOCAL_ARG   = "0";
my $HELP_ARG    = "0";
my $VERSION_ARG = "0";
my $UPDATE_ARG  = "0";

#---------------------------

my $FILE;
my $OUTFILE;

#---------------------------

my %string  =  ();
my @elements;
my $n;

#---------------------------

$| = 1;

GetOptions (
        "local|l"    => \$LOCAL_ARG,
        "help|h|?"   => \$HELP_ARG,
        "version|v"  => \$VERSION_ARG,
        "update"     => \$UPDATE_ARG,
        ) or &Error;

&SplitOnArgument;


#---------------------------------------------------
# Check for options. 
# This section will check for the different options.
#---------------------------------------------------

sub SplitOnArgument {

    if ($VERSION_ARG) {
    &Version;

    } elsif ($HELP_ARG) {
    &Help;
   
    } elsif ($LOCAL_ARG) {
        &PlaceLocal;
        &Preparation;
        &WriteFile;

    } elsif ($UPDATE_ARG) {
    &PlaceNormal;
        &Preparation;
    &WriteFile;

    } elsif (@ARGV > 0) {
    &PlaceNormal;
    &Message;
    &Preparation;
    &WriteFile;

    } else {
    &Help;

    }  
}    

sub PlaceNormal {
    $FILE    = $ARGV[0];
    $OUTFILE     = "$FILE.h";
}   

sub PlaceLocal {
    $FILE    = $ARGV[0];
    $OUTFILE     = fileparse($FILE, ());
    if (!-e "tmp/") {
    system("mkdir tmp/");
    }
    $OUTFILE     = "./tmp/$OUTFILE.h"
}


#-------------------
sub Version{
    print "The XML UI Translations Extractor $VERSION\n";
    print "Written by Kenneth Christiansen, 2000.\n\n";
    print "Copyright (C) 2000 Free Software Foundation, Inc.\n";
    print "This is free software; see the source for copying conditions.  There is NO\n";
    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
    exit;
}

#-------------------  
sub Help{
    print "Usage: ui-extract.pl [FILENAME] [OPTIONS] ...\n";
    print "Generates a headerfile from an xml source.\n\nGraps all strings ";
    print "between <_translatable_node> and it's end tag,\nwhere tag are all allowed ";
    print "xml tags. Read the docs for more info.\n\n"; 
    print "  -V, --version                shows the version\n";
    print "  -H, --help                   shows this help page\n";
    print "\nReport bugs to <kenneth\@gnu.org>.\n";
    exit;
}

#------------------- 
sub Error{
#   print "ui-extract: invalid option @ARGV\n";
    print "Try `ui-extract.pl --help' for more information.\n";
    exit;
}

sub Message {
    print "Generating headerfile for XML translation.\n";
}

sub Preparation {

   if (-s "$OUTFILE"){
    unlink "$OUTFILE";
   }

   &Convert ($FILE);
}

sub WriteFile {

    open OUT, ">>$OUTFILE";
    &addMessages;
    close OUT;

    print  "Wrote $OUTFILE\n";
}

#-------------------
sub Convert($) {

    #-----------------
    # Reading the file
    #-----------------
    my $input; {
    local (*IN);
    local $/; #slurp mode
    open (IN, "<$FILE") || die "can't open $FILE: $!";
    $input = <IN>;
    }
 
    if (!-s "$OUTFILE"){
        open OUT, ">$OUTFILE";

    print OUT "/*\n";
        print OUT " * Translatable strings file generated by extract-ui.\n";
        print OUT " * Add this file to your project's POTFILES.in\n";
        print OUT " * DO NOT compile it as part of your application.\n";
        print OUT " */\n\n"; 
            
        }   
        close OUT;

    ### For generic translatable XML files ###
 
        if ($FILE =~ /xml$/sg){
        while ($input =~ /[\t\n\s]_[a-zA-Z0-9_]+=\"([^\"]+)\"/sg) {
        $string{$1} = [];
        }

    while ($input =~ /<_[a-zA-Z0-9_]+>(..[^_]*)<\/_[a-zA-Z0-9_]+>/sg) {
        $string{$1} = [];
    }}

        ### For translatable Glade XML files ###

        if ($FILE =~ /glade$/sg){
        my $translate = "label|items|title|text|format|copyright|comments|preview_text|tooltip";

        while ($input =~ /<($translate)>(..[^<]*)<\/($translate)>/sg) {

        # Glade has some bugs, especially it uses translations tags to contain little
        # non-translatable content. We work around this, by not including these
            # strings that only includes something like: label4, and window1
            if ($2 =~ /^(window|label)[0-9]$/g) {
            } else {
                $string{$2} = [];
            }
        }}
    }

sub addMessages{

    foreach my $theMessage (sort keys %string) {
    my ($lineNo,$fileName) = @{ $string{$theMessage} };

    # Replace XML codes for special chars to
    # geniune gettext syntax
    #---------------------------------------
    $theMessage =~ s/&quot;/\\"/mg;
    $theMessage =~ s/&lt;/</mg;
    $theMessage =~ s/&gt;/>/mg;

    if ($theMessage =~ /\n/) {

        @elements =  split (/\n/, $theMessage);
        for ($n = 0; $n < @elements; $n++) {

           if ($n == 0) { 
           print OUT "gchar *s = N_"; 
               print OUT "(\"$elements[$n]\\n\"\n";
           }

           elsif ($n == @elements - 1) { 
           print OUT "              ";
               print OUT "\"$elements[$n]\");\n";
           }

           elsif ($n > 0)  {    
           print OUT "             ";
               print OUT "(\"$elements[$n]\\n\");\n";
           }
        }

    } else {
        
        print OUT "gchar *s = N_(\"$theMessage\");\n";

    }
        
    }
}