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.5";
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 =~ /^(#(.+)\n)?^_.*=(.*)$/mg) {
273 ### For generic translatable mime/keys files ###
274 while ($input =~ /^\s*_\w+=(.*)$/mg) {
280 ### For generic translatable XML files ###
281 my $tree = readXml
($input);
287 my $vartype = ref $var;
289 if ($vartype =~ /ARRAY/) {
292 foreach my $el (@arr) {
297 } elsif ($vartype =~ /HASH/) {
300 foreach my $key (keys %hash) {
302 print_var
($hash{$key});
311 # Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment)
312 sub getAttributeString
315 my $do_translate = shift || 1;
316 my $language = shift || "";
317 my $translate = shift;
319 foreach my $e (reverse(sort(keys %{ $sub }))) {
321 my $string = $sub->{$e};
324 $string =~ s/^[\s]+//;
325 $string =~ s/[\s]+$//;
327 if ($string =~ /^'.*'$/)
331 $string =~ s/^['"]//g;
332 $string =~ s/['"]$//g;
334 ## differences from intltool-merge.in.in
336 $comments{entity_decode
($string)} = $XMLCOMMENT if $XMLCOMMENT;
337 $messages{entity_decode
($string)} = [];
340 ## differences end here from intltool-merge.in.in
341 $result .= " $key=$quote$string$quote";
346 # Verbatim copy from intltool-merge.in.in
350 my $spacepreserve = shift || 0;
351 my @list = @
{ $ref };
354 my $count = scalar(@list);
355 my $attrs = $list[0];
358 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
359 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
361 while ($index < $count) {
362 my $type = $list[$index];
363 my $content = $list[$index+1];
367 # lets strip the whitespace here, and *ONLY* here
368 $content =~ s/\s+/ /gs if (!$spacepreserve);
371 } elsif ( "$type" ne "1" ) {
372 # We've got another element
374 $result .= getAttributeString
(@
{$content}[0], 0); # no nested translatable elements
376 my $subresult = getXMLstring
($content, $spacepreserve);
378 $result .= ">".$subresult . "</$type>";
391 # Verbatim copy from intltool-merge.in.in, except for MULTIPLE_OUTPUT handling removed
392 # Translate list of nodes if necessary
393 sub translate_subnodes
397 my $language = shift || "";
398 my $singlelang = shift || 0;
399 my $spacepreserve = shift || 0;
401 my @nodes = @
{ $content };
403 my $count = scalar(@nodes);
405 while ($index < $count) {
406 my $type = $nodes[$index];
407 my $rest = $nodes[$index+1];
408 traverse
($fh, $type, $rest, $language, $spacepreserve);
413 # Based on traverse() in intltool-merge.in.in
416 my $fh = shift; # unused, to allow us to sync code between -merge and -extract
417 my $nodename = shift;
419 my $language = shift || "";
420 my $spacepreserve = shift || 0;
422 if ($nodename && "$nodename" eq "1") {
423 $XMLCOMMENT = $content;
424 } elsif ($nodename) {
426 my @all = @
{ $content };
427 my $attrs = shift @all;
429 my $outattr = getAttributeString
($attrs, 1, $language, \
$translate);
431 if ($nodename =~ /^_/) {
437 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
438 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
441 $lookup = getXMLstring
($content, $spacepreserve);
442 if (!$spacepreserve) {
443 $lookup =~ s/^\s+//s;
444 $lookup =~ s/\s+$//s;
447 if ($lookup && $translate != 2) {
448 $comments{$lookup} = $XMLCOMMENT if $XMLCOMMENT;
449 $messages{$lookup} = [];
450 } elsif ($translate == 2) {
451 translate_subnodes
($fh, \
@all, $language, 1, $spacepreserve);
455 my $count = scalar(@all);
458 while ($index < $count) {
459 my $type = $all[$index];
460 my $rest = $all[$index+1];
461 traverse
($fh, $type, $rest, $language, $spacepreserve);
471 # Verbatim copy from intltool-merge.in.in, $fh for compatibility
476 my $language = shift || "";
478 my $name = shift @
{ $ref };
479 my $cont = shift @
{ $ref };
481 while (!$name || "$name" eq "1") {
482 $name = shift @
{ $ref };
483 $cont = shift @
{ $ref };
486 my $spacepreserve = 0;
487 my $attrs = @
{$cont}[0];
488 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
490 traverse
($fh, $name, $cont, $language, $spacepreserve);
493 # Verbatim copy from intltool-merge.in.in
494 sub intltool_tree_comment
497 my $data = $expat->original_string();
498 my $clist = $expat->{Curlist
};
503 push @
$clist, 1 => $data;
506 # Verbatim copy from intltool-merge.in.in
507 sub intltool_tree_cdatastart
510 my $clist = $expat->{Curlist
};
513 push @
$clist, 0 => $expat->original_string();
516 # Verbatim copy from intltool-merge.in.in
517 sub intltool_tree_cdataend
520 my $clist = $expat->{Curlist
};
523 $clist->[$pos] .= $expat->original_string();
526 # Verbatim copy from intltool-merge.in.in
527 sub intltool_tree_char
531 my $clist = $expat->{Curlist
};
534 # Use original_string so that we retain escaped entities
537 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
538 $clist->[$pos] .= $expat->original_string();
540 push @
$clist, 0 => $expat->original_string();
544 # Verbatim copy from intltool-merge.in.in
545 sub intltool_tree_start
551 # Use original_string so that we retain escaped entities
552 # in attribute values. We must convert the string to an
553 # @origlist array to conform to the structure of the Tree
556 my @original_array = split /\x/, $expat->original_string();
557 my $source = $expat->original_string();
559 # Remove leading tag.
561 $source =~ s
|^\s
*<\s
*(\S
+)||s
;
563 # Grab attribute key/value pairs and push onto @origlist array.
567 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
569 $source =~ s
|^\s
*([\w
:-]+)\s
*[=]\s
*["]([^"]*)["]||s;
571 push @origlist, '"' . $2 . '"';
573 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
575 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
577 push @origlist, "'" . $2 . "'";
585 my $ol = [ { @origlist } ];
587 push @{ $expat->{Lists} }, $expat->{Curlist};
588 push @{ $expat->{Curlist} }, $tag => $ol;
589 $expat->{Curlist} = $ol;
592 # Copied from intltool-merge.in.in and added comment handler.
595 my $xmldoc = shift || return;
596 my $ret = eval 'require XML::Parser';
598 die "You must have XML
::Parser installed to run
$0\n\n";
600 my $xp = new XML::Parser(Style => 'Tree');
601 $xp->setHandlers(Char => \&intltool_tree_char);
602 $xp->setHandlers(Start => \&intltool_tree_start);
603 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
604 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
606 ## differences from intltool-merge.in.in
607 $xp->setHandlers(Comment => \&intltool_tree_comment);
608 ## differences end here from intltool-merge.in.in
610 my $tree = $xp->parse($xmldoc);
613 # <foo><!-- comment --><head id="a
">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
615 # [foo, [{}, 1, "comment
", head, [{id => "a
"}, 0, "Hello
", em, [{}, 0, "there
"]], bar,
616 # [{}, 0, "Howdy
", ref, [{}]], 0, "do" ] ]
622 ### For schemas XML files ###
624 # FIXME: We should handle escaped < (less than)
626 <locale\ name="C
">\s*
627 (<default>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/default>\s*)?
628 (<short>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/short>\s*)?
629 (<long>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/long>\s*)?
632 my @totranslate = ($3,$6,$9);
633 my @eachcomment = ($2,$5,$8);
634 foreach (@totranslate) {
635 my $currentcomment = shift @eachcomment;
638 $messages{entity_decode_minimal($_)} = [];
639 $comments{entity_decode_minimal($_)} = $currentcomment if (defined($currentcomment));
645 ### For rfc822-style Debian configuration files ###
649 while ($input =~ /\G(.*?)(^|\n)(_+)([^:]+):[ \t]*(.*?)(?=\n\S|$)/sg)
651 my ($pre, $newline, $underscore, $tag, $text) = ($1, $2, $3, $4, $5);
652 while ($pre =~ m/\n/g)
656 $lineno += length($newline);
657 my @str_list = rfc822deb_split(length($underscore), $text);
658 for my $str (@str_list)
661 $messages{$str} = [];
662 $loc{$str} = $lineno;
663 $count{$str} = $strcount;
664 my $usercomment = '';
665 while($pre =~ s/(^|\n)#([^\n]*)$//s)
667 $usercomment = "\n" . $2 . $usercomment;
669 $comments{$str} = $tag . $usercomment;
671 $lineno += ($text =~ s/\n//g);
675 sub rfc822deb_split {
676 # Debian defines a special way to deal with rfc822-style files:
677 # when a value contain newlines, it consists of
678 # 1. a short form (first line)
679 # 2. a long description, all lines begin with a space,
680 # and paragraphs are separated by a single dot on a line
681 # This routine returns an array of all paragraphs, and reformat
683 # When first argument is 2, the string is a comma separated list of
687 $text =~ s/^[ \t]//mg;
688 return (split(/, */, $text, 0)) if $type ne 1;
689 return ($text) if $text !~ /\n/;
691 $text =~ s/([^\n]*)\n//;
694 for my $line (split (/\n/, $text))
697 if ($line =~ /^\.\s*$/)
704 elsif ($line =~ /^\s/)
706 # Line which must not be reformatted
707 $str .= "\n" if length ($str) && $str !~ /\n$/;
713 # Continuation line, remove newline
714 $str .= " " if length ($str) && $str !~ /\n$/;
719 push(@list, $str) if length ($str);
724 while ($input =~ /\"(([^\"]|\\\")*[^\\\"])\"/g) {
727 $message =~ s/\\\"/\"/g;
728 $before =~ s/[^\n]//g;
729 $messages{$message} = [];
730 $loc{$message} = length ($before) + 2;
735 ### For translatable Glade XML files ###
737 my $tags = "label
|title
|text
|format
|copyright
|comments
|preview_text
|tooltip
|message
";
739 while ($input =~ /<($tags)>([^<]+)<\/($tags)>/sg) {
740 # Glade sometimes uses tags that normally mark translatable things for
741 # little bits of non-translatable content. We work around this by not
742 # translating strings that only includes something like label4 or window1.
743 $messages{entity_decode($2)} = [] unless $2 =~ /^(window|label|dialog)[0-9]+$/;
746 while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
747 for my $item (split (/\n/, $1)) {
748 $messages{entity_decode($item)} = [];
752 ## handle new glade files
753 while ($input =~ /<(property|atkproperty)\s+[^>]*translatable\s*=\s*"yes
"(?:\s+[^>]*comments\s*=\s*"([^"]*)")?
[^>]*>([^<]+)<\
/\
1>/sg
) {
754 $messages{entity_decode
($3)} = [] unless $3 =~ /^(window|label)[0-9]+$/;
755 if (defined($2) and !($3 =~ /^(window|label)[0-9]+$/)) {
756 $comments{entity_decode
($3)} = entity_decode
($2) ;
759 while ($input =~ /<atkaction\s+action_name="([^>]*)"\s+description="([^>]+)"\/>/sg
) {
760 $messages{entity_decode_minimal
($2)} = [];
765 my ($line, $i, $state, $str, $trcomment, $char);
766 for $line (split(/\n/, $input)) {
768 $state = 0; # 0 - nothing, 1 - string, 2 - translatable string
769 while ($i < length($line)) {
770 if (substr($line,$i,1) eq "\"") {
772 $comments{$str} = $trcomment if ($trcomment);
773 $messages{$str} = [];
775 $state = 0; $trcomment = "";
776 } elsif ($state == 1) {
778 $state = 0; $trcomment = "";
782 if ($i>0 && substr($line,$i-1,1) eq '_') {
787 if (substr($line,$i,1) eq ";") {
788 $trcomment = substr($line,$i+1);
789 $trcomment =~ s/^;*\s*//;
791 } elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
795 if (substr($line,$i,1) eq "\\") {
796 $char = substr($line,$i+1,1);
797 if ($char ne "\"" && $char ne "\\") {
802 $str = $str . substr($line,$i,1);
813 @msgids = sort { $count{$a} <=> $count{$b} } keys %count;
817 @msgids = sort keys %messages;
819 for my $message (@msgids)
822 $offsetlines++ if $message =~ /%/;
823 if (defined ($comments{$message}))
825 while ($comments{$message} =~ m/\n/g)
830 print OUT
"# ".($loc{$message} - $offsetlines). " \"$FILE\"\n"
831 if defined $loc{$message};
832 print OUT
"/* ".$comments{$message}." */\n"
833 if defined $comments{$message};
834 print OUT
"/* xgettext:no-c-format */\n" if $message =~ /%/;
836 my @lines = split (/\n/, $message, -1);
837 for (my $n = 0; $n < @lines; $n++)
841 print OUT
"char *s = N_(\"";
848 print OUT escape
($lines[$n]);