l10n: Updated Russian (ru) translation to 100%
[maepad.git] / intltool-merge
blob023ac857a6e85c2089f96f60c8d7e5e3d8b455b3
1 #!/scratchbox/tools/bin/perl -w
2 # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4 -*-
5 # The Intltool Message Merger
7 # Copyright (C) 2000, 2003 Free Software Foundation.
8 # Copyright (C) 2000, 2001 Eazel, Inc
10 # Intltool is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # version 2 published by the Free Software Foundation.
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: Maciej Stachowiak <mjs@noisehavoc.org>
29 # Kenneth Christiansen <kenneth@gnu.org>
30 # Darin Adler <darin@bentspoon.com>
32 # Proper XML UTF-8'ification written by Cyrille Chepelov <chepelov@calixo.net>
35 ## Release information
36 my $PROGRAM = "intltool-merge";
37 my $PACKAGE = "intltool";
38 my $VERSION = "0.35.0";
40 ## Loaded modules
41 use strict;
42 use Getopt::Long;
43 use Text::Wrap;
44 use File::Basename;
46 my $must_end_tag = -1;
47 my $last_depth = -1;
48 my $translation_depth = -1;
49 my @tag_stack = ();
50 my @entered_tag = ();
51 my @translation_strings = ();
52 my $leading_space = "";
54 ## Scalars used by the option stuff
55 my $HELP_ARG = 0;
56 my $VERSION_ARG = 0;
57 my $BA_STYLE_ARG = 0;
58 my $XML_STYLE_ARG = 0;
59 my $KEYS_STYLE_ARG = 0;
60 my $DESKTOP_STYLE_ARG = 0;
61 my $SCHEMAS_STYLE_ARG = 0;
62 my $RFC822DEB_STYLE_ARG = 0;
63 my $QUIET_ARG = 0;
64 my $PASS_THROUGH_ARG = 0;
65 my $UTF8_ARG = 0;
66 my $MULTIPLE_OUTPUT = 0;
67 my $cache_file;
69 ## Handle options
70 GetOptions
72 "help" => \$HELP_ARG,
73 "version" => \$VERSION_ARG,
74 "quiet|q" => \$QUIET_ARG,
75 "oaf-style|o" => \$BA_STYLE_ARG, ## for compatibility
76 "ba-style|b" => \$BA_STYLE_ARG,
77 "xml-style|x" => \$XML_STYLE_ARG,
78 "keys-style|k" => \$KEYS_STYLE_ARG,
79 "desktop-style|d" => \$DESKTOP_STYLE_ARG,
80 "schemas-style|s" => \$SCHEMAS_STYLE_ARG,
81 "rfc822deb-style|r" => \$RFC822DEB_STYLE_ARG,
82 "pass-through|p" => \$PASS_THROUGH_ARG,
83 "utf8|u" => \$UTF8_ARG,
84 "multiple-output|m" => \$MULTIPLE_OUTPUT,
85 "cache|c=s" => \$cache_file
86 ) or &error;
88 my $PO_DIR;
89 my $FILE;
90 my $OUTFILE;
92 my %po_files_by_lang = ();
93 my %translations = ();
94 my $iconv = $ENV{"ICONV"} || $ENV{"INTLTOOL_ICONV"} || "/usr/bin/iconv";
95 my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
97 # Use this instead of \w for XML files to handle more possible characters.
98 my $w = "[-A-Za-z0-9._:]";
100 # XML quoted string contents
101 my $q = "[^\\\"]*";
103 ## Check for options.
105 if ($VERSION_ARG)
107 &print_version;
109 elsif ($HELP_ARG)
111 &print_help;
113 elsif ($BA_STYLE_ARG && @ARGV > 2)
115 &utf8_sanity_check;
116 &preparation;
117 &print_message;
118 &ba_merge_translations;
119 &finalize;
121 elsif ($XML_STYLE_ARG && @ARGV > 2)
123 &utf8_sanity_check;
124 &preparation;
125 &print_message;
126 &xml_merge_output;
127 &finalize;
129 elsif ($KEYS_STYLE_ARG && @ARGV > 2)
131 &utf8_sanity_check;
132 &preparation;
133 &print_message;
134 &keys_merge_translations;
135 &finalize;
137 elsif ($DESKTOP_STYLE_ARG && @ARGV > 2)
139 &utf8_sanity_check;
140 &preparation;
141 &print_message;
142 &desktop_merge_translations;
143 &finalize;
145 elsif ($SCHEMAS_STYLE_ARG && @ARGV > 2)
147 &utf8_sanity_check;
148 &preparation;
149 &print_message;
150 &schemas_merge_translations;
151 &finalize;
153 elsif ($RFC822DEB_STYLE_ARG && @ARGV > 2)
155 &preparation;
156 &print_message;
157 &rfc822deb_merge_translations;
158 &finalize;
160 else
162 &print_help;
165 exit;
167 ## Sub for printing release information
168 sub print_version
170 print <<_EOF_;
171 ${PROGRAM} (${PACKAGE}) ${VERSION}
172 Written by Maciej Stachowiak, Darin Adler and Kenneth Christiansen.
174 Copyright (C) 2000-2003 Free Software Foundation, Inc.
175 Copyright (C) 2000-2001 Eazel, Inc.
176 This is free software; see the source for copying conditions. There is NO
177 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
178 _EOF_
179 exit;
182 ## Sub for printing usage information
183 sub print_help
185 print <<_EOF_;
186 Usage: ${PROGRAM} [OPTION]... PO_DIRECTORY FILENAME OUTPUT_FILE
187 Generates an output file that includes some localized attributes from an
188 untranslated source file.
190 Mandatory options: (exactly one must be specified)
191 -b, --ba-style includes translations in the bonobo-activation style
192 -d, --desktop-style includes translations in the desktop style
193 -k, --keys-style includes translations in the keys style
194 -s, --schemas-style includes translations in the schemas style
195 -r, --rfc822deb-style includes translations in the RFC822 style
196 -x, --xml-style includes translations in the standard xml style
198 Other options:
199 -u, --utf8 convert all strings to UTF-8 before merging
200 (default for everything except RFC822 style)
201 -p, --pass-through deprecated, does nothing and issues a warning
202 -m, --multiple-output output one localized file per locale, instead of
203 a single file containing all localized elements
204 -c, --cache=FILE specify cache file name
205 (usually \$top_builddir/po/.intltool-merge-cache)
206 -q, --quiet suppress most messages
207 --help display this help and exit
208 --version output version information and exit
210 Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
211 or send email to <xml-i18n-tools\@gnome.org>.
212 _EOF_
213 exit;
217 ## Sub for printing error messages
218 sub print_error
220 print STDERR "Try `${PROGRAM} --help' for more information.\n";
221 exit;
225 sub print_message
227 print "Merging translations into $OUTFILE.\n" unless $QUIET_ARG;
231 sub preparation
233 $PO_DIR = $ARGV[0];
234 $FILE = $ARGV[1];
235 $OUTFILE = $ARGV[2];
237 &gather_po_files;
238 &get_translation_database;
241 # General-purpose code for looking up translations in .po files
243 sub po_file2lang
245 my ($tmp) = @_;
246 $tmp =~ s/^.*\/(.*)\.po$/$1/;
247 return $tmp;
250 sub gather_po_files
252 for my $po_file (glob "$PO_DIR/*.po") {
253 $po_files_by_lang{po_file2lang($po_file)} = $po_file;
257 sub get_local_charset
259 my ($encoding) = @_;
260 my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "/usr/lib/charset.alias";
262 # seek character encoding aliases in charset.alias (glib)
264 if (open CHARSET_ALIAS, $alias_file)
266 while (<CHARSET_ALIAS>)
268 next if /^\#/;
269 return $1 if (/^\s*([-._a-zA-Z0-9]+)\s+$encoding\b/i)
272 close CHARSET_ALIAS;
275 # if not found, return input string
277 return $encoding;
280 sub get_po_encoding
282 my ($in_po_file) = @_;
283 my $encoding = "";
285 open IN_PO_FILE, $in_po_file or die;
286 while (<IN_PO_FILE>)
288 ## example: "Content-Type: text/plain; charset=ISO-8859-1\n"
289 if (/Content-Type\:.*charset=([-a-zA-Z0-9]+)\\n/)
291 $encoding = $1;
292 last;
295 close IN_PO_FILE;
297 if (!$encoding)
299 print STDERR "Warning: no encoding found in $in_po_file. Assuming ISO-8859-1\n" unless $QUIET_ARG;
300 $encoding = "ISO-8859-1";
303 system ("$iconv -f $encoding -t UTF-8 <$devnull 2>$devnull");
304 if ($?) {
305 $encoding = get_local_charset($encoding);
308 return $encoding
311 sub utf8_sanity_check
313 print STDERR "Warning: option --pass-through has been removed.\n" if $PASS_THROUGH_ARG;
314 $UTF8_ARG = 1;
317 sub get_translation_database
319 if ($cache_file) {
320 &get_cached_translation_database;
321 } else {
322 &create_translation_database;
326 sub get_newest_po_age
328 my $newest_age;
330 foreach my $file (values %po_files_by_lang)
332 my $file_age = -M $file;
333 $newest_age = $file_age if !$newest_age || $file_age < $newest_age;
336 $newest_age = 0 if !$newest_age;
338 return $newest_age;
341 sub create_cache
343 print "Generating and caching the translation database\n" unless $QUIET_ARG;
345 &create_translation_database;
347 open CACHE, ">$cache_file" || die;
348 print CACHE join "\x01", %translations;
349 close CACHE;
352 sub load_cache
354 print "Found cached translation database\n" unless $QUIET_ARG;
356 my $contents;
357 open CACHE, "<$cache_file" || die;
359 local $/;
360 $contents = <CACHE>;
362 close CACHE;
363 %translations = split "\x01", $contents;
366 sub get_cached_translation_database
368 my $cache_file_age = -M $cache_file;
369 if (defined $cache_file_age)
371 if ($cache_file_age <= &get_newest_po_age)
373 &load_cache;
374 return;
376 print "Found too-old cached translation database\n" unless $QUIET_ARG;
379 &create_cache;
382 sub create_translation_database
384 for my $lang (keys %po_files_by_lang)
386 my $po_file = $po_files_by_lang{$lang};
388 if ($UTF8_ARG)
390 my $encoding = get_po_encoding ($po_file);
392 if (lc $encoding eq "utf-8")
394 open PO_FILE, "<$po_file";
396 else
398 print "NOTICE: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
400 open PO_FILE, "$iconv -f $encoding -t UTF-8 $po_file|";
403 else
405 open PO_FILE, "<$po_file";
408 my $nextfuzzy = 0;
409 my $inmsgid = 0;
410 my $inmsgstr = 0;
411 my $msgid = "";
412 my $msgstr = "";
414 while (<PO_FILE>)
416 $nextfuzzy = 1 if /^#, fuzzy/;
418 if (/^msgid "((\\.|[^\\])*)"/ )
420 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
421 $msgid = "";
422 $msgstr = "";
424 if ($nextfuzzy) {
425 $inmsgid = 0;
426 } else {
427 $msgid = unescape_po_string($1);
428 $inmsgid = 1;
430 $inmsgstr = 0;
431 $nextfuzzy = 0;
434 if (/^msgstr "((\\.|[^\\])*)"/)
436 $msgstr = unescape_po_string($1);
437 $inmsgstr = 1;
438 $inmsgid = 0;
441 if (/^"((\\.|[^\\])*)"/)
443 $msgid .= unescape_po_string($1) if $inmsgid;
444 $msgstr .= unescape_po_string($1) if $inmsgstr;
447 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
451 sub finalize
455 sub unescape_one_sequence
457 my ($sequence) = @_;
459 return "\\" if $sequence eq "\\\\";
460 return "\"" if $sequence eq "\\\"";
461 return "\n" if $sequence eq "\\n";
462 return "\r" if $sequence eq "\\r";
463 return "\t" if $sequence eq "\\t";
464 return "\b" if $sequence eq "\\b";
465 return "\f" if $sequence eq "\\f";
466 return "\a" if $sequence eq "\\a";
467 return chr(11) if $sequence eq "\\v"; # vertical tab, see ascii(7)
469 return chr(hex($1)) if ($sequence =~ /\\x([0-9a-fA-F]{2})/);
470 return chr(oct($1)) if ($sequence =~ /\\([0-7]{3})/);
472 # FIXME: Is \0 supported as well? Kenneth and Rodney don't want it, see bug #48489
474 return $sequence;
477 sub unescape_po_string
479 my ($string) = @_;
481 $string =~ s/(\\x[0-9a-fA-F]{2}|\\[0-7]{3}|\\.)/unescape_one_sequence($1)/eg;
483 return $string;
486 ## NOTE: deal with < - &lt; but not > - &gt; because it seems its ok to have
487 ## > in the entity. For further info please look at #84738.
488 sub entity_decode
490 local ($_) = @_;
492 s/&apos;/'/g; # '
493 s/&quot;/"/g; # "
494 s/&amp;/&/g;
495 s/&lt;/</g;
497 return $_;
500 # entity_encode: (string)
502 # Encode the given string to XML format (encode '<' etc).
504 sub entity_encode
506 my ($pre_encoded) = @_;
508 my @list_of_chars = unpack ('C*', $pre_encoded);
510 # with UTF-8 we only encode minimalistic
511 return join ('', map (&entity_encode_int_minimalist, @list_of_chars));
514 sub entity_encode_int_minimalist
516 return "&quot;" if $_ == 34;
517 return "&amp;" if $_ == 38;
518 return "&apos;" if $_ == 39;
519 return "&lt;" if $_ == 60;
520 return chr $_;
523 sub entity_encoded_translation
525 my ($lang, $string) = @_;
527 my $translation = $translations{$lang, $string};
528 return $string if !$translation;
529 return entity_encode ($translation);
532 ## XML (bonobo-activation specific) merge code
534 sub ba_merge_translations
536 my $source;
539 local $/; # slurp mode
540 open INPUT, "<$FILE" or die "can't open $FILE: $!";
541 $source = <INPUT>;
542 close INPUT;
545 open OUTPUT, ">$OUTFILE" or die "can't open $OUTFILE: $!";
546 # Binmode so that selftest works ok if using a native Win32 Perl...
547 binmode (OUTPUT) if $^O eq 'MSWin32';
549 while ($source =~ s|^(.*?)([ \t]*<\s*$w+\s+($w+\s*=\s*"$q"\s*)+/?>)([ \t]*\n)?||s)
551 print OUTPUT $1;
553 my $node = $2 . "\n";
555 my @strings = ();
556 $_ = $node;
557 while (s/(\s)_($w+\s*=\s*"($q)")/$1$2/s) {
558 push @strings, entity_decode($3);
560 print OUTPUT;
562 my %langs;
563 for my $string (@strings)
565 for my $lang (keys %po_files_by_lang)
567 $langs{$lang} = 1 if $translations{$lang, $string};
571 for my $lang (sort keys %langs)
573 $_ = $node;
574 s/(\sname\s*=\s*)"($q)"/$1"$2-$lang"/s;
575 s/(\s)_($w+\s*=\s*")($q)"/$1 . $2 . entity_encoded_translation($lang, $3) . '"'/seg;
576 print OUTPUT;
580 print OUTPUT $source;
582 close OUTPUT;
586 ## XML (non-bonobo-activation) merge code
589 # Process tag attributes
590 # Only parameter is a HASH containing attributes -> values mapping
591 sub getAttributeString
593 my $sub = shift;
594 my $do_translate = shift || 0;
595 my $language = shift || "";
596 my $result = "";
597 my $translate = shift;
598 foreach my $e (reverse(sort(keys %{ $sub }))) {
599 my $key = $e;
600 my $string = $sub->{$e};
601 my $quote = '"';
603 $string =~ s/^[\s]+//;
604 $string =~ s/[\s]+$//;
606 if ($string =~ /^'.*'$/)
608 $quote = "'";
610 $string =~ s/^['"]//g;
611 $string =~ s/['"]$//g;
613 if ($do_translate && $key =~ /^_/) {
614 $key =~ s|^_||g;
615 if ($language) {
616 # Handle translation
617 my $decode_string = entity_decode($string);
618 my $translation = $translations{$language, $decode_string};
619 if ($translation) {
620 $translation = entity_encode($translation);
621 $string = $translation;
623 $$translate = 2;
624 } else {
625 $$translate = 2 if ($translate && (!$$translate)); # watch not to "overwrite" $translate
629 $result .= " $key=$quote$string$quote";
631 return $result;
634 # Returns a translatable string from XML node, it works on contents of every node in XML::Parser tree
635 sub getXMLstring
637 my $ref = shift;
638 my $spacepreserve = shift || 0;
639 my @list = @{ $ref };
640 my $result = "";
642 my $count = scalar(@list);
643 my $attrs = $list[0];
644 my $index = 1;
646 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
647 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
649 while ($index < $count) {
650 my $type = $list[$index];
651 my $content = $list[$index+1];
652 if (! $type ) {
653 # We've got CDATA
654 if ($content) {
655 # lets strip the whitespace here, and *ONLY* here
656 $content =~ s/\s+/ /gs if (!$spacepreserve);
657 $result .= $content;
659 } elsif ( "$type" ne "1" ) {
660 # We've got another element
661 $result .= "<$type";
662 $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
663 if ($content) {
664 my $subresult = getXMLstring($content, $spacepreserve);
665 if ($subresult) {
666 $result .= ">".$subresult . "</$type>";
667 } else {
668 $result .= "/>";
670 } else {
671 $result .= "/>";
674 $index += 2;
676 return $result;
679 # Translate list of nodes if necessary
680 sub translate_subnodes
682 my $fh = shift;
683 my $content = shift;
684 my $language = shift || "";
685 my $singlelang = shift || 0;
686 my $spacepreserve = shift || 0;
688 my @nodes = @{ $content };
690 my $count = scalar(@nodes);
691 my $index = 0;
692 while ($index < $count) {
693 my $type = $nodes[$index];
694 my $rest = $nodes[$index+1];
695 if ($singlelang) {
696 my $oldMO = $MULTIPLE_OUTPUT;
697 $MULTIPLE_OUTPUT = 1;
698 traverse($fh, $type, $rest, $language, $spacepreserve);
699 $MULTIPLE_OUTPUT = $oldMO;
700 } else {
701 traverse($fh, $type, $rest, $language, $spacepreserve);
703 $index += 2;
707 sub isWellFormedXmlFragment
709 my $ret = eval 'require XML::Parser';
710 if(!$ret) {
711 die "You must have XML::Parser installed to run $0\n\n";
714 my $fragment = shift;
715 return 0 if (!$fragment);
717 $fragment = "<root>$fragment</root>";
718 my $xp = new XML::Parser(Style => 'Tree');
719 my $tree = 0;
720 eval { $tree = $xp->parse($fragment); };
721 return $tree;
724 sub traverse
726 my $fh = shift;
727 my $nodename = shift;
728 my $content = shift;
729 my $language = shift || "";
730 my $spacepreserve = shift || 0;
732 if (!$nodename) {
733 if ($content =~ /^[\s]*$/) {
734 $leading_space .= $content;
736 print $fh $content;
737 } else {
738 # element
739 my @all = @{ $content };
740 my $attrs = shift @all;
741 my $translate = 0;
742 my $outattr = getAttributeString($attrs, 1, $language, \$translate);
744 if ($nodename =~ /^_/) {
745 $translate = 1;
746 $nodename =~ s/^_//;
748 my $lookup = '';
750 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
751 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
753 print $fh "<$nodename", $outattr;
754 if ($translate) {
755 $lookup = getXMLstring($content, $spacepreserve);
756 if (!$spacepreserve) {
757 $lookup =~ s/^\s+//s;
758 $lookup =~ s/\s+$//s;
761 if ($lookup || $translate == 2) {
762 my $translation = $translations{$language, $lookup} if isWellFormedXmlFragment($translations{$language, $lookup});
763 if ($MULTIPLE_OUTPUT && ($translation || $translate == 2)) {
764 $translation = $lookup if (!$translation);
765 print $fh " xml:lang=\"", $language, "\"" if $language;
766 print $fh ">";
767 if ($translate == 2) {
768 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
769 } else {
770 print $fh $translation;
772 print $fh "</$nodename>";
774 return; # this means there will be no same translation with xml:lang="$language"...
775 # if we want them both, just remove this "return"
776 } else {
777 print $fh ">";
778 if ($translate == 2) {
779 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
780 } else {
781 print $fh $lookup;
783 print $fh "</$nodename>";
785 } else {
786 print $fh "/>";
789 for my $lang (sort keys %po_files_by_lang) {
790 if ($MULTIPLE_OUTPUT && $lang ne "$language") {
791 next;
793 if ($lang) {
794 # Handle translation
796 my $translate = 0;
797 my $localattrs = getAttributeString($attrs, 1, $lang, \$translate);
798 my $translation = $translations{$lang, $lookup} if isWellFormedXmlFragment($translations{$lang, $lookup});
799 if ($translate && !$translation) {
800 $translation = $lookup;
803 if ($translation || $translate) {
804 print $fh "\n";
805 $leading_space =~ s/.*\n//g;
806 print $fh $leading_space;
807 print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs, ">";
808 if ($translate == 2) {
809 translate_subnodes($fh, \@all, $lang, 1, $spacepreserve);
810 } else {
811 print $fh $translation;
813 print $fh "</$nodename>";
818 } else {
819 my $count = scalar(@all);
820 if ($count > 0) {
821 print $fh ">";
822 my $index = 0;
823 while ($index < $count) {
824 my $type = $all[$index];
825 my $rest = $all[$index+1];
826 traverse($fh, $type, $rest, $language, $spacepreserve);
827 $index += 2;
829 print $fh "</$nodename>";
830 } else {
831 print $fh "/>";
837 sub intltool_tree_comment
839 my $expat = shift;
840 my $data = shift;
841 my $clist = $expat->{Curlist};
842 my $pos = $#$clist;
844 push @$clist, 1 => $data;
847 sub intltool_tree_cdatastart
849 my $expat = shift;
850 my $clist = $expat->{Curlist};
851 my $pos = $#$clist;
853 push @$clist, 0 => $expat->original_string();
856 sub intltool_tree_cdataend
858 my $expat = shift;
859 my $clist = $expat->{Curlist};
860 my $pos = $#$clist;
862 $clist->[$pos] .= $expat->original_string();
865 sub intltool_tree_char
867 my $expat = shift;
868 my $text = shift;
869 my $clist = $expat->{Curlist};
870 my $pos = $#$clist;
872 # Use original_string so that we retain escaped entities
873 # in CDATA sections.
875 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
876 $clist->[$pos] .= $expat->original_string();
877 } else {
878 push @$clist, 0 => $expat->original_string();
882 sub intltool_tree_start
884 my $expat = shift;
885 my $tag = shift;
886 my @origlist = ();
888 # Use original_string so that we retain escaped entities
889 # in attribute values. We must convert the string to an
890 # @origlist array to conform to the structure of the Tree
891 # Style.
893 my @original_array = split /\x/, $expat->original_string();
894 my $source = $expat->original_string();
896 # Remove leading tag.
898 $source =~ s|^\s*<\s*(\S+)||s;
900 # Grab attribute key/value pairs and push onto @origlist array.
902 while ($source)
904 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
906 $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
907 push @origlist, $1;
908 push @origlist, '"' . $2 . '"';
910 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
912 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
913 push @origlist, $1;
914 push @origlist, "'" . $2 . "'";
916 else
918 last;
922 my $ol = [ { @origlist } ];
924 push @{ $expat->{Lists} }, $expat->{Curlist};
925 push @{ $expat->{Curlist} }, $tag => $ol;
926 $expat->{Curlist} = $ol;
929 sub readXml
931 my $filename = shift || return;
932 if(!-f $filename) {
933 die "ERROR Cannot find filename: $filename\n";
936 my $ret = eval 'require XML::Parser';
937 if(!$ret) {
938 die "You must have XML::Parser installed to run $0\n\n";
940 my $xp = new XML::Parser(Style => 'Tree');
941 $xp->setHandlers(Char => \&intltool_tree_char);
942 $xp->setHandlers(Start => \&intltool_tree_start);
943 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
944 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
945 my $tree = $xp->parsefile($filename);
947 # <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
948 # would be:
949 # [foo, [{}, head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar, [{},
950 # 0, "Howdy", ref, [{}]], 0, "do" ] ]
952 return $tree;
955 sub print_header
957 my $infile = shift;
958 my $fh = shift;
959 my $source;
961 if(!-f $infile) {
962 die "ERROR Cannot find filename: $infile\n";
965 print $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n};
967 local $/;
968 open DOCINPUT, "<${FILE}" or die;
969 $source = <DOCINPUT>;
970 close DOCINPUT;
972 if ($source =~ /(<!DOCTYPE.*\[.*\]\s*>)/s)
974 print $fh "$1\n";
976 elsif ($source =~ /(<!DOCTYPE[^>]*>)/s)
978 print $fh "$1\n";
982 sub parseTree
984 my $fh = shift;
985 my $ref = shift;
986 my $language = shift || "";
988 my $name = shift @{ $ref };
989 my $cont = shift @{ $ref };
991 while (!$name || "$name" eq "1") {
992 $name = shift @{ $ref };
993 $cont = shift @{ $ref };
996 my $spacepreserve = 0;
997 my $attrs = @{$cont}[0];
998 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
1000 traverse($fh, $name, $cont, $language, $spacepreserve);
1003 sub xml_merge_output
1005 my $source;
1007 if ($MULTIPLE_OUTPUT) {
1008 for my $lang (sort keys %po_files_by_lang) {
1009 if ( ! -e $lang ) {
1010 mkdir $lang or die "Cannot create subdirectory $lang: $!\n";
1012 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1013 binmode (OUTPUT) if $^O eq 'MSWin32';
1014 my $tree = readXml($FILE);
1015 print_header($FILE, \*OUTPUT);
1016 parseTree(\*OUTPUT, $tree, $lang);
1017 close OUTPUT;
1018 print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1021 open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
1022 binmode (OUTPUT) if $^O eq 'MSWin32';
1023 my $tree = readXml($FILE);
1024 print_header($FILE, \*OUTPUT);
1025 parseTree(\*OUTPUT, $tree);
1026 close OUTPUT;
1027 print "CREATED $OUTFILE\n" unless $QUIET_ARG;
1030 sub keys_merge_translations
1032 open INPUT, "<${FILE}" or die;
1033 open OUTPUT, ">${OUTFILE}" or die;
1034 binmode (OUTPUT) if $^O eq 'MSWin32';
1036 while (<INPUT>)
1038 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1040 my $string = $3;
1042 print OUTPUT;
1044 my $non_translated_line = $_;
1046 for my $lang (sort keys %po_files_by_lang)
1048 my $translation = $translations{$lang, $string};
1049 next if !$translation;
1051 $_ = $non_translated_line;
1052 s/(\w+)=.*/[$lang]$1=$translation/;
1053 print OUTPUT;
1056 else
1058 print OUTPUT;
1062 close OUTPUT;
1063 close INPUT;
1066 sub desktop_merge_translations
1068 open INPUT, "<${FILE}" or die;
1069 open OUTPUT, ">${OUTFILE}" or die;
1070 binmode (OUTPUT) if $^O eq 'MSWin32';
1072 while (<INPUT>)
1074 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1076 my $string = $3;
1078 print OUTPUT;
1080 my $non_translated_line = $_;
1082 for my $lang (sort keys %po_files_by_lang)
1084 my $translation = $translations{$lang, $string};
1085 next if !$translation;
1087 $_ = $non_translated_line;
1088 s/(\w+)=.*/${1}[$lang]=$translation/;
1089 print OUTPUT;
1092 else
1094 print OUTPUT;
1098 close OUTPUT;
1099 close INPUT;
1102 sub schemas_merge_translations
1104 my $source;
1107 local $/; # slurp mode
1108 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1109 $source = <INPUT>;
1110 close INPUT;
1113 open OUTPUT, ">$OUTFILE" or die;
1114 binmode (OUTPUT) if $^O eq 'MSWin32';
1116 # FIXME: support attribute translations
1118 # Empty nodes never need translation, so unmark all of them.
1119 # For example, <_foo/> is just replaced by <foo/>.
1120 $source =~ s|<\s*_($w+)\s*/>|<$1/>|g;
1122 while ($source =~ s/
1123 (.*?)
1124 (\s+)(<locale\ name="C">(\s*)
1125 (<default>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/default>)?(\s*)
1126 (<short>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/short>)?(\s*)
1127 (<long>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/long>)?(\s*)
1128 <\/locale>)
1129 //sx)
1131 print OUTPUT $1;
1133 my $locale_start_spaces = $2 ? $2 : '';
1134 my $default_spaces = $4 ? $4 : '';
1135 my $short_spaces = $7 ? $7 : '';
1136 my $long_spaces = $10 ? $10 : '';
1137 my $locale_end_spaces = $13 ? $13 : '';
1138 my $c_default_block = $3 ? $3 : '';
1139 my $default_string = $6 ? $6 : '';
1140 my $short_string = $9 ? $9 : '';
1141 my $long_string = $12 ? $12 : '';
1143 print OUTPUT "$locale_start_spaces$c_default_block";
1145 $default_string =~ s/\s+/ /g;
1146 $default_string = entity_decode($default_string);
1147 $short_string =~ s/\s+/ /g;
1148 $short_string = entity_decode($short_string);
1149 $long_string =~ s/\s+/ /g;
1150 $long_string = entity_decode($long_string);
1152 for my $lang (sort keys %po_files_by_lang)
1154 my $default_translation = $translations{$lang, $default_string};
1155 my $short_translation = $translations{$lang, $short_string};
1156 my $long_translation = $translations{$lang, $long_string};
1158 next if (!$default_translation && !$short_translation &&
1159 !$long_translation);
1161 print OUTPUT "\n$locale_start_spaces<locale name=\"$lang\">";
1163 print OUTPUT "$default_spaces";
1165 if ($default_translation)
1167 $default_translation = entity_encode($default_translation);
1168 print OUTPUT "<default>$default_translation</default>";
1171 print OUTPUT "$short_spaces";
1173 if ($short_translation)
1175 $short_translation = entity_encode($short_translation);
1176 print OUTPUT "<short>$short_translation</short>";
1179 print OUTPUT "$long_spaces";
1181 if ($long_translation)
1183 $long_translation = entity_encode($long_translation);
1184 print OUTPUT "<long>$long_translation</long>";
1187 print OUTPUT "$locale_end_spaces</locale>";
1191 print OUTPUT $source;
1193 close OUTPUT;
1196 sub rfc822deb_merge_translations
1198 my %encodings = ();
1199 for my $lang (keys %po_files_by_lang) {
1200 $encodings{$lang} = ($UTF8_ARG ? 'UTF-8' : get_po_encoding($po_files_by_lang{$lang}));
1203 my $source;
1205 $Text::Wrap::huge = 'overflow';
1206 $Text::Wrap::break = qr/\n|\s(?=\S)/;
1209 local $/; # slurp mode
1210 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1211 $source = <INPUT>;
1212 close INPUT;
1215 open OUTPUT, ">${OUTFILE}" or die;
1216 binmode (OUTPUT) if $^O eq 'MSWin32';
1218 while ($source =~ /(^|\n+)(_*)([^:\s]+)(:[ \t]*)(.*?)(?=\n[\S\n]|$)/sg)
1220 my $sep = $1;
1221 my $non_translated_line = $3.$4;
1222 my $string = $5;
1223 my $underscore = length($2);
1224 next if $underscore eq 0 && $non_translated_line =~ /^#/;
1225 # Remove [] dummy strings
1226 my $stripped = $string;
1227 $stripped =~ s/\[\s[^\[\]]*\],/,/g if $underscore eq 2;
1228 $stripped =~ s/\[\s[^\[\]]*\]$//;
1229 $non_translated_line .= $stripped;
1231 print OUTPUT $sep.$non_translated_line;
1233 if ($underscore)
1235 my @str_list = rfc822deb_split($underscore, $string);
1237 for my $lang (sort keys %po_files_by_lang)
1239 my $is_translated = 1;
1240 my $str_translated = '';
1241 my $first = 1;
1243 for my $str (@str_list)
1245 my $translation = $translations{$lang, $str};
1247 if (!$translation)
1249 $is_translated = 0;
1250 last;
1253 # $translation may also contain [] dummy
1254 # strings, mostly to indicate an empty string
1255 $translation =~ s/\[\s[^\[\]]*\]$//;
1257 if ($first)
1259 if ($underscore eq 2)
1261 $str_translated .= $translation;
1263 else
1265 $str_translated .=
1266 Text::Tabs::expand($translation) .
1267 "\n";
1270 else
1272 if ($underscore eq 2)
1274 $str_translated .= ', ' . $translation;
1276 else
1278 $str_translated .= Text::Tabs::expand(
1279 Text::Wrap::wrap(' ', ' ', $translation)) .
1280 "\n .\n";
1283 $first = 0;
1285 # To fix some problems with Text::Wrap::wrap
1286 $str_translated =~ s/(\n )+\n/\n .\n/g;
1288 next unless $is_translated;
1290 $str_translated =~ s/\n \.\n$//;
1291 $str_translated =~ s/\s+$//;
1293 $_ = $non_translated_line;
1294 s/^(\w+):\s*.*/$sep${1}-$lang.$encodings{$lang}: $str_translated/s;
1295 print OUTPUT;
1299 print OUTPUT "\n";
1301 close OUTPUT;
1302 close INPUT;
1305 sub rfc822deb_split
1307 # Debian defines a special way to deal with rfc822-style files:
1308 # when a value contain newlines, it consists of
1309 # 1. a short form (first line)
1310 # 2. a long description, all lines begin with a space,
1311 # and paragraphs are separated by a single dot on a line
1312 # This routine returns an array of all paragraphs, and reformat
1313 # them.
1314 # When first argument is 2, the string is a comma separated list of
1315 # values.
1316 my $type = shift;
1317 my $text = shift;
1318 $text =~ s/^[ \t]//mg;
1319 return (split(/, */, $text, 0)) if $type ne 1;
1320 return ($text) if $text !~ /\n/;
1322 $text =~ s/([^\n]*)\n//;
1323 my @list = ($1);
1324 my $str = '';
1326 for my $line (split (/\n/, $text))
1328 chomp $line;
1329 if ($line =~ /^\.\s*$/)
1331 # New paragraph
1332 $str =~ s/\s*$//;
1333 push(@list, $str);
1334 $str = '';
1336 elsif ($line =~ /^\s/)
1338 # Line which must not be reformatted
1339 $str .= "\n" if length ($str) && $str !~ /\n$/;
1340 $line =~ s/\s+$//;
1341 $str .= $line."\n";
1343 else
1345 # Continuation line, remove newline
1346 $str .= " " if length ($str) && $str !~ /\n$/;
1347 $str .= $line;
1351 $str =~ s/\s*$//;
1352 push(@list, $str) if length ($str);
1354 return @list;