2 # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4 -*-
5 # The Intltool Message Extractor
7 # Copyright (C) 2000-2001, 2003 Free Software Foundation.
9 # Intltool 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.
14 # Intltool 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.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # As a special exception to the GNU General Public License, if you
24 # distribute this file as part of a program that contains a
25 # configuration script generated by Autoconf, you may include it under
26 # the same distribution terms that you use for the rest of that program.
28 # Authors: Kenneth Christiansen <kenneth@gnu.org>
29 # Darin Adler <darin@bentspoon.com>
32 ## Release information
33 my $PROGRAM = "intltool-extract";
34 my $PACKAGE = "intltool";
42 ## Scalars used by the option stuff
46 my $VERSION_ARG = "0";
54 my $gettext_type = "";
62 ## Use this instead of \w for XML files to handle more possible characters.
63 my $w = "[-A-Za-z0-9._:]";
70 "type=s" => \$TYPE_ARG,
71 "local|l" => \$LOCAL_ARG,
72 "help|h" => \$HELP_ARG,
73 "version|v" => \$VERSION_ARG,
74 "update" => \$UPDATE_ARG,
75 "quiet|q" => \$QUIET_ARG,
76 "srcdir=s" => \$SRCDIR_ARG,
83 ## This section will check for the different options.
85 sub split_on_argument {
93 } elsif ($LOCAL_ARG) {
97 } elsif ($UPDATE_ARG) {
101 } elsif (@ARGV > 0) {
114 $OUTFILE = "$FILE.h";
118 $OUTFILE = fileparse($FILE, ());
120 system("mkdir tmp/");
122 $OUTFILE = "./tmp/$OUTFILE.h"
126 if ($TYPE_ARG =~ /^gettext\/(.*)/) {
131 ## Sub for printing release information
134 ${PROGRAM} (${PACKAGE}) $VERSION
135 Copyright (C) 2000, 2003 Free Software Foundation, Inc.
136 Written by Kenneth Christiansen, 2000.
138 This is free software; see the source for copying conditions. There is NO
139 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
144 ## Sub for printing usage information
147 Usage: ${PROGRAM} [OPTION]... [FILENAME]
148 Generates a header file from an XML source file.
150 It grabs all strings between <_translatable_node> and its end tag in
151 XML files. Read manpage (man ${PROGRAM}) for more info.
153 --type=TYPE Specify the file type of FILENAME. Currently supports:
154 "gettext/glade", "gettext/ini", "gettext/keys"
155 "gettext/rfc822deb", "gettext/schemas",
156 "gettext/scheme", "gettext/xml"
157 -l, --local Writes output into current working directory
158 (conflicts with --update)
159 --update Writes output into the same directory the source file
160 reside (conflicts with --local)
161 --srcdir Root of the source tree
162 -v, --version Output version information and exit
163 -h, --help Display this help and exit
164 -q, --quiet Quiet mode
166 Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
167 or send email to <xml-i18n-tools\@gnome.org>.
172 ## Sub for printing error messages
174 print STDERR "Try `${PROGRAM} --help' for more information.\n";
179 print "Generating C format header file for translation.\n" unless $QUIET_ARG;
187 open OUT, ">$OUTFILE";
191 print "Wrote $OUTFILE\n" unless $QUIET_ARG;
199 local $/; #slurp mode
200 open (IN, "<$SRCDIR_ARG/$FILE") || die "can't open $SRCDIR_ARG/$FILE: $!";
204 &type_ini if $gettext_type eq "ini";
205 &type_keys if $gettext_type eq "keys";
206 &type_xml if $gettext_type eq "xml";
207 &type_glade if $gettext_type eq "glade";
208 &type_scheme if $gettext_type eq "scheme";
209 &type_schemas if $gettext_type eq "schemas";
210 &type_rfc822deb if $gettext_type eq "rfc822deb";
213 sub entity_decode_minimal
239 return '\"' if $_ eq '"';
240 return '\n' if $_ eq "\n";
241 return '\\' if $_ eq '\\';
249 return join "", map &escape_char, split //, $string;
253 ### For generic translatable desktop files ###
254 while ($input =~ /^_.*=(.*)$/mg) {
260 ### For generic translatable mime/keys files ###
261 while ($input =~ /^\s*_\w+=(.*)$/mg) {
267 ### For generic translatable XML files ###
269 while ($input =~ /\s_$w+\s*=\s*\"([^"]+)\"/sg) { # "
270 $messages{entity_decode_minimal($1)} = [];
273 while ($input =~ /<_($w+)(?: xml:space="($w+)")?>(.+?)<\/_\1>/sg) {
275 if (!defined($2) || $2 ne "preserve") {
280 $messages{entity_decode_minimal($_)} = [];
285 ### For schemas XML files ###
287 # FIXME: We should handle escaped < (less than)
289 <locale\ name="C">\s*
290 (<default>\s*(.*?)\s*<\/default>\s*)?
291 (<short>\s*(.*?)\s*<\/short>\s*)?
292 (<long>\s*(.*?)\s*<\/long>\s*)?
295 my @totranslate = ($2,$4,$6);
296 foreach (@totranslate) {
299 $messages{entity_decode_minimal($_)} = [];
305 ### For rfc822-style Debian configuration files ###
309 while ($input =~ /\G(.*?)(^|\n)(_+)([^:]+):[ \t]*(.*?)(?=\n\S|$)/sg)
311 my ($pre, $newline, $underscore, $tag, $text) = ($1, $2, $3, $4, $5);
312 while ($pre =~ m/\n/g)
316 $lineno += length($newline);
317 my @str_list = rfc822deb_split(length($underscore), $text);
318 for my $str (@str_list)
321 $messages{$str} = [];
322 $loc{$str} = $lineno;
323 $count{$str} = $strcount;
324 my $usercomment = '';
325 while($pre =~ s/(^|\n)#([^\n]*)$//s)
327 $usercomment = "\n" . $2 . $usercomment;
329 $comments{$str} = $tag . $usercomment;
331 $lineno += ($text =~ s/\n//g);
335 sub rfc822deb_split {
336 # Debian defines a special way to deal with rfc822-style files:
337 # when a value contain newlines, it consists of
338 # 1. a short form (first line)
339 # 2. a long description, all lines begin with a space,
340 # and paragraphs are separated by a single dot on a line
341 # This routine returns an array of all paragraphs, and reformat
343 # When first argument is 2, the string is a comma separated list of
347 $text =~ s/^[ \t]//mg;
348 return (split(/, */, $text, 0)) if $type ne 1;
349 return ($text) if $text !~ /\n/;
351 $text =~ s/([^\n]*)\n//;
354 for my $line (split (/\n/, $text))
357 if ($line =~ /^\.\s*$/)
364 elsif ($line =~ /^\s/)
366 # Line which must not be reformatted
367 $str .= "\n" if length ($str) && $str !~ /\n$/;
373 # Continuation line, remove newline
374 $str .= " " if length ($str) && $str !~ /\n$/;
379 push(@list, $str) if length ($str);
384 ### For translatable Glade XML files ###
386 my $tags = "label|title|text|format|copyright|comments|preview_text|tooltip|message";
388 while ($input =~ /<($tags)>([^<]+)<\/($tags)>/sg) {
389 # Glade sometimes uses tags that normally mark translatable things for
390 # little bits of non-translatable content. We work around this by not
391 # translating strings that only includes something like label4 or window1.
392 $messages{entity_decode($2)} = [] unless $2 =~ /^(window|label)[0-9]+$/;
395 while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
396 for my $item (split (/\n/, $1)) {
397 $messages{entity_decode($item)} = [];
401 ## handle new glade files
402 while ($input =~ /<(property|atkproperty)\s+[^>]*translatable\s*=\s*"yes"[^>]*>([^<]+)<\/\1>/sg) {
403 $messages{entity_decode($2)} = [] unless $2 =~ /^(window|label)[0-9]+$/;
405 while ($input =~ /<atkaction\s+action_name="([^>]*)"\s+description="([^>]+)"\/>/sg) {
406 $messages{entity_decode_minimal($2)} = [];
411 while ($input =~ /_\w*\(?"((?:[^"\\]+|\\.)*)"\)?/sg) {
420 @msgids = sort { $count{$a} <=> $count{$b} } keys %count;
424 @msgids = sort keys %messages;
426 for my $message (@msgids)
429 $offsetlines++ if $message =~ /%/;
430 if (defined ($comments{$message}))
432 while ($comments{$message} =~ m/\n/g)
437 print OUT "# ".($loc{$message} - $offsetlines). " \"$FILE\"\n"
438 if defined $loc{$message};
439 print OUT "/* ".$comments{$message}." */\n"
440 if defined $comments{$message};
441 print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/;
443 my @lines = split (/\n/, $message, -1);
444 for (my $n = 0; $n < @lines; $n++)
448 print OUT "char *s = N_(\"";
455 print OUT escape($lines[$n]);