source: trunk/third/xml-i18n-tools/xml-i18n-extract.in @ 15545

Revision 15545, 7.8 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15544, which included commits to RCS files with non-trunk default branches.
Line 
1#!@PERL@ -w
2# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 2  -*-
3
4#
5#  The XML Translation Extractor
6#
7#  Copyright (C) 2000 Free Software Foundation.
8#
9#  This library is free software; you can redistribute it and/or
10#  modify it under the terms of the GNU General Public License as
11#  published by the Free Software Foundation; either version 2 of the
12#  License, or (at your option) any later version.
13#
14#  This script is distributed in the hope that it will be useful,
15#  but WITHOUT ANY WARRANTY; without even the implied warranty of
16#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17#  General Public License for more details.
18#
19#  You should have received a copy of the GNU General Public License
20#  along with this library; if not, write to the Free Software
21#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22#
23#  Authors: Kenneth Christiansen <kenneth@gnu.org>
24#           Darin Adler <darin@eazel.com>
25#
26
27## Release information
28my $PROGRAM      = "xml-i18n-extract";
29my $PACKAGE      = "@PACKAGE@";
30my $VERSION      = "@VERSION@";
31
32## Script options - Enable by setting value to 1
33my $ENABLE_DESKTOP = "1";
34my $ENABLE_KEYS  = "1";
35my $ENABLE_GLADE = "1";
36my $ENABLE_XML   = "1";
37my $ENABLE_XP    = "0";
38
39## Loaded modules
40use strict;
41use File::Basename;
42use Getopt::Long;
43
44## Scalars used by the option stuff
45my $LOCAL_ARG   = "0";
46my $HELP_ARG    = "0";
47my $VERSION_ARG = "0";
48my $UPDATE_ARG  = "0";
49my $QUIET_ARG   = "0";
50
51my $FILE;
52my $OUTFILE;
53
54my %messages = ();
55
56## Always print first
57$| = 1;
58
59## Handle options
60GetOptions (
61            "local|l"    => \$LOCAL_ARG,
62            "help|h|?"   => \$HELP_ARG,
63            "version|v"  => \$VERSION_ARG,
64            "update"     => \$UPDATE_ARG,
65            "quiet|q"    => \$QUIET_ARG,
66            ) or &Error;
67
68&split_on_argument;
69
70
71## Check for options.
72## This section will check for the different options.
73
74sub split_on_argument {
75
76    if ($VERSION_ARG) {
77        &version;
78
79    } elsif ($HELP_ARG) {
80        &help;
81       
82    } elsif ($LOCAL_ARG) {
83        &place_local;
84        &extract;
85
86    } elsif ($UPDATE_ARG) {
87        &place_normal;
88        &extract;
89
90    } elsif (@ARGV > 0) {
91        &place_normal;
92        &message;
93        &extract;
94
95    } else {
96        &help;
97
98    } 
99}   
100
101sub place_normal {
102    $FILE        = $ARGV[0];
103    $OUTFILE     = "$FILE.h";
104}   
105
106sub place_local {
107    $FILE        = $ARGV[0];
108    $OUTFILE     = fileparse($FILE, ());
109    if (!-e "tmp/") {
110        system("mkdir tmp/");
111    }
112    $OUTFILE     = "./tmp/$OUTFILE.h"
113}
114
115sub precheck {
116    if (! $ENABLE_XML) {
117        if ($FILE =~ /(xml|oaf(\.in)+)$/) {
118            exit;
119        }
120    }
121    if (! $ENABLE_GLADE) {
122        if ($FILE =~ /glade$/) {
123            exit;
124        }
125    }
126    if (! $ENABLE_XP) {
127        if ($FILE =~ /\/xp\/.*\.h$/) {
128            exit;
129        }
130    }
131
132
133## Sub for printing release information
134sub version{
135    print "${PROGRAM} (${PACKAGE}) $VERSION\n";
136    print "Copyright (C) 2000 Free Software Foundation, Inc.\n";
137    print "Written by Kenneth Christiansen, 2000.\n\n";
138    print "This is free software; see the source for copying conditions. There is NO\n";
139    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
140    exit;
141}
142
143## Sub for printing usage information
144sub help{
145    print "Usage: ${PROGRAM} [FILENAME] [OPTIONS] ...\n";
146    print "Generates a header file from an xml source file.\n\nGrabs all strings ";
147    print "between <_translatable_node> and it's end tag,\nwhere tag are all allowed ";
148    print "xml tags. Read the docs for more info.\n\n";
149    print "  -V, --version                shows the version\n";
150    print "  -H, --help                   shows this help page\n";
151    print "  -X, --verbose                verbose mode\n";
152    print "  -Q, --quiet                  quiet mode\n";
153    print "\nReport bugs to <kenneth\@gnu.org>.\n";
154    exit;
155}
156
157## Sub for printing error messages
158sub error{
159#   print "xml-i18n-extract: invalid option @ARGV\n";
160    print "Try `xml-i18n-extract --help' for more information.\n";
161    exit;
162}
163
164sub message {
165    print "Generating C format header file for translation.\n";
166}
167
168sub extract {
169    &precheck;
170
171    &convert ($FILE);
172
173    open OUT, ">$OUTFILE";
174    &msg_write;
175    close OUT;
176
177    print "Wrote $OUTFILE\n" unless $QUIET_ARG;
178}
179
180sub convert($) {
181
182    ## Reading the file
183    my $input;
184    {
185        local (*IN);
186        local $/; #slurp mode
187        open (IN, "<$FILE") || die "can't open $FILE: $!";
188        $input = <IN>;
189    }
190
191    # This used to put a comment in $OUTFILE, but that comment
192    # showed up all over the .pot file, so it should not be put
193    # in there. The comment was more appropriate back when the
194    # files went in the source tree rather than a /tmp directory..
195
196    ######################
197    if ($ENABLE_DESKTOP) {
198    ######################
199       
200        ### For generic translatable desktop files ###
201   
202        if ($FILE =~ /\.desktop\.in$/){
203   
204            while ($input =~ /^_.*=(.*)$/mg) {
205                $messages{$1} = [];
206            }
207        }
208    }
209   
210    ###################
211    if ($ENABLE_KEYS) {
212    ###################
213   
214        ### For generic translatable mime/keys files ###
215
216        if ($FILE =~ /\.keys\.in$/){
217            while ($input =~ /^\s*_\w+=(.*)$/mg) {
218                $messages{$1} = [];
219            }
220        }
221    }
222
223
224    ##################
225    if ($ENABLE_XML) {
226    ##################
227
228        ### For generic translatable XML files ###
229       
230        if ($FILE =~ /\.(xml|oaf(\.in)+)$/){
231
232            while ($input =~ /[\t\n\s]_\w+=\"([^\"]+)\"/sg) {
233                $messages{$1} = [];
234            }
235
236            while ($input =~ /<_\w+>([^_]+)<\/_\w+>/sg) {
237                $messages{$1} = [];
238            }
239
240        }
241    }
242
243    ####################
244    if ($ENABLE_GLADE) {
245    ####################
246       
247        ### For translatable Glade XML files ###
248
249        if ($FILE =~ /glade$/){
250
251            my $translate = "label|title|text|format|copyright|comments|
252                             preview_text|tooltip";
253
254            while ($input =~ /<($translate)>([^<]+)<\/($translate)>/sg) {
255
256                # Glade has some bugs, especially it uses translations tags to contain little
257                # non-translatable content. We work around this, by not including these
258                # strings that only includes something like: label4, and window1
259                if ($2 !~ /^(window|label)[0-9]$/) {
260                    $messages{$2} = [];
261                }
262            }
263           
264            while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
265                my @items =  split (/\n/, $1);
266                for (my $n = 0; $n < @items; $n++) {
267                    $messages{$items[$n]} = [];
268                }
269            }
270
271        }
272    }
273
274    #################
275    if ($ENABLE_XP) {
276    #################
277
278        ### For generic translatable XP header files ###
279       
280        if ($FILE =~ /\/xp\/.*\.h$/){
281
282            while ($input =~ /\((.*),(.+)\"(.*)\"/g) {
283                my $tag = $1;
284                $messages{$3} = [$tag];
285            }
286
287        }
288
289    }
290}
291
292sub msg_write {
293   
294    foreach my $message (sort keys %messages) {
295       
296        my ($tag) = @{ $messages{$message} };
297       
298        # Replace XML entities for some special characters with
299        # the appropriate gettext syntax for those characters.
300        $message =~ s/&quot;/\\"/mg; # "
301        $message =~ s/&lt;/</mg;
302        $message =~ s/&gt;/>/mg;
303        $message =~ s/&amp;/&/mg;
304       
305        print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/;
306        print OUT "/* $tag */\n" if $tag;
307       
308        my @lines = split (/\n/, $message);
309
310        for (my $n = 0; $n < @lines; $n++) {
311
312            if ($n == 0) {
313                print OUT "char *s = N_(\"";
314            } else { 
315                print OUT "             \"";
316            }
317
318            print OUT $lines[$n];
319
320            if ($n < @lines - 1) {
321                print OUT "\\n\"\n";
322            } else {
323                print OUT "\");\n"; 
324            }
325        }
326    }
327}
328
329
Note: See TracBrowser for help on using the repository browser.