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.36.1";
46 my $must_end_tag = -1;
48 my $translation_depth = -1;
51 my @translation_strings = ();
52 my $leading_space = "";
54 ## Scalars used by the option stuff
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 $QUOTED_STYLE_ARG = 0;
65 my $PASS_THROUGH_ARG = 0;
67 my $MULTIPLE_OUTPUT = 0;
74 "version" => \$VERSION_ARG,
75 "quiet|q" => \$QUIET_ARG,
76 "oaf-style|o" => \$BA_STYLE_ARG, ## for compatibility
77 "ba-style|b" => \$BA_STYLE_ARG,
78 "xml-style|x" => \$XML_STYLE_ARG,
79 "keys-style|k" => \$KEYS_STYLE_ARG,
80 "desktop-style|d" => \$DESKTOP_STYLE_ARG,
81 "schemas-style|s" => \$SCHEMAS_STYLE_ARG,
82 "rfc822deb-style|r" => \$RFC822DEB_STYLE_ARG,
83 "quoted-style" => \$QUOTED_STYLE_ARG,
84 "pass-through|p" => \$PASS_THROUGH_ARG,
85 "utf8|u" => \$UTF8_ARG,
86 "multiple-output|m" => \$MULTIPLE_OUTPUT,
87 "cache|c=s" => \$cache_file
94 my %po_files_by_lang = ();
95 my %translations = ();
96 my $iconv = $ENV{"ICONV"} || "iconv";
97 my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
102 # If either a file exists, or when run it returns 0 exit status
103 return 1 if ((-x $file) or (system("$file -l >$devnull") == 0));
107 if (! isProgramInPath ("$iconv"))
109 print STDERR " *** iconv is not found on this system!\n".
110 " *** Without it, intltool-merge can not convert encodings.\n";
114 # Use this instead of \w for XML files to handle more possible characters.
115 my $w = "[-A-Za-z0-9._:]";
117 # XML quoted string contents
120 ## Check for options.
130 elsif ($BA_STYLE_ARG && @ARGV > 2)
135 &ba_merge_translations;
138 elsif ($XML_STYLE_ARG && @ARGV > 2)
146 elsif ($KEYS_STYLE_ARG && @ARGV > 2)
151 &keys_merge_translations;
154 elsif ($DESKTOP_STYLE_ARG && @ARGV > 2)
159 &desktop_merge_translations;
162 elsif ($SCHEMAS_STYLE_ARG && @ARGV > 2)
167 &schemas_merge_translations;
170 elsif ($RFC822DEB_STYLE_ARG && @ARGV > 2)
174 &rfc822deb_merge_translations;
177 elsif ($QUOTED_STYLE_ARG && @ARGV > 2)
182 "ed_merge_translations;
192 ## Sub for printing release information
196 ${PROGRAM} (${PACKAGE}) ${VERSION}
197 Written by Maciej Stachowiak, Darin Adler and Kenneth Christiansen.
199 Copyright (C) 2000-2003 Free Software Foundation, Inc.
200 Copyright (C) 2000-2001 Eazel, Inc.
201 This is free software; see the source for copying conditions. There is NO
202 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
207 ## Sub for printing usage information
211 Usage: ${PROGRAM} [OPTION]... PO_DIRECTORY FILENAME OUTPUT_FILE
212 Generates an output file that includes some localized attributes from an
213 untranslated source file.
215 Mandatory options: (exactly one must be specified)
216 -b, --ba-style includes translations in the bonobo-activation style
217 -d, --desktop-style includes translations in the desktop style
218 -k, --keys-style includes translations in the keys style
219 -s, --schemas-style includes translations in the schemas style
220 -r, --rfc822deb-style includes translations in the RFC822 style
221 --quoted-style includes translations in the quoted string style
222 -x, --xml-style includes translations in the standard xml style
225 -u, --utf8 convert all strings to UTF-8 before merging
226 (default for everything except RFC822 style)
227 -p, --pass-through deprecated, does nothing and issues a warning
228 -m, --multiple-output output one localized file per locale, instead of
229 a single file containing all localized elements
230 -c, --cache=FILE specify cache file name
231 (usually \$top_builddir/po/.intltool-merge-cache)
232 -q, --quiet suppress most messages
233 --help display this help and exit
234 --version output version information and exit
236 Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
237 or send email to <xml-i18n-tools\@gnome.org>.
243 ## Sub for printing error messages
246 print STDERR "Try `${PROGRAM} --help' for more information.\n";
253 print "Merging translations into $OUTFILE.\n" unless $QUIET_ARG;
264 &get_translation_database;
267 # General-purpose code for looking up translations in .po files
272 $tmp =~ s/^.*\/(.*)\.po$/$1/;
278 if (my $linguas = $ENV{"LINGUAS"})
280 for my $lang (split / /, $linguas) {
281 my $po_file = $PO_DIR . "/" . $lang . ".po";
283 $po_files_by_lang{$lang} = $po_file;
289 if (open LINGUAS_FILE, "$PO_DIR/LINGUAS")
291 while (<LINGUAS_FILE>)
298 my $po_file = $PO_DIR . "/" . $lang . ".po";
300 $po_files_by_lang{$lang} = $po_file;
309 for my $po_file (glob "$PO_DIR/*.po") {
310 $po_files_by_lang{po_file2lang($po_file)} = $po_file;
316 sub get_local_charset
319 my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "@INTLTOOL_LIBDIR@/charset.alias";
321 # seek character encoding aliases in charset.alias (glib)
323 if (open CHARSET_ALIAS, $alias_file)
325 while (<CHARSET_ALIAS>)
328 return $1 if (/^\s*([-._a-zA-Z0-9]+)\s+$encoding\b/i)
334 # if not found, return input string
341 my ($in_po_file) = @_;
344 open IN_PO_FILE, $in_po_file or die;
347 ## example: "Content-Type: text/plain; charset=ISO-8859-1\n"
348 if (/Content-Type\:.*charset=([-a-zA-Z0-9]+)\\n/)
358 print STDERR "Warning: no encoding found in $in_po_file. Assuming ISO-8859-1\n" unless $QUIET_ARG;
359 $encoding = "ISO-8859-1";
362 system ("$iconv -f $encoding -t UTF-8 <$devnull 2>$devnull");
364 $encoding = get_local_charset($encoding);
370 sub utf8_sanity_check
372 print STDERR "Warning: option --pass-through has been removed.\n" if $PASS_THROUGH_ARG;
376 sub get_translation_database
379 &get_cached_translation_database;
381 &create_translation_database;
385 sub get_newest_po_age
389 foreach my $file (values %po_files_by_lang)
391 my $file_age = -M $file;
392 $newest_age = $file_age if !$newest_age || $file_age < $newest_age;
395 $newest_age = 0 if !$newest_age;
402 print "Generating and caching the translation database\n" unless $QUIET_ARG;
404 &create_translation_database;
406 open CACHE, ">$cache_file" || die;
407 print CACHE join "\x01", %translations;
413 print "Found cached translation database\n" unless $QUIET_ARG;
416 open CACHE, "<$cache_file" || die;
422 %translations = split "\x01", $contents;
425 sub get_cached_translation_database
427 my $cache_file_age = -M $cache_file;
428 if (defined $cache_file_age)
430 if ($cache_file_age <= &get_newest_po_age)
435 print "Found too-old cached translation database\n" unless $QUIET_ARG;
441 sub create_translation_database
443 for my $lang (keys %po_files_by_lang)
445 my $po_file = $po_files_by_lang{$lang};
449 my $encoding = get_po_encoding ($po_file);
451 if (lc $encoding eq "utf-8")
453 open PO_FILE, "<$po_file";
457 print "NOTICE: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
459 open PO_FILE, "$iconv -f $encoding -t UTF-8 $po_file|";
464 open PO_FILE, "<$po_file";
475 $nextfuzzy = 1 if /^#, fuzzy/;
477 if (/^msgid "((\\.|[^\\]+)*)"/ )
479 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
486 $msgid = unescape_po_string($1);
493 if (/^msgstr "((\\.|[^\\]+)*)"/)
495 $msgstr = unescape_po_string($1);
500 if (/^"((\\.|[^\\]+)*)"/)
502 $msgid .= unescape_po_string($1) if $inmsgid;
503 $msgstr .= unescape_po_string($1) if $inmsgstr;
506 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
514 sub unescape_one_sequence
518 return "\\" if $sequence eq "\\\\";
519 return "\"" if $sequence eq "\\\"";
520 return "\n" if $sequence eq "\\n";
521 return "\r" if $sequence eq "\\r";
522 return "\t" if $sequence eq "\\t";
523 return "\b" if $sequence eq "\\b";
524 return "\f" if $sequence eq "\\f";
525 return "\a" if $sequence eq "\\a";
526 return chr(11) if $sequence eq "\\v"; # vertical tab, see ascii(7)
528 return chr(hex($1)) if ($sequence =~ /\\x([0-9a-fA-F]{2})/);
529 return chr(oct($1)) if ($sequence =~ /\\([0-7]{3})/);
531 # FIXME: Is \0 supported as well? Kenneth and Rodney don't want it, see bug #48489
536 sub unescape_po_string
540 $string =~ s/(\\x[0-9a-fA-F]{2}|\\[0-7]{3}|\\.)/unescape_one_sequence($1)/eg;
558 # entity_encode: (string)
560 # Encode the given string to XML format (encode '<' etc).
564 my ($pre_encoded) = @_;
566 my @list_of_chars = unpack ('C*', $pre_encoded);
568 # with UTF-8 we only encode minimalistic
569 return join ('', map (&entity_encode_int_minimalist, @list_of_chars));
572 sub entity_encode_int_minimalist
574 return """ if $_ == 34;
575 return "&" if $_ == 38;
576 return "'" if $_ == 39;
577 return "<" if $_ == 60;
581 sub entity_encoded_translation
583 my ($lang, $string) = @_;
585 my $translation = $translations{$lang, $string};
586 return $string if !$translation;
587 return entity_encode ($translation);
590 ## XML (bonobo-activation specific) merge code
592 sub ba_merge_translations
597 local $/; # slurp mode
598 open INPUT, "<$FILE" or die "can't open $FILE: $!";
603 open OUTPUT, ">$OUTFILE" or die "can't open $OUTFILE: $!";
604 # Binmode so that selftest works ok if using a native Win32 Perl...
605 binmode (OUTPUT) if $^O eq 'MSWin32';
607 while ($source =~ s|^(.*?)([ \t]*<\s*$w+\s+($w+\s*=\s*"$q"\s*)+/?>)([ \t]*\n)?||s)
611 my $node = $2 . "\n";
615 while (s/(\s)_($w+\s*=\s*"($q)")/$1$2/s) {
616 push @strings, entity_decode($3);
621 for my $string (@strings)
623 for my $lang (keys %po_files_by_lang)
625 $langs{$lang} = 1 if $translations{$lang, $string};
629 for my $lang (sort keys %langs)
632 s/(\sname\s*=\s*)"($q)"/$1"$2-$lang"/s;
633 s/(\s)_($w+\s*=\s*")($q)"/$1 . $2 . entity_encoded_translation($lang, $3) . '"'/seg;
638 print OUTPUT $source;
644 ## XML (non-bonobo-activation) merge code
647 # Process tag attributes
648 # Only parameter is a HASH containing attributes -> values mapping
649 sub getAttributeString
652 my $do_translate = shift || 0;
653 my $language = shift || "";
655 my $translate = shift;
656 foreach my $e (reverse(sort(keys %{ $sub }))) {
658 my $string = $sub->{$e};
661 $string =~ s/^[\s]+//;
662 $string =~ s/[\s]+$//;
664 if ($string =~ /^'.*'$/)
668 $string =~ s/^['"]//g;
669 $string =~ s/['"]$//g;
671 if ($do_translate && $key =~ /^_/) {
675 my $decode_string = entity_decode($string);
676 my $translation = $translations{$language, $decode_string};
678 $translation = entity_encode($translation);
679 $string = $translation;
683 $$translate = 2 if ($translate && (!$$translate)); # watch not to "overwrite" $translate
687 $result .= " $key=$quote$string$quote";
692 # Returns a translatable string from XML node, it works on contents of every node in XML::Parser tree
696 my $spacepreserve = shift || 0;
697 my @list = @{ $ref };
700 my $count = scalar(@list);
701 my $attrs = $list[0];
704 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
705 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
707 while ($index < $count) {
708 my $type = $list[$index];
709 my $content = $list[$index+1];
713 # lets strip the whitespace here, and *ONLY* here
714 $content =~ s/\s+/ /gs if (!$spacepreserve);
717 } elsif ( "$type" ne "1" ) {
718 # We've got another element
720 $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
722 my $subresult = getXMLstring($content, $spacepreserve);
724 $result .= ">".$subresult . "</$type>";
737 # Translate list of nodes if necessary
738 sub translate_subnodes
742 my $language = shift || "";
743 my $singlelang = shift || 0;
744 my $spacepreserve = shift || 0;
746 my @nodes = @{ $content };
748 my $count = scalar(@nodes);
750 while ($index < $count) {
751 my $type = $nodes[$index];
752 my $rest = $nodes[$index+1];
754 my $oldMO = $MULTIPLE_OUTPUT;
755 $MULTIPLE_OUTPUT = 1;
756 traverse($fh, $type, $rest, $language, $spacepreserve);
757 $MULTIPLE_OUTPUT = $oldMO;
759 traverse($fh, $type, $rest, $language, $spacepreserve);
765 sub isWellFormedXmlFragment
767 my $ret = eval 'require XML::Parser';
769 die "You must have XML::Parser installed to run $0\n\n";
772 my $fragment = shift;
773 return 0 if (!$fragment);
775 $fragment = "<root>$fragment</root>";
776 my $xp = new XML::Parser(Style => 'Tree');
778 eval { $tree = $xp->parse($fragment); };
785 my $nodename = shift;
787 my $language = shift || "";
788 my $spacepreserve = shift || 0;
791 if ($content =~ /^[\s]*$/) {
792 $leading_space .= $content;
797 my @all = @{ $content };
798 my $attrs = shift @all;
800 my $outattr = getAttributeString($attrs, 1, $language, \$translate);
802 if ($nodename =~ /^_/) {
808 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
809 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
811 print $fh "<$nodename", $outattr;
813 $lookup = getXMLstring($content, $spacepreserve);
814 if (!$spacepreserve) {
815 $lookup =~ s/^\s+//s;
816 $lookup =~ s/\s+$//s;
819 if ($lookup || $translate == 2) {
820 my $translation = $translations{$language, $lookup} if isWellFormedXmlFragment($translations{$language, $lookup});
821 if ($MULTIPLE_OUTPUT && ($translation || $translate == 2)) {
822 $translation = $lookup if (!$translation);
823 print $fh " xml:lang=\"", $language, "\"" if $language;
825 if ($translate == 2) {
826 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
828 print $fh $translation;
830 print $fh "</$nodename>";
832 return; # this means there will be no same translation with xml:lang="$language"...
833 # if we want them both, just remove this "return"
836 if ($translate == 2) {
837 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
841 print $fh "</$nodename>";
847 for my $lang (sort keys %po_files_by_lang) {
848 if ($MULTIPLE_OUTPUT && $lang ne "$language") {
855 my $localattrs = getAttributeString($attrs, 1, $lang, \$translate);
856 my $translation = $translations{$lang, $lookup} if isWellFormedXmlFragment($translations{$lang, $lookup});
857 if ($translate && !$translation) {
858 $translation = $lookup;
861 if ($translation || $translate) {
863 $leading_space =~ s/.*\n//g;
864 print $fh $leading_space;
865 print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs, ">";
866 if ($translate == 2) {
867 translate_subnodes($fh, \@all, $lang, 1, $spacepreserve);
869 print $fh $translation;
871 print $fh "</$nodename>";
877 my $count = scalar(@all);
881 while ($index < $count) {
882 my $type = $all[$index];
883 my $rest = $all[$index+1];
884 traverse($fh, $type, $rest, $language, $spacepreserve);
887 print $fh "</$nodename>";
895 sub intltool_tree_comment
899 my $clist = $expat->{Curlist};
902 push @$clist, 1 => $data;
905 sub intltool_tree_cdatastart
908 my $clist = $expat->{Curlist};
911 push @$clist, 0 => $expat->original_string();
914 sub intltool_tree_cdataend
917 my $clist = $expat->{Curlist};
920 $clist->[$pos] .= $expat->original_string();
923 sub intltool_tree_char
927 my $clist = $expat->{Curlist};
930 # Use original_string so that we retain escaped entities
933 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
934 $clist->[$pos] .= $expat->original_string();
936 push @$clist, 0 => $expat->original_string();
940 sub intltool_tree_start
946 # Use original_string so that we retain escaped entities
947 # in attribute values. We must convert the string to an
948 # @origlist array to conform to the structure of the Tree
951 my @original_array = split /\x/, $expat->original_string();
952 my $source = $expat->original_string();
954 # Remove leading tag.
956 $source =~ s|^\s*<\s*(\S+)||s;
958 # Grab attribute key/value pairs and push onto @origlist array.
962 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
964 $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
966 push @origlist, '"' . $2 . '"';
968 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
970 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
972 push @origlist, "'" . $2 . "'";
980 my $ol = [ { @origlist } ];
982 push @{ $expat->{Lists} }, $expat->{Curlist};
983 push @{ $expat->{Curlist} }, $tag => $ol;
984 $expat->{Curlist} = $ol;
989 my $filename = shift || return;
991 die "ERROR Cannot find filename: $filename\n";
994 my $ret = eval 'require XML::Parser';
996 die "You must have XML::Parser installed to run $0\n\n";
998 my $xp = new XML::Parser(Style => 'Tree');
999 $xp->setHandlers(Char => \&intltool_tree_char);
1000 $xp->setHandlers(Start => \&intltool_tree_start);
1001 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
1002 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
1003 my $tree = $xp->parsefile($filename);
1005 # <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
1007 # [foo, [{}, head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar, [{},
1008 # 0, "Howdy", ref, [{}]], 0, "do" ] ]
1020 die "ERROR Cannot find filename: $infile\n";
1023 print $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n};
1026 open DOCINPUT, "<${FILE}" or die;
1027 $source = <DOCINPUT>;
1030 if ($source =~ /(<!DOCTYPE.*\[.*\]\s*>)/s)
1034 elsif ($source =~ /(<!DOCTYPE[^>]*>)/s)
1044 my $language = shift || "";
1046 my $name = shift @{ $ref };
1047 my $cont = shift @{ $ref };
1049 while (!$name || "$name" eq "1") {
1050 $name = shift @{ $ref };
1051 $cont = shift @{ $ref };
1054 my $spacepreserve = 0;
1055 my $attrs = @{$cont}[0];
1056 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
1058 traverse($fh, $name, $cont, $language, $spacepreserve);
1061 sub xml_merge_output
1065 if ($MULTIPLE_OUTPUT) {
1066 for my $lang (sort keys %po_files_by_lang) {
1068 mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1070 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1071 binmode (OUTPUT) if $^O eq 'MSWin32';
1072 my $tree = readXml($FILE);
1073 print_header($FILE, \*OUTPUT);
1074 parseTree(\*OUTPUT, $tree, $lang);
1076 print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1079 open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
1080 binmode (OUTPUT) if $^O eq 'MSWin32';
1081 my $tree = readXml($FILE);
1082 print_header($FILE, \*OUTPUT);
1083 parseTree(\*OUTPUT, $tree);
1085 print "CREATED $OUTFILE\n" unless $QUIET_ARG;
1088 sub keys_merge_translations
1090 open INPUT, "<${FILE}" or die;
1091 open OUTPUT, ">${OUTFILE}" or die;
1092 binmode (OUTPUT) if $^O eq 'MSWin32';
1096 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1102 my $non_translated_line = $_;
1104 for my $lang (sort keys %po_files_by_lang)
1106 my $translation = $translations{$lang, $string};
1107 next if !$translation;
1109 $_ = $non_translated_line;
1110 s/(\w+)=.*/[$lang]$1=$translation/;
1124 sub desktop_merge_translations
1126 open INPUT, "<${FILE}" or die;
1127 open OUTPUT, ">${OUTFILE}" or die;
1128 binmode (OUTPUT) if $^O eq 'MSWin32';
1132 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1138 my $non_translated_line = $_;
1140 for my $lang (sort keys %po_files_by_lang)
1142 my $translation = $translations{$lang, $string};
1143 next if !$translation;
1145 $_ = $non_translated_line;
1146 s/(\w+)=.*/${1}[$lang]=$translation/;
1160 sub schemas_merge_translations
1165 local $/; # slurp mode
1166 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1171 open OUTPUT, ">$OUTFILE" or die;
1172 binmode (OUTPUT) if $^O eq 'MSWin32';
1174 # FIXME: support attribute translations
1176 # Empty nodes never need translation, so unmark all of them.
1177 # For example, <_foo/> is just replaced by <foo/>.
1178 $source =~ s|<\s*_($w+)\s*/>|<$1/>|g;
1180 while ($source =~ s/
1182 (\s+)(<locale\ name="C">(\s*)
1183 (<default>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/default>)?(\s*)
1184 (<short>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/short>)?(\s*)
1185 (<long>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/long>)?(\s*)
1191 my $locale_start_spaces = $2 ? $2 : '';
1192 my $default_spaces = $4 ? $4 : '';
1193 my $short_spaces = $7 ? $7 : '';
1194 my $long_spaces = $10 ? $10 : '';
1195 my $locale_end_spaces = $13 ? $13 : '';
1196 my $c_default_block = $3 ? $3 : '';
1197 my $default_string = $6 ? $6 : '';
1198 my $short_string = $9 ? $9 : '';
1199 my $long_string = $12 ? $12 : '';
1201 print OUTPUT "$locale_start_spaces$c_default_block";
1203 $default_string =~ s/\s+/ /g;
1204 $default_string = entity_decode($default_string);
1205 $short_string =~ s/\s+/ /g;
1206 $short_string = entity_decode($short_string);
1207 $long_string =~ s/\s+/ /g;
1208 $long_string = entity_decode($long_string);
1210 for my $lang (sort keys %po_files_by_lang)
1212 my $default_translation = $translations{$lang, $default_string};
1213 my $short_translation = $translations{$lang, $short_string};
1214 my $long_translation = $translations{$lang, $long_string};
1216 next if (!$default_translation && !$short_translation &&
1217 !$long_translation);
1219 print OUTPUT "\n$locale_start_spaces<locale name=\"$lang\">";
1221 print OUTPUT "$default_spaces";
1223 if ($default_translation)
1225 $default_translation = entity_encode($default_translation);
1226 print OUTPUT "<default>$default_translation</default>";
1229 print OUTPUT "$short_spaces";
1231 if ($short_translation)
1233 $short_translation = entity_encode($short_translation);
1234 print OUTPUT "<short>$short_translation</short>";
1237 print OUTPUT "$long_spaces";
1239 if ($long_translation)
1241 $long_translation = entity_encode($long_translation);
1242 print OUTPUT "<long>$long_translation</long>";
1245 print OUTPUT "$locale_end_spaces</locale>";
1249 print OUTPUT $source;
1254 sub rfc822deb_merge_translations
1257 for my $lang (keys %po_files_by_lang) {
1258 $encodings{$lang} = ($UTF8_ARG ? 'UTF-8' : get_po_encoding($po_files_by_lang{$lang}));
1263 $Text::Wrap::huge = 'overflow';
1264 $Text::Wrap::break = qr/\n|\s(?=\S)/;
1267 local $/; # slurp mode
1268 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1273 open OUTPUT, ">${OUTFILE}" or die;
1274 binmode (OUTPUT) if $^O eq 'MSWin32';
1276 while ($source =~ /(^|\n+)(_*)([^:\s]+)(:[ \t]*)(.*?)(?=\n[\S\n]|$)/sg)
1279 my $non_translated_line = $3.$4;
1281 my $underscore = length($2);
1282 next if $underscore eq 0 && $non_translated_line =~ /^#/;
1283 # Remove [] dummy strings
1284 my $stripped = $string;
1285 $stripped =~ s/\[\s[^\[\]]*\],/,/g if $underscore eq 2;
1286 $stripped =~ s/\[\s[^\[\]]*\]$//;
1287 $non_translated_line .= $stripped;
1289 print OUTPUT $sep.$non_translated_line;
1293 my @str_list = rfc822deb_split($underscore, $string);
1295 for my $lang (sort keys %po_files_by_lang)
1297 my $is_translated = 1;
1298 my $str_translated = '';
1301 for my $str (@str_list)
1303 my $translation = $translations{$lang, $str};
1311 # $translation may also contain [] dummy
1312 # strings, mostly to indicate an empty string
1313 $translation =~ s/\[\s[^\[\]]*\]$//;
1317 if ($underscore eq 2)
1319 $str_translated .= $translation;
1324 Text::Tabs::expand($translation) .
1330 if ($underscore eq 2)
1332 $str_translated .= ', ' . $translation;
1336 $str_translated .= Text::Tabs::expand(
1337 Text::Wrap::wrap(' ', ' ', $translation)) .
1343 # To fix some problems with Text::Wrap::wrap
1344 $str_translated =~ s/(\n )+\n/\n .\n/g;
1346 next unless $is_translated;
1348 $str_translated =~ s/\n \.\n$//;
1349 $str_translated =~ s/\s+$//;
1351 $_ = $non_translated_line;
1352 s/^(\w+):\s*.*/$sep${1}-$lang.$encodings{$lang}: $str_translated/s;
1365 # Debian defines a special way to deal with rfc822-style files:
1366 # when a value contain newlines, it consists of
1367 # 1. a short form (first line)
1368 # 2. a long description, all lines begin with a space,
1369 # and paragraphs are separated by a single dot on a line
1370 # This routine returns an array of all paragraphs, and reformat
1372 # When first argument is 2, the string is a comma separated list of
1376 $text =~ s/^[ \t]//mg;
1377 return (split(/, */, $text, 0)) if $type ne 1;
1378 return ($text) if $text !~ /\n/;
1380 $text =~ s/([^\n]*)\n//;
1384 for my $line (split (/\n/, $text))
1387 if ($line =~ /^\.\s*$/)
1394 elsif ($line =~ /^\s/)
1396 # Line which must not be reformatted
1397 $str .= "\n" if length ($str) && $str !~ /\n$/;
1403 # Continuation line, remove newline
1404 $str .= " " if length ($str) && $str !~ /\n$/;
1410 push(@list, $str) if length ($str);
1415 sub quoted_translation
1417 my ($lang, $string) = @_;
1419 $string =~ s/\\\"/\"/g;
1421 my $translation = $translations{$lang, $string};
1422 $translation = $string if !$translation;
1424 $translation =~ s/\"/\\\"/g;
1428 sub quoted_merge_translations
1430 if (!$MULTIPLE_OUTPUT) {
1431 print "Quoted only supports Multiple Output.\n";
1435 for my $lang (sort keys %po_files_by_lang) {
1437 mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1439 open INPUT, "<${FILE}" or die;
1440 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1441 binmode (OUTPUT) if $^O eq 'MSWin32';
1444 s/\"(([^\"]|\\\")*[^\\\"])\"/"\"" . "ed_translation($lang, $1) . "\""/ge;