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.37.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";
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",
166 -l, --local Writes output into current working directory
167 (conflicts with --update)
168 --update Writes output into the same directory the source file
169 reside (conflicts with --local)
170 --srcdir Root of the source tree
171 -v, --version Output version information and exit
172 -h, --help Display this help and exit
173 -q, --quiet Quiet mode
175 Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
176 or send email to <xml-i18n-tools\@gnome.org>.
181 ## Sub for printing error messages
183 print STDERR "Try `${PROGRAM} --help' for more information.\n";
188 print "Generating C format header file for translation.\n" unless $QUIET_ARG;
196 open OUT, ">$OUTFILE";
197 binmode (OUT) if $^O eq 'MSWin32';
201 print "Wrote $OUTFILE\n" unless $QUIET_ARG;
209 local $/; #slurp mode
210 open (IN, "<$SRCDIR_ARG/$FILE") || die "can't open $SRCDIR_ARG/$FILE: $!";
214 &type_ini if $gettext_type eq "ini";
215 &type_keys if $gettext_type eq "keys";
216 &type_xml if $gettext_type eq "xml";
217 &type_glade if $gettext_type eq "glade";
218 &type_scheme if $gettext_type eq "scheme";
219 &type_schemas if $gettext_type eq "schemas";
220 &type_rfc822deb if $gettext_type eq "rfc822deb";
221 &type_quoted if $gettext_type eq "quoted";
222 &type_quotedxml if $gettext_type eq "quotedxml";
225 sub entity_decode_minimal
251 return '\"' if $_ eq '"';
252 return '\n' if $_ eq "\n";
253 return '\\\\' if $_ eq '\\';
261 return join "", map &escape_char, split //, $string;
265 ### For generic translatable desktop files ###
266 while ($input =~ /^(#(.+)\n)?^_.*=(.*)$/mg) {
275 ### For generic translatable mime/keys files ###
276 while ($input =~ /^\s*_\w+=(.*)$/mg) {
282 ### For generic translatable XML files ###
283 my $tree = readXml($input);
289 my $vartype = ref $var;
291 if ($vartype =~ /ARRAY/) {
294 foreach my $el (@arr) {
299 } elsif ($vartype =~ /HASH/) {
302 foreach my $key (keys %hash) {
304 print_var($hash{$key});
313 # Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment)
314 sub getAttributeString
317 my $do_translate = shift || 1;
318 my $language = shift || "";
319 my $translate = shift;
321 foreach my $e (reverse(sort(keys %{ $sub }))) {
323 my $string = $sub->{$e};
326 $string =~ s/^[\s]+//;
327 $string =~ s/[\s]+$//;
329 if ($string =~ /^'.*'$/)
333 $string =~ s/^['"]//g;
334 $string =~ s/['"]$//g;
336 ## differences from intltool-merge.in.in
338 $comments{entity_decode($string)} = $XMLCOMMENT if $XMLCOMMENT;
339 $messages{entity_decode($string)} = [];
342 ## differences end here from intltool-merge.in.in
343 $result .= " $key=$quote$string$quote";
348 # Verbatim copy from intltool-merge.in.in
352 my $spacepreserve = shift || 0;
353 my @list = @{ $ref };
356 my $count = scalar(@list);
357 my $attrs = $list[0];
360 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
361 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
363 while ($index < $count) {
364 my $type = $list[$index];
365 my $content = $list[$index+1];
369 # lets strip the whitespace here, and *ONLY* here
370 $content =~ s/\s+/ /gs if (!$spacepreserve);
373 } elsif ( "$type" ne "1" ) {
374 # We've got another element
376 $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
378 my $subresult = getXMLstring($content, $spacepreserve);
380 $result .= ">".$subresult . "</$type>";
393 # Verbatim copy from intltool-merge.in.in, except for MULTIPLE_OUTPUT handling removed
394 # Translate list of nodes if necessary
395 sub translate_subnodes
399 my $language = shift || "";
400 my $singlelang = shift || 0;
401 my $spacepreserve = shift || 0;
403 my @nodes = @{ $content };
405 my $count = scalar(@nodes);
407 while ($index < $count) {
408 my $type = $nodes[$index];
409 my $rest = $nodes[$index+1];
410 traverse($fh, $type, $rest, $language, $spacepreserve);
415 # Based on traverse() in intltool-merge.in.in
418 my $fh = shift; # unused, to allow us to sync code between -merge and -extract
419 my $nodename = shift;
421 my $language = shift || "";
422 my $spacepreserve = shift || 0;
424 if ($nodename && "$nodename" eq "1") {
425 $XMLCOMMENT = $content;
426 } elsif ($nodename) {
428 my @all = @{ $content };
429 my $attrs = shift @all;
431 my $outattr = getAttributeString($attrs, 1, $language, \$translate);
433 if ($nodename =~ /^_/) {
439 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
440 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
443 $lookup = getXMLstring($content, $spacepreserve);
444 if (!$spacepreserve) {
445 $lookup =~ s/^\s+//s;
446 $lookup =~ s/\s+$//s;
449 if ($lookup && $translate != 2) {
450 $comments{$lookup} = $XMLCOMMENT if $XMLCOMMENT;
451 $messages{$lookup} = [];
452 } elsif ($translate == 2) {
453 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
457 my $count = scalar(@all);
460 while ($index < $count) {
461 my $type = $all[$index];
462 my $rest = $all[$index+1];
463 traverse($fh, $type, $rest, $language, $spacepreserve);
473 # Verbatim copy from intltool-merge.in.in, $fh for compatibility
478 my $language = shift || "";
480 my $name = shift @{ $ref };
481 my $cont = shift @{ $ref };
483 while (!$name || "$name" eq "1") {
484 $name = shift @{ $ref };
485 $cont = shift @{ $ref };
488 my $spacepreserve = 0;
489 my $attrs = @{$cont}[0];
490 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
492 traverse($fh, $name, $cont, $language, $spacepreserve);
495 # Verbatim copy from intltool-merge.in.in
496 sub intltool_tree_comment
499 my $data = $expat->original_string();
500 my $clist = $expat->{Curlist};
505 push @$clist, 1 => $data;
508 # Verbatim copy from intltool-merge.in.in
509 sub intltool_tree_cdatastart
512 my $clist = $expat->{Curlist};
515 push @$clist, 0 => $expat->original_string();
518 # Verbatim copy from intltool-merge.in.in
519 sub intltool_tree_cdataend
522 my $clist = $expat->{Curlist};
525 $clist->[$pos] .= $expat->original_string();
528 # Verbatim copy from intltool-merge.in.in
529 sub intltool_tree_char
533 my $clist = $expat->{Curlist};
536 # Use original_string so that we retain escaped entities
539 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
540 $clist->[$pos] .= $expat->original_string();
542 push @$clist, 0 => $expat->original_string();
546 # Verbatim copy from intltool-merge.in.in
547 sub intltool_tree_start
553 # Use original_string so that we retain escaped entities
554 # in attribute values. We must convert the string to an
555 # @origlist array to conform to the structure of the Tree
558 my @original_array = split /\x/, $expat->original_string();
559 my $source = $expat->original_string();
561 # Remove leading tag.
563 $source =~ s|^\s*<\s*(\S+)||s;
565 # Grab attribute key/value pairs and push onto @origlist array.
569 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
571 $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
573 push @origlist, '"' . $2 . '"';
575 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
577 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
579 push @origlist, "'" . $2 . "'";
587 my $ol = [ { @origlist } ];
589 push @{ $expat->{Lists} }, $expat->{Curlist};
590 push @{ $expat->{Curlist} }, $tag => $ol;
591 $expat->{Curlist} = $ol;
594 # Copied from intltool-merge.in.in and added comment handler.
597 my $xmldoc = shift || return;
598 my $ret = eval 'require XML::Parser';
600 die "You must have XML::Parser installed to run $0\n\n";
602 my $xp = new XML::Parser(Style => 'Tree');
603 $xp->setHandlers(Char => \&intltool_tree_char);
604 $xp->setHandlers(Start => \&intltool_tree_start);
605 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
606 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
608 ## differences from intltool-merge.in.in
609 $xp->setHandlers(Comment => \&intltool_tree_comment);
610 ## differences end here from intltool-merge.in.in
612 my $tree = $xp->parse($xmldoc);
615 # <foo><!-- comment --><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
617 # [foo, [{}, 1, "comment", head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar,
618 # [{}, 0, "Howdy", ref, [{}]], 0, "do" ] ]
624 ### For schemas XML files ###
626 # FIXME: We should handle escaped < (less than)
628 <locale\ name="C">\s*
629 (<default>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/default>\s*)?
630 (<short>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/short>\s*)?
631 (<long>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/long>\s*)?
634 my @totranslate = ($3,$6,$9);
635 my @eachcomment = ($2,$5,$8);
636 foreach (@totranslate) {
637 my $currentcomment = shift @eachcomment;
640 $messages{entity_decode_minimal($_)} = [];
641 $comments{entity_decode_minimal($_)} = $currentcomment if (defined($currentcomment));
647 ### For rfc822-style Debian configuration files ###
651 while ($input =~ /\G(.*?)(^|\n)(_+)([^:]+):[ \t]*(.*?)(?=\n\S|$)/sg)
653 my ($pre, $newline, $underscore, $tag, $text) = ($1, $2, $3, $4, $5);
654 while ($pre =~ m/\n/g)
658 $lineno += length($newline);
659 my @str_list = rfc822deb_split(length($underscore), $text);
660 for my $str (@str_list)
663 $messages{$str} = [];
664 $loc{$str} = $lineno;
665 $count{$str} = $strcount;
666 my $usercomment = '';
667 while($pre =~ s/(^|\n)#([^\n]*)$//s)
669 $usercomment = "\n" . $2 . $usercomment;
671 $comments{$str} = $tag . $usercomment;
673 $lineno += ($text =~ s/\n//g);
677 sub rfc822deb_split {
678 # Debian defines a special way to deal with rfc822-style files:
679 # when a value contain newlines, it consists of
680 # 1. a short form (first line)
681 # 2. a long description, all lines begin with a space,
682 # and paragraphs are separated by a single dot on a line
683 # This routine returns an array of all paragraphs, and reformat
685 # When first argument is 2, the string is a comma separated list of
689 $text =~ s/^[ \t]//mg;
690 return (split(/, */, $text, 0)) if $type ne 1;
691 return ($text) if $text !~ /\n/;
693 $text =~ s/([^\n]*)\n//;
696 for my $line (split (/\n/, $text))
699 if ($line =~ /^\.\s*$/)
706 elsif ($line =~ /^\s/)
708 # Line which must not be reformatted
709 $str .= "\n" if length ($str) && $str !~ /\n$/;
715 # Continuation line, remove newline
716 $str .= " " if length ($str) && $str !~ /\n$/;
721 push(@list, $str) if length ($str);
726 while ($input =~ /\"(([^\"]|\\\")*[^\\\"])\"/g) {
729 $message =~ s/\\\"/\"/g;
730 $before =~ s/[^\n]//g;
731 $messages{$message} = [];
732 $loc{$message} = length ($before) + 2;
737 while ($input =~ /\"(([^\"]|\\\")*[^\\\"])\"/g) {
740 $message =~ s/\\\"/\"/g;
741 $message = entity_decode($message);
742 $before =~ s/[^\n]//g;
743 $messages{$message} = [];
744 $loc{$message} = length ($before) + 2;
749 ### For translatable Glade XML files ###
751 my $tags = "label|title|text|format|copyright|comments|preview_text|tooltip|message";
753 while ($input =~ /<($tags)>([^<]+)<\/($tags)>/sg) {
754 # Glade sometimes uses tags that normally mark translatable things for
755 # little bits of non-translatable content. We work around this by not
756 # translating strings that only includes something like label4 or window1.
757 $messages{entity_decode($2)} = [] unless $2 =~ /^(window|label|dialog)[0-9]+$/;
760 while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
761 for my $item (split (/\n/, $1)) {
762 $messages{entity_decode($item)} = [];
766 ## handle new glade files
767 while ($input =~ /<(property|atkproperty)\s+[^>]*translatable\s*=\s*"yes"(?:\s+[^>]*comments\s*=\s*"([^"]*)")?[^>]*>([^<]+)<\/\1>/sg) {
768 $messages{entity_decode($3)} = [] unless $3 =~ /^(window|label)[0-9]+$/;
769 if (defined($2) and !($3 =~ /^(window|label)[0-9]+$/)) {
770 $comments{entity_decode($3)} = entity_decode($2) ;
773 while ($input =~ /<atkaction\s+action_name="([^>]*)"\s+description="([^>]+)"\/>/sg) {
774 $messages{entity_decode_minimal($2)} = [];
779 my ($line, $i, $state, $str, $trcomment, $char);
780 for $line (split(/\n/, $input)) {
782 $state = 0; # 0 - nothing, 1 - string, 2 - translatable string
783 while ($i < length($line)) {
784 if (substr($line,$i,1) eq "\"") {
786 $comments{$str} = $trcomment if ($trcomment);
787 $messages{$str} = [];
789 $state = 0; $trcomment = "";
790 } elsif ($state == 1) {
792 $state = 0; $trcomment = "";
796 if ($i>0 && substr($line,$i-1,1) eq '_') {
801 if (substr($line,$i,1) eq ";") {
802 $trcomment = substr($line,$i+1);
803 $trcomment =~ s/^;*\s*//;
805 } elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
809 if (substr($line,$i,1) eq "\\") {
810 $char = substr($line,$i+1,1);
811 if ($char ne "\"" && $char ne "\\") {
816 $str = $str . substr($line,$i,1);
827 @msgids = sort { $count{$a} <=> $count{$b} } keys %count;
831 @msgids = sort keys %messages;
833 for my $message (@msgids)
836 $offsetlines++ if $message =~ /%/;
837 if (defined ($comments{$message}))
839 while ($comments{$message} =~ m/\n/g)
844 print OUT "# ".($loc{$message} - $offsetlines). " \"$FILE\"\n"
845 if defined $loc{$message};
846 print OUT "/* ".$comments{$message}." */\n"
847 if defined $comments{$message};
848 print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/;
850 my @lines = split (/\n/, $message, -1);
851 for (my $n = 0; $n < @lines; $n++)
855 print OUT "char *s = N_(\"";
862 print OUT escape($lines[$n]);