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";
35
my $VERSION = "0.35.4";
42
## Scalars used by the option stuff
46
my $VERSION_ARG = "0";
54
my $gettext_type = "";
64
## Use this instead of \w for XML files to handle more possible characters.
65
my $w = "[-A-Za-z0-9._:]";
72
"type=s" => \$TYPE_ARG,
73
"local|l" => \$LOCAL_ARG,
74
"help|h" => \$HELP_ARG,
75
"version|v" => \$VERSION_ARG,
76
"update" => \$UPDATE_ARG,
77
"quiet|q" => \$QUIET_ARG,
78
"srcdir=s" => \$SRCDIR_ARG,
85
## This section will check for the different options.
87
sub split_on_argument {
95
} elsif ($LOCAL_ARG) {
99
} elsif ($UPDATE_ARG) {
103
} elsif (@ARGV > 0) {
116
$OUTFILE = "$FILE.h";
118
my $dirname = dirname ($OUTFILE);
119
if (! -d "$dirname" && $dirname ne "") {
120
system ("mkdir -p $dirname");
126
$OUTFILE = fileparse($FILE, ());
128
system("mkdir tmp/");
130
$OUTFILE = "./tmp/$OUTFILE.h"
134
if ($TYPE_ARG =~ /^gettext\/(.*)/) {
139
## Sub for printing release information
142
${PROGRAM} (${PACKAGE}) $VERSION
143
Copyright (C) 2000, 2003 Free Software Foundation, Inc.
144
Written by Kenneth Christiansen, 2000.
146
This is free software; see the source for copying conditions. There is NO
147
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
152
## Sub for printing usage information
155
Usage: ${PROGRAM} [OPTION]... [FILENAME]
156
Generates a header file from an XML source file.
158
It grabs all strings between <_translatable_node> and its end tag in
159
XML files. Read manpage (man ${PROGRAM}) for more info.
161
--type=TYPE Specify the file type of FILENAME. Currently supports:
162
"gettext/glade", "gettext/ini", "gettext/keys"
163
"gettext/rfc822deb", "gettext/schemas",
164
"gettext/scheme", "gettext/xml", "gettext/quoted"
165
-l, --local Writes output into current working directory
166
(conflicts with --update)
167
--update Writes output into the same directory the source file
168
reside (conflicts with --local)
169
--srcdir Root of the source tree
170
-v, --version Output version information and exit
171
-h, --help Display this help and exit
172
-q, --quiet Quiet mode
174
Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
175
or send email to <xml-i18n-tools\@gnome.org>.
180
## Sub for printing error messages
182
print STDERR "Try `${PROGRAM} --help' for more information.\n";
187
print "Generating C format header file for translation.\n" unless $QUIET_ARG;
195
open OUT, ">$OUTFILE";
196
binmode (OUT) if $^O eq 'MSWin32';
200
print "Wrote $OUTFILE\n" unless $QUIET_ARG;
208
local $/; #slurp mode
209
open (IN, "<$SRCDIR_ARG/$FILE") || die "can't open $SRCDIR_ARG/$FILE: $!";
213
&type_ini if $gettext_type eq "ini";
214
&type_keys if $gettext_type eq "keys";
215
&type_xml if $gettext_type eq "xml";
216
&type_glade if $gettext_type eq "glade";
217
&type_scheme if $gettext_type eq "scheme";
218
&type_schemas if $gettext_type eq "schemas";
219
&type_rfc822deb if $gettext_type eq "rfc822deb";
220
&type_quoted if $gettext_type eq "quoted";
223
sub entity_decode_minimal
249
return '\"' if $_ eq '"';
250
return '\n' if $_ eq "\n";
251
return '\\\\' if $_ eq '\\';
259
return join "", map &escape_char, split //, $string;
263
### For generic translatable desktop files ###
264
while ($input =~ /^_.*=(.*)$/mg) {
270
### For generic translatable mime/keys files ###
271
while ($input =~ /^\s*_\w+=(.*)$/mg) {
277
### For generic translatable XML files ###
278
my $tree = readXml($input);
284
my $vartype = ref $var;
286
if ($vartype =~ /ARRAY/) {
289
foreach my $el (@arr) {
294
} elsif ($vartype =~ /HASH/) {
297
foreach my $key (keys %hash) {
299
print_var($hash{$key});
308
# Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment)
309
sub getAttributeString
312
my $do_translate = shift || 1;
313
my $language = shift || "";
314
my $translate = shift;
316
foreach my $e (reverse(sort(keys %{ $sub }))) {
318
my $string = $sub->{$e};
321
$string =~ s/^[\s]+//;
322
$string =~ s/[\s]+$//;
324
if ($string =~ /^'.*'$/)
328
$string =~ s/^['"]//g;
329
$string =~ s/['"]$//g;
331
## differences from intltool-merge.in.in
333
$comments{entity_decode($string)} = $XMLCOMMENT if $XMLCOMMENT;
334
$messages{entity_decode($string)} = [];
337
## differences end here from intltool-merge.in.in
338
$result .= " $key=$quote$string$quote";
343
# Verbatim copy from intltool-merge.in.in
347
my $spacepreserve = shift || 0;
348
my @list = @{ $ref };
351
my $count = scalar(@list);
352
my $attrs = $list[0];
355
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
356
$spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
358
while ($index < $count) {
359
my $type = $list[$index];
360
my $content = $list[$index+1];
364
# lets strip the whitespace here, and *ONLY* here
365
$content =~ s/\s+/ /gs if (!$spacepreserve);
368
} elsif ( "$type" ne "1" ) {
369
# We've got another element
371
$result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
373
my $subresult = getXMLstring($content, $spacepreserve);
375
$result .= ">".$subresult . "</$type>";
388
# Verbatim copy from intltool-merge.in.in, except for MULTIPLE_OUTPUT handling removed
389
# Translate list of nodes if necessary
390
sub translate_subnodes
394
my $language = shift || "";
395
my $singlelang = shift || 0;
396
my $spacepreserve = shift || 0;
398
my @nodes = @{ $content };
400
my $count = scalar(@nodes);
402
while ($index < $count) {
403
my $type = $nodes[$index];
404
my $rest = $nodes[$index+1];
405
traverse($fh, $type, $rest, $language, $spacepreserve);
410
# Based on traverse() in intltool-merge.in.in
413
my $fh = shift; # unused, to allow us to sync code between -merge and -extract
414
my $nodename = shift;
416
my $language = shift || "";
417
my $spacepreserve = shift || 0;
419
if ($nodename && "$nodename" eq "1") {
420
$XMLCOMMENT = $content;
421
} elsif ($nodename) {
423
my @all = @{ $content };
424
my $attrs = shift @all;
426
my $outattr = getAttributeString($attrs, 1, $language, \$translate);
428
if ($nodename =~ /^_/) {
434
$spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
435
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
438
$lookup = getXMLstring($content, $spacepreserve);
439
if (!$spacepreserve) {
440
$lookup =~ s/^\s+//s;
441
$lookup =~ s/\s+$//s;
444
if ($lookup && $translate != 2) {
445
$comments{$lookup} = $XMLCOMMENT if $XMLCOMMENT;
446
$messages{$lookup} = [];
447
} elsif ($translate == 2) {
448
translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
452
my $count = scalar(@all);
455
while ($index < $count) {
456
my $type = $all[$index];
457
my $rest = $all[$index+1];
458
traverse($fh, $type, $rest, $language, $spacepreserve);
468
# Verbatim copy from intltool-merge.in.in, $fh for compatibility
473
my $language = shift || "";
475
my $name = shift @{ $ref };
476
my $cont = shift @{ $ref };
478
while (!$name || "$name" eq "1") {
479
$name = shift @{ $ref };
480
$cont = shift @{ $ref };
483
my $spacepreserve = 0;
484
my $attrs = @{$cont}[0];
485
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
487
traverse($fh, $name, $cont, $language, $spacepreserve);
490
# Verbatim copy from intltool-merge.in.in
491
sub intltool_tree_comment
494
my $data = $expat->original_string();
495
my $clist = $expat->{Curlist};
500
push @$clist, 1 => $data;
503
# Verbatim copy from intltool-merge.in.in
504
sub intltool_tree_cdatastart
507
my $clist = $expat->{Curlist};
510
push @$clist, 0 => $expat->original_string();
513
# Verbatim copy from intltool-merge.in.in
514
sub intltool_tree_cdataend
517
my $clist = $expat->{Curlist};
520
$clist->[$pos] .= $expat->original_string();
523
# Verbatim copy from intltool-merge.in.in
524
sub intltool_tree_char
528
my $clist = $expat->{Curlist};
531
# Use original_string so that we retain escaped entities
534
if ($pos > 0 and $clist->[$pos - 1] eq '0') {
535
$clist->[$pos] .= $expat->original_string();
537
push @$clist, 0 => $expat->original_string();
541
# Verbatim copy from intltool-merge.in.in
542
sub intltool_tree_start
548
# Use original_string so that we retain escaped entities
549
# in attribute values. We must convert the string to an
550
# @origlist array to conform to the structure of the Tree
553
my @original_array = split /\x/, $expat->original_string();
554
my $source = $expat->original_string();
556
# Remove leading tag.
558
$source =~ s|^\s*<\s*(\S+)||s;
560
# Grab attribute key/value pairs and push onto @origlist array.
564
if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
566
$source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
568
push @origlist, '"' . $2 . '"';
570
elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
572
$source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
574
push @origlist, "'" . $2 . "'";
582
my $ol = [ { @origlist } ];
584
push @{ $expat->{Lists} }, $expat->{Curlist};
585
push @{ $expat->{Curlist} }, $tag => $ol;
586
$expat->{Curlist} = $ol;
589
# Copied from intltool-merge.in.in and added comment handler.
592
my $xmldoc = shift || return;
593
my $ret = eval 'require XML::Parser';
595
die "You must have XML::Parser installed to run $0\n\n";
597
my $xp = new XML::Parser(Style => 'Tree');
598
$xp->setHandlers(Char => \&intltool_tree_char);
599
$xp->setHandlers(Start => \&intltool_tree_start);
600
$xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
601
$xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
603
## differences from intltool-merge.in.in
604
$xp->setHandlers(Comment => \&intltool_tree_comment);
605
## differences end here from intltool-merge.in.in
607
my $tree = $xp->parse($xmldoc);
610
# <foo><!-- comment --><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
612
# [foo, [{}, 1, "comment", head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar,
613
# [{}, 0, "Howdy", ref, [{}]], 0, "do" ] ]
619
### For schemas XML files ###
621
# FIXME: We should handle escaped < (less than)
623
<locale\ name="C">\s*
624
(<default>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/default>\s*)?
625
(<short>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/short>\s*)?
626
(<long>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/long>\s*)?
629
my @totranslate = ($3,$6,$9);
630
my @eachcomment = ($2,$5,$8);
631
foreach (@totranslate) {
632
my $currentcomment = shift @eachcomment;
635
$messages{entity_decode_minimal($_)} = [];
636
$comments{entity_decode_minimal($_)} = $currentcomment if (defined($currentcomment));
642
### For rfc822-style Debian configuration files ###
646
while ($input =~ /\G(.*?)(^|\n)(_+)([^:]+):[ \t]*(.*?)(?=\n\S|$)/sg)
648
my ($pre, $newline, $underscore, $tag, $text) = ($1, $2, $3, $4, $5);
649
while ($pre =~ m/\n/g)
653
$lineno += length($newline);
654
my @str_list = rfc822deb_split(length($underscore), $text);
655
for my $str (@str_list)
658
$messages{$str} = [];
659
$loc{$str} = $lineno;
660
$count{$str} = $strcount;
661
my $usercomment = '';
662
while($pre =~ s/(^|\n)#([^\n]*)$//s)
664
$usercomment = "\n" . $2 . $usercomment;
666
$comments{$str} = $tag . $usercomment;
668
$lineno += ($text =~ s/\n//g);
672
sub rfc822deb_split {
673
# Debian defines a special way to deal with rfc822-style files:
674
# when a value contain newlines, it consists of
675
# 1. a short form (first line)
676
# 2. a long description, all lines begin with a space,
677
# and paragraphs are separated by a single dot on a line
678
# This routine returns an array of all paragraphs, and reformat
680
# When first argument is 2, the string is a comma separated list of
684
$text =~ s/^[ \t]//mg;
685
return (split(/, */, $text, 0)) if $type ne 1;
686
return ($text) if $text !~ /\n/;
688
$text =~ s/([^\n]*)\n//;
691
for my $line (split (/\n/, $text))
694
if ($line =~ /^\.\s*$/)
701
elsif ($line =~ /^\s/)
703
# Line which must not be reformatted
704
$str .= "\n" if length ($str) && $str !~ /\n$/;
710
# Continuation line, remove newline
711
$str .= " " if length ($str) && $str !~ /\n$/;
716
push(@list, $str) if length ($str);
721
while ($input =~ /\"(([^\"]|\\\")*[^\\\"])\"/g) {
724
$message =~ s/\\\"/\"/g;
725
$before =~ s/[^\n]//g;
726
$messages{$message} = [];
727
$loc{$message} = length ($before) + 2;
732
### For translatable Glade XML files ###
734
my $tags = "label|title|text|format|copyright|comments|preview_text|tooltip|message";
736
while ($input =~ /<($tags)>([^<]+)<\/($tags)>/sg) {
737
# Glade sometimes uses tags that normally mark translatable things for
738
# little bits of non-translatable content. We work around this by not
739
# translating strings that only includes something like label4 or window1.
740
$messages{entity_decode($2)} = [] unless $2 =~ /^(window|label|dialog)[0-9]+$/;
743
while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
744
for my $item (split (/\n/, $1)) {
745
$messages{entity_decode($item)} = [];
749
## handle new glade files
750
while ($input =~ /<(property|atkproperty)\s+[^>]*translatable\s*=\s*"yes"(?:\s+[^>]*comments\s*=\s*"([^"]*)")?[^>]*>([^<]+)<\/\1>/sg) {
751
$messages{entity_decode($3)} = [] unless $3 =~ /^(window|label)[0-9]+$/;
752
if (defined($2) and !($3 =~ /^(window|label)[0-9]+$/)) {
753
$comments{entity_decode($3)} = entity_decode($2) ;
756
while ($input =~ /<atkaction\s+action_name="([^>]*)"\s+description="([^>]+)"\/>/sg) {
757
$messages{entity_decode_minimal($2)} = [];
762
my ($line, $i, $state, $str, $trcomment, $char);
763
for $line (split(/\n/, $input)) {
765
$state = 0; # 0 - nothing, 1 - string, 2 - translatable string
766
while ($i < length($line)) {
767
if (substr($line,$i,1) eq "\"") {
769
$comments{$str} = $trcomment if ($trcomment);
770
$messages{$str} = [];
772
$state = 0; $trcomment = "";
773
} elsif ($state == 1) {
775
$state = 0; $trcomment = "";
779
if ($i>0 && substr($line,$i-1,1) eq '_') {
784
if (substr($line,$i,1) eq ";") {
785
$trcomment = substr($line,$i+1);
786
$trcomment =~ s/^;*\s*//;
788
} elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
792
if (substr($line,$i,1) eq "\\") {
793
$char = substr($line,$i+1,1);
794
if ($char ne "\"" && $char ne "\\") {
799
$str = $str . substr($line,$i,1);
810
@msgids = sort { $count{$a} <=> $count{$b} } keys %count;
814
@msgids = sort keys %messages;
816
for my $message (@msgids)
819
$offsetlines++ if $message =~ /%/;
820
if (defined ($comments{$message}))
822
while ($comments{$message} =~ m/\n/g)
827
print OUT "# ".($loc{$message} - $offsetlines). " \"$FILE\"\n"
828
if defined $loc{$message};
829
print OUT "/* ".$comments{$message}." */\n"
830
if defined $comments{$message};
831
print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/;
833
my @lines = split (/\n/, $message, -1);
834
for (my $n = 0; $n < @lines; $n++)
838
print OUT "char *s = N_(\"";
845
print OUT escape($lines[$n]);