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.34.1";
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";
121 $OUTFILE = fileparse
($FILE, ());
123 system("mkdir tmp/");
125 $OUTFILE = "./tmp/$OUTFILE.h"
129 if ($TYPE_ARG =~ /^gettext\/(.*)/) {
134 ## Sub for printing release information
137 ${PROGRAM
} (${PACKAGE
}) $VERSION
138 Copyright
(C
) 2000, 2003 Free Software Foundation
, Inc
.
139 Written by Kenneth Christiansen
, 2000.
141 This is free software
; see the source
for copying conditions
. There is NO
142 warranty
; not even
for MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE
.
147 ## Sub for printing usage information
150 Usage
: ${PROGRAM
} [OPTION
]... [FILENAME
]
151 Generates a header file from an XML source file
.
153 It grabs all strings between
<_translatable_node
> and its end tag
in
154 XML files
. Read manpage
(man
${PROGRAM
}) for more info
.
156 --type
=TYPE Specify the file type of FILENAME
. Currently supports
:
157 "gettext/glade", "gettext/ini", "gettext/keys"
158 "gettext/rfc822deb", "gettext/schemas",
159 "gettext/scheme", "gettext/xml"
160 -l
, --local Writes output into current working directory
161 (conflicts with
--update
)
162 --update Writes output into the same directory the source file
163 reside
(conflicts with
--local)
164 --srcdir Root of the source tree
165 -v
, --version Output version information
and exit
166 -h
, --help Display this help
and exit
167 -q
, --quiet Quiet mode
169 Report bugs to http
://bugzilla
.gnome
.org
/ (product name
"$PACKAGE")
170 or send email to
<xml
-i18n
-tools\
@gnome.org
>.
175 ## Sub for printing error messages
177 print STDERR
"Try `${PROGRAM} --help' for more information.\n";
182 print "Generating C format header file for translation.\n" unless $QUIET_ARG;
190 open OUT
, ">$OUTFILE";
191 binmode (OUT
) if $^O
eq 'MSWin32';
195 print "Wrote $OUTFILE\n" unless $QUIET_ARG;
203 local $/; #slurp mode
204 open (IN
, "<$SRCDIR_ARG/$FILE") || die "can't open $SRCDIR_ARG/$FILE: $!";
208 &type_ini
if $gettext_type eq "ini";
209 &type_keys
if $gettext_type eq "keys";
210 &type_xml
if $gettext_type eq "xml";
211 &type_glade
if $gettext_type eq "glade";
212 &type_scheme
if $gettext_type eq "scheme";
213 &type_schemas
if $gettext_type eq "schemas";
214 &type_rfc822deb
if $gettext_type eq "rfc822deb";
217 sub entity_decode_minimal
243 return '\"' if $_ eq '"';
244 return '\n' if $_ eq "\n";
245 return '\\' if $_ eq '\\';
253 return join "", map &escape_char
, split //, $string;
257 ### For generic translatable desktop files ###
258 while ($input =~ /^_.*=(.*)$/mg) {
264 ### For generic translatable mime/keys files ###
265 while ($input =~ /^\s*_\w+=(.*)$/mg) {
271 ### For generic translatable XML files ###
272 my $tree = readXml
($input);
278 my $vartype = ref $var;
280 if ($vartype =~ /ARRAY/) {
283 foreach my $el (@arr) {
288 } elsif ($vartype =~ /HASH/) {
291 foreach my $key (keys %hash) {
293 print_var
($hash{$key});
302 # Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment)
303 sub getAttributeString
306 my $do_translate = shift || 1;
307 my $language = shift || "";
308 my $translate = shift;
310 foreach my $e (reverse(sort(keys %{ $sub }))) {
312 my $string = $sub->{$e};
315 $string =~ s/^[\s]+//;
316 $string =~ s/[\s]+$//;
318 if ($string =~ /^'.*'$/)
322 $string =~ s/^['"]//g;
323 $string =~ s/['"]$//g;
325 ## differences from intltool-merge.in.in
327 $comments{entity_decode
($string)} = $XMLCOMMENT if $XMLCOMMENT;
328 $messages{entity_decode
($string)} = [];
331 ## differences end here from intltool-merge.in.in
332 $result .= " $key=$quote$string$quote";
337 # Verbatim copy from intltool-merge.in.in
341 my $spacepreserve = shift || 0;
342 my @list = @
{ $ref };
345 my $count = scalar(@list);
346 my $attrs = $list[0];
349 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
350 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
352 while ($index < $count) {
353 my $type = $list[$index];
354 my $content = $list[$index+1];
358 # lets strip the whitespace here, and *ONLY* here
359 $content =~ s/\s+/ /gs if (!$spacepreserve);
362 } elsif ( "$type" ne "1" ) {
363 # We've got another element
365 $result .= getAttributeString
(@
{$content}[0], 0); # no nested translatable elements
367 my $subresult = getXMLstring
($content, $spacepreserve);
369 $result .= ">".$subresult . "</$type>";
382 # Verbatim copy from intltool-merge.in.in, except for MULTIPLE_OUTPUT handling removed
383 # Translate list of nodes if necessary
384 sub translate_subnodes
388 my $language = shift || "";
389 my $singlelang = shift || 0;
390 my $spacepreserve = shift || 0;
392 my @nodes = @
{ $content };
394 my $count = scalar(@nodes);
396 while ($index < $count) {
397 my $type = $nodes[$index];
398 my $rest = $nodes[$index+1];
399 traverse
($fh, $type, $rest, $language, $spacepreserve);
404 # Based on traverse() in intltool-merge.in.in
407 my $fh = shift; # unused, to allow us to sync code between -merge and -extract
408 my $nodename = shift;
410 my $language = shift || "";
411 my $spacepreserve = shift || 0;
413 if ($nodename && "$nodename" eq "1") {
414 $XMLCOMMENT = $content;
415 } elsif ($nodename) {
417 my @all = @
{ $content };
418 my $attrs = shift @all;
420 my $outattr = getAttributeString
($attrs, 1, $language, \
$translate);
422 if ($nodename =~ /^_/) {
428 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
429 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
432 $lookup = getXMLstring
($content, $spacepreserve);
433 if (!$spacepreserve) {
434 $lookup =~ s/^\s+//s;
435 $lookup =~ s/\s+$//s;
438 if ($lookup && $translate != 2) {
439 $comments{$lookup} = $XMLCOMMENT if $XMLCOMMENT;
440 $messages{$lookup} = [];
441 } elsif ($translate == 2) {
442 translate_subnodes
($fh, \
@all, $language, 1, $spacepreserve);
446 my $count = scalar(@all);
449 while ($index < $count) {
450 my $type = $all[$index];
451 my $rest = $all[$index+1];
452 traverse
($fh, $type, $rest, $language, $spacepreserve);
462 # Verbatim copy from intltool-merge.in.in, $fh for compatibility
467 my $language = shift || "";
469 my $name = shift @
{ $ref };
470 my $cont = shift @
{ $ref };
472 while (!$name || "$name" eq "1") {
473 $name = shift @
{ $ref };
474 $cont = shift @
{ $ref };
477 my $spacepreserve = 0;
478 my $attrs = @
{$cont}[0];
479 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
481 traverse
($fh, $name, $cont, $language, $spacepreserve);
484 # Verbatim copy from intltool-merge.in.in
485 sub intltool_tree_comment
489 my $clist = $expat->{Curlist
};
492 push @
$clist, 1 => $data;
495 # Verbatim copy from intltool-merge.in.in
496 sub intltool_tree_cdatastart
499 my $clist = $expat->{Curlist
};
502 push @
$clist, 0 => $expat->original_string();
505 # Verbatim copy from intltool-merge.in.in
506 sub intltool_tree_cdataend
509 my $clist = $expat->{Curlist
};
512 $clist->[$pos] .= $expat->original_string();
515 # Verbatim copy from intltool-merge.in.in
516 sub intltool_tree_char
520 my $clist = $expat->{Curlist
};
523 # Use original_string so that we retain escaped entities
526 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
527 $clist->[$pos] .= $expat->original_string();
529 push @
$clist, 0 => $expat->original_string();
533 # Verbatim copy from intltool-merge.in.in
534 sub intltool_tree_start
540 # Use original_string so that we retain escaped entities
541 # in attribute values. We must convert the string to an
542 # @origlist array to conform to the structure of the Tree
545 my @original_array = split /\x/, $expat->original_string();
546 my $source = $expat->original_string();
548 # Remove leading tag.
550 $source =~ s
|^\s
*<\s
*(\S
+)||s
;
552 # Grab attribute key/value pairs and push onto @origlist array.
556 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
558 $source =~ s
|^\s
*([\w
:-]+)\s
*[=]\s
*["]([^"]*)["]||s;
560 push @origlist, '"' . $2 . '"';
562 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
564 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
566 push @origlist, "'" . $2 . "'";
574 my $ol = [ { @origlist } ];
576 push @{ $expat->{Lists} }, $expat->{Curlist};
577 push @{ $expat->{Curlist} }, $tag => $ol;
578 $expat->{Curlist} = $ol;
581 # Copied from intltool-merge.in.in and added comment handler.
584 my $xmldoc = shift || return;
585 my $ret = eval 'require XML::Parser';
587 die "You must have XML
::Parser installed to run
$0\n\n";
589 my $xp = new XML::Parser(Style => 'Tree');
590 $xp->setHandlers(Char => \&intltool_tree_char);
591 $xp->setHandlers(Start => \&intltool_tree_start);
592 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
593 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
595 ## differences from intltool-merge.in.in
596 $xp->setHandlers(Comment => \&intltool_tree_comment);
597 ## differences end here from intltool-merge.in.in
599 my $tree = $xp->parse($xmldoc);
602 # <foo><!-- comment --><head id="a
">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
604 # [foo, [{}, 1, "comment
", head, [{id => "a
"}, 0, "Hello
", em, [{}, 0, "there
"]], bar,
605 # [{}, 0, "Howdy
", ref, [{}]], 0, "do" ] ]
611 ### For schemas XML files ###
613 # FIXME: We should handle escaped < (less than)
615 <locale\ name="C
">\s*
616 (<default>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/default>\s*)?
617 (<short>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/short>\s*)?
618 (<long>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/long>\s*)?
621 my @totranslate = ($3,$6,$9);
622 my @eachcomment = ($2,$5,$8);
623 foreach (@totranslate) {
624 my $currentcomment = shift @eachcomment;
627 $messages{entity_decode_minimal($_)} = [];
628 $comments{entity_decode_minimal($_)} = $currentcomment if (defined($currentcomment));
634 ### For rfc822-style Debian configuration files ###
638 while ($input =~ /\G(.*?)(^|\n)(_+)([^:]+):[ \t]*(.*?)(?=\n\S|$)/sg)
640 my ($pre, $newline, $underscore, $tag, $text) = ($1, $2, $3, $4, $5);
641 while ($pre =~ m/\n/g)
645 $lineno += length($newline);
646 my @str_list = rfc822deb_split(length($underscore), $text);
647 for my $str (@str_list)
650 $messages{$str} = [];
651 $loc{$str} = $lineno;
652 $count{$str} = $strcount;
653 my $usercomment = '';
654 while($pre =~ s/(^|\n)#([^\n]*)$//s)
656 $usercomment = "\n" . $2 . $usercomment;
658 $comments{$str} = $tag . $usercomment;
660 $lineno += ($text =~ s/\n//g);
664 sub rfc822deb_split {
665 # Debian defines a special way to deal with rfc822-style files:
666 # when a value contain newlines, it consists of
667 # 1. a short form (first line)
668 # 2. a long description, all lines begin with a space,
669 # and paragraphs are separated by a single dot on a line
670 # This routine returns an array of all paragraphs, and reformat
672 # When first argument is 2, the string is a comma separated list of
676 $text =~ s/^[ \t]//mg;
677 return (split(/, */, $text, 0)) if $type ne 1;
678 return ($text) if $text !~ /\n/;
680 $text =~ s/([^\n]*)\n//;
683 for my $line (split (/\n/, $text))
686 if ($line =~ /^\.\s*$/)
693 elsif ($line =~ /^\s/)
695 # Line which must not be reformatted
696 $str .= "\n" if length ($str) && $str !~ /\n$/;
702 # Continuation line, remove newline
703 $str .= " " if length ($str) && $str !~ /\n$/;
708 push(@list, $str) if length ($str);
713 ### For translatable Glade XML files ###
715 my $tags = "label
|title
|text
|format
|copyright
|comments
|preview_text
|tooltip
|message
";
717 while ($input =~ /<($tags)>([^<]+)<\/($tags)>/sg) {
718 # Glade sometimes uses tags that normally mark translatable things for
719 # little bits of non-translatable content. We work around this by not
720 # translating strings that only includes something like label4 or window1.
721 $messages{entity_decode($2)} = [] unless $2 =~ /^(window|label|dialog)[0-9]+$/;
724 while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
725 for my $item (split (/\n/, $1)) {
726 $messages{entity_decode($item)} = [];
730 ## handle new glade files
731 while ($input =~ /<(property|atkproperty)\s+[^>]*translatable\s*=\s*"yes
"(?:\s+[^>]*comments\s*=\s*"([^"]*)")?
[^>]*>([^<]+)<\
/\
1>/sg
) {
732 $messages{entity_decode
($3)} = [] unless $3 =~ /^(window|label)[0-9]+$/;
733 if (defined($2) and !($3 =~ /^(window|label)[0-9]+$/)) {
734 $comments{entity_decode
($3)} = entity_decode
($2) ;
737 while ($input =~ /<atkaction\s+action_name="([^>]*)"\s+description="([^>]+)"\/>/sg
) {
738 $messages{entity_decode_minimal
($2)} = [];
743 my ($line, $i, $state, $str, $trcomment, $char);
744 for $line (split(/\n/, $input)) {
746 $state = 0; # 0 - nothing, 1 - string, 2 - translatable string
747 while ($i < length($line)) {
748 if (substr($line,$i,1) eq "\"") {
750 $comments{$str} = $trcomment if ($trcomment);
751 $messages{$str} = [];
753 $state = 0; $trcomment = "";
754 } elsif ($state == 1) {
756 $state = 0; $trcomment = "";
760 if ($i>0 && substr($line,$i-1,1) eq '_') {
765 if (substr($line,$i,1) eq ";") {
766 $trcomment = substr($line,$i+1);
767 $trcomment =~ s/^;*\s*//;
769 } elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
773 if (substr($line,$i,1) eq "\\") {
774 $char = substr($line,$i+1,1);
775 if ($char ne "\"" && $char ne "\\") {
780 $str = $str . substr($line,$i,1);
791 @msgids = sort { $count{$a} <=> $count{$b} } keys %count;
795 @msgids = sort keys %messages;
797 for my $message (@msgids)
800 $offsetlines++ if $message =~ /%/;
801 if (defined ($comments{$message}))
803 while ($comments{$message} =~ m/\n/g)
808 print OUT
"# ".($loc{$message} - $offsetlines). " \"$FILE\"\n"
809 if defined $loc{$message};
810 print OUT
"/* ".$comments{$message}." */\n"
811 if defined $comments{$message};
812 print OUT
"/* xgettext:no-c-format */\n" if $message =~ /%/;
814 my @lines = split (/\n/, $message, -1);
815 for (my $n = 0; $n < @lines; $n++)
819 print OUT
"char *s = N_(\"";
826 print OUT escape
($lines[$n]);