File: xml-i18n-extract.in

package info (click to toggle)
glade-nonus 0.6.4-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,152 kB
  • ctags: 3,879
  • sloc: ansic: 54,332; sh: 6,839; makefile: 743; sed: 93
file content (303 lines) | stat: -rw-r--r-- 7,168 bytes parent folder | download | duplicates (10)
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
#!@XML_I18N_TOOLS_PERL@ -w 
# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-

#
#  The XML 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>
#           Darin Adler <darin@eazel.com>
#

## Release information
my $PROGRAM      = "xml-i18n-extract";
my $PACKAGE      = "xml-i18n-tools";
my $VERSION      = "0.9";

## Script options - Enable by setting value to 1
my $ENABLE_INI   = "1"; ## desktop and alike files
my $ENABLE_KEYS  = "1"; ## mimetype descriptions
my $ENABLE_GLADE = "1"; ## glade files
my $ENABLE_XML 	 = "1"; ## generic xml files

## Loaded modules
use strict; 
use File::Basename;
use Getopt::Long;

## Scalars used by the option stuff
my $TYPE_ARG	= "0";
my $LOCAL_ARG	= "0";
my $HELP_ARG 	= "0";
my $VERSION_ARG = "0";
my $UPDATE_ARG  = "0";
my $QUIET_ARG   = "0";

my $FILE;
my $OUTFILE;

my $gettext_type = "";
my $input;
my %messages = ();

## Always print first
$| = 1;

## Handle options
GetOptions (
	    "type=s"     => \$TYPE_ARG,
            "local|l"    => \$LOCAL_ARG,
            "help|h|?"   => \$HELP_ARG,
            "version|v"  => \$VERSION_ARG,
            "update"     => \$UPDATE_ARG,
	    "quiet|q"    => \$QUIET_ARG,
            ) or &error;

&split_on_argument;


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

sub split_on_argument {

    if ($VERSION_ARG) {
        &version;

    } elsif ($HELP_ARG) {
	&help;
        
    } elsif ($LOCAL_ARG) {
        &place_local;
        &extract;

    } elsif ($UPDATE_ARG) {
	&place_normal;
	&extract;

    } elsif (@ARGV > 0) {
	&place_normal;
	&message;
	&extract;

    } else {
	&help;

    }  
}    

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

sub place_local {
    $OUTFILE     = fileparse($FILE, ());
    if (!-e "tmp/") { 
        system("mkdir tmp/"); 
    }
    $OUTFILE     = "./tmp/$OUTFILE.h"
}

sub determine_type {
   if ($TYPE_ARG =~ /^gettext\/(.*)/) {
	$gettext_type=$1
   }
}

## Sub for printing release information
sub version{
    print "${PROGRAM} (${PACKAGE}) $VERSION\n";
    print "Copyright (C) 2000 Free Software Foundation, Inc.\n";
    print "Written by Kenneth Christiansen, 2000.\n\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 for printing usage information
sub help{
    print "Usage: ${PROGRAM} [FILENAME] [OPTIONS] ...\n";
    print "Generates a header file from an xml source file.\n\nGrabs 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 "  -q, --quiet                  quiet mode\n";
    print "\nReport bugs to <kenneth\@gnu.org>.\n";
    exit;
}

## Sub for printing error messages
sub error{
#   print "xml-i18n-extract: invalid option @ARGV\n";
    print "Try `${PROGRAM} --help' for more information.\n";
    exit;
}

sub message {
    print "Generating C format header file for translation.\n";
}

sub extract {
    &determine_type;

    &convert ($FILE);

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

    print "Wrote $OUTFILE\n" unless $QUIET_ARG;
}

sub convert($) {

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

    &type_ini;
    &type_keys;
    &type_xml;
    &type_glade;
}

sub type_ini {

    if ($ENABLE_INI) { 
        
        ### For generic translatable desktop files ###
    
        if ($gettext_type eq "ini"){    

            while ($input =~ /^_.*=(.*)$/mg) {
                $messages{$1} = [];
            }
        }
    }
}

sub type_keys {
    
    if ($ENABLE_KEYS) {
    
        ### For generic translatable mime/keys files ###

        if ($gettext_type eq "keys"){
            while ($input =~ /^\s*_\w+=(.*)$/mg) {
                $messages{$1} = [];
            }
        }
    }
}

sub type_xml {

    if ($ENABLE_XML) {

	### For generic translatable XML files ###
        
        if ($gettext_type eq "xml"){

            while ($input =~ /[\t\n\s]_\w+=\"([^\"]+)\"/sg) {
                $messages{$1} = [];
            }

            while ($input =~ /<_(\w+)>([^<]+)<\/_\1>/sg) {
                $messages{$2} = [];
            }

	}
    }
}

sub type_glade {

    if ($ENABLE_GLADE) {
        
        ### For translatable Glade XML files ###

        if ($gettext_type eq "glade"){

            my $translate = "label|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]+$/) {
                    $messages{$2} = [];
                }
            }
            
            while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
                my @items =  split (/\n/, $1);
                for (my $n = 0; $n < @items; $n++) {
                    $messages{$items[$n]} = [];
                }
            }

	}
    }

}

sub msg_write {
    
    foreach my $message (sort keys %messages) { 
        
        my ($tag) = @{ $messages{$message} };
        
        # Replace XML entities for some special characters with
        # the appropriate gettext syntax for those characters.
     	$message =~ s/&quot;/\\"/mg; # "
    	$message =~ s/&lt;/</mg;
    	$message =~ s/&gt;/>/mg;
     	$message =~ s/&amp;/&/mg;
       
   	print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/;
    	print OUT "/* $tag */\n" if $tag;
        
    	my @lines = split (/\n/, $message);

    	for (my $n = 0; $n < @lines; $n++) {

            if ($n == 0) { 
 		print OUT "char *s = N_(\""; 
            } else {  
 		print OUT "             \""; 
	    }

            print OUT $lines[$n];

            if ($n < @lines - 1) { 
		print OUT "\\n\"\n"; 
 	    } else { 
		print OUT "\");\n";  
	    }
        }
    }
}