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.2";
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>)
295 if (/([-a-zA-Z_@.]+)\n/)
299 my $po_file = $PO_DIR . "/" . $lang . ".po";
301 $po_files_by_lang{$lang} = $po_file;
310 for my $po_file (glob "$PO_DIR/*.po") {
311 $po_files_by_lang{po_file2lang($po_file)} = $po_file;
317 sub get_local_charset
320 my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "@INTLTOOL_LIBDIR@/charset.alias";
322 # seek character encoding aliases in charset.alias (glib)
324 if (open CHARSET_ALIAS, $alias_file)
326 while (<CHARSET_ALIAS>)
329 return $1 if (/^\s*([-._a-zA-Z0-9]+)\s+$encoding\b/i)
335 # if not found, return input string
342 my ($in_po_file) = @_;
345 open IN_PO_FILE, $in_po_file or die;
348 ## example: "Content-Type: text/plain; charset=ISO-8859-1\n"
349 if (/Content-Type\:.*charset=([-a-zA-Z0-9]+)\\n/)
359 print STDERR "Warning: no encoding found in $in_po_file. Assuming ISO-8859-1\n" unless $QUIET_ARG;
360 $encoding = "ISO-8859-1";
363 system ("$iconv -f $encoding -t UTF-8 <$devnull 2>$devnull");
365 $encoding = get_local_charset($encoding);
371 sub utf8_sanity_check
373 print STDERR "Warning: option --pass-through has been removed.\n" if $PASS_THROUGH_ARG;
377 sub get_translation_database
380 &get_cached_translation_database;
382 &create_translation_database;
386 sub get_newest_po_age
390 foreach my $file (values %po_files_by_lang)
392 my $file_age = -M $file;
393 $newest_age = $file_age if !$newest_age || $file_age < $newest_age;
396 $newest_age = 0 if !$newest_age;
403 print "Generating and caching the translation database\n" unless $QUIET_ARG;
405 &create_translation_database;
407 open CACHE, ">$cache_file" || die;
408 print CACHE join "\x01", %translations;
414 print "Found cached translation database\n" unless $QUIET_ARG;
417 open CACHE, "<$cache_file" || die;
423 %translations = split "\x01", $contents;
426 sub get_cached_translation_database
428 my $cache_file_age = -M $cache_file;
429 if (defined $cache_file_age)
431 if ($cache_file_age <= &get_newest_po_age)
436 print "Found too-old cached translation database\n" unless $QUIET_ARG;
442 sub create_translation_database
444 for my $lang (keys %po_files_by_lang)
446 my $po_file = $po_files_by_lang{$lang};
450 my $encoding = get_po_encoding ($po_file);
452 if (lc $encoding eq "utf-8")
454 open PO_FILE, "<$po_file";
458 print "NOTICE: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
460 open PO_FILE, "$iconv -f $encoding -t UTF-8 $po_file|";
465 open PO_FILE, "<$po_file";
476 $nextfuzzy = 1 if /^#, fuzzy/;
478 if (/^msgid "((\\.|[^\\]+)*)"/ )
480 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
487 $msgid = unescape_po_string($1);
494 if (/^msgstr "((\\.|[^\\]+)*)"/)
496 $msgstr = unescape_po_string($1);
501 if (/^"((\\.|[^\\]+)*)"/)
503 $msgid .= unescape_po_string($1) if $inmsgid;
504 $msgstr .= unescape_po_string($1) if $inmsgstr;
507 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
515 sub unescape_one_sequence
519 return "\\" if $sequence eq "\\\\";
520 return "\"" if $sequence eq "\\\"";
521 return "\n" if $sequence eq "\\n";
522 return "\r" if $sequence eq "\\r";
523 return "\t" if $sequence eq "\\t";
524 return "\b" if $sequence eq "\\b";
525 return "\f" if $sequence eq "\\f";
526 return "\a" if $sequence eq "\\a";
527 return chr(11) if $sequence eq "\\v"; # vertical tab, see ascii(7)
529 return chr(hex($1)) if ($sequence =~ /\\x([0-9a-fA-F]{2})/);
530 return chr(oct($1)) if ($sequence =~ /\\([0-7]{3})/);
532 # FIXME: Is \0 supported as well? Kenneth and Rodney don't want it, see bug #48489
537 sub unescape_po_string
541 $string =~ s/(\\x[0-9a-fA-F]{2}|\\[0-7]{3}|\\.)/unescape_one_sequence($1)/eg;
559 # entity_encode: (string)
561 # Encode the given string to XML format (encode '<' etc).
565 my ($pre_encoded) = @_;
567 my @list_of_chars = unpack ('C*', $pre_encoded);
569 # with UTF-8 we only encode minimalistic
570 return join ('', map (&entity_encode_int_minimalist, @list_of_chars));
573 sub entity_encode_int_minimalist
575 return """ if $_ == 34;
576 return "&" if $_ == 38;
577 return "'" if $_ == 39;
578 return "<" if $_ == 60;
582 sub entity_encoded_translation
584 my ($lang, $string) = @_;
586 my $translation = $translations{$lang, $string};
587 return $string if !$translation;
588 return entity_encode ($translation);
591 ## XML (bonobo-activation specific) merge code
593 sub ba_merge_translations
598 local $/; # slurp mode
599 open INPUT, "<$FILE" or die "can't open $FILE: $!";
604 open OUTPUT, ">$OUTFILE" or die "can't open $OUTFILE: $!";
605 # Binmode so that selftest works ok if using a native Win32 Perl...
606 binmode (OUTPUT) if $^O eq 'MSWin32';
608 while ($source =~ s|^(.*?)([ \t]*<\s*$w+\s+($w+\s*=\s*"$q"\s*)+/?>)([ \t]*\n)?||s)
612 my $node = $2 . "\n";
616 while (s/(\s)_($w+\s*=\s*"($q)")/$1$2/s) {
617 push @strings, entity_decode($3);
622 for my $string (@strings)
624 for my $lang (keys %po_files_by_lang)
626 $langs{$lang} = 1 if $translations{$lang, $string};
630 for my $lang (sort keys %langs)
633 s/(\sname\s*=\s*)"($q)"/$1"$2-$lang"/s;
634 s/(\s)_($w+\s*=\s*")($q)"/$1 . $2 . entity_encoded_translation($lang, $3) . '"'/seg;
639 print OUTPUT $source;
645 ## XML (non-bonobo-activation) merge code
648 # Process tag attributes
649 # Only parameter is a HASH containing attributes -> values mapping
650 sub getAttributeString
653 my $do_translate = shift || 0;
654 my $language = shift || "";
656 my $translate = shift;
657 foreach my $e (reverse(sort(keys %{ $sub }))) {
659 my $string = $sub->{$e};
662 $string =~ s/^[\s]+//;
663 $string =~ s/[\s]+$//;
665 if ($string =~ /^'.*'$/)
669 $string =~ s/^['"]//g;
670 $string =~ s/['"]$//g;
672 if ($do_translate && $key =~ /^_/) {
676 my $decode_string = entity_decode($string);
677 my $translation = $translations{$language, $decode_string};
679 $translation = entity_encode($translation);
680 $string = $translation;
684 $$translate = 2 if ($translate && (!$$translate)); # watch not to "overwrite" $translate
688 $result .= " $key=$quote$string$quote";
693 # Returns a translatable string from XML node, it works on contents of every node in XML::Parser tree
697 my $spacepreserve = shift || 0;
698 my @list = @{ $ref };
701 my $count = scalar(@list);
702 my $attrs = $list[0];
705 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
706 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
708 while ($index < $count) {
709 my $type = $list[$index];
710 my $content = $list[$index+1];
714 # lets strip the whitespace here, and *ONLY* here
715 $content =~ s/\s+/ /gs if (!$spacepreserve);
718 } elsif ( "$type" ne "1" ) {
719 # We've got another element
721 $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
723 my $subresult = getXMLstring($content, $spacepreserve);
725 $result .= ">".$subresult . "</$type>";
738 # Translate list of nodes if necessary
739 sub translate_subnodes
743 my $language = shift || "";
744 my $singlelang = shift || 0;
745 my $spacepreserve = shift || 0;
747 my @nodes = @{ $content };
749 my $count = scalar(@nodes);
751 while ($index < $count) {
752 my $type = $nodes[$index];
753 my $rest = $nodes[$index+1];
755 my $oldMO = $MULTIPLE_OUTPUT;
756 $MULTIPLE_OUTPUT = 1;
757 traverse($fh, $type, $rest, $language, $spacepreserve);
758 $MULTIPLE_OUTPUT = $oldMO;
760 traverse($fh, $type, $rest, $language, $spacepreserve);
766 sub isWellFormedXmlFragment
768 my $ret = eval 'require XML::Parser';
770 die "You must have XML::Parser installed to run $0\n\n";
773 my $fragment = shift;
774 return 0 if (!$fragment);
776 $fragment = "<root>$fragment</root>";
777 my $xp = new XML::Parser(Style => 'Tree');
779 eval { $tree = $xp->parse($fragment); };
786 my $nodename = shift;
788 my $language = shift || "";
789 my $spacepreserve = shift || 0;
792 if ($content =~ /^[\s]*$/) {
793 $leading_space .= $content;
798 my @all = @{ $content };
799 my $attrs = shift @all;
801 my $outattr = getAttributeString($attrs, 1, $language, \$translate);
803 if ($nodename =~ /^_/) {
809 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
810 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
812 print $fh "<$nodename", $outattr;
814 $lookup = getXMLstring($content, $spacepreserve);
815 if (!$spacepreserve) {
816 $lookup =~ s/^\s+//s;
817 $lookup =~ s/\s+$//s;
820 if ($lookup || $translate == 2) {
821 my $translation = $translations{$language, $lookup} if isWellFormedXmlFragment($translations{$language, $lookup});
822 if ($MULTIPLE_OUTPUT && ($translation || $translate == 2)) {
823 $translation = $lookup if (!$translation);
824 print $fh " xml:lang=\"", $language, "\"" if $language;
826 if ($translate == 2) {
827 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
829 print $fh $translation;
831 print $fh "</$nodename>";
833 return; # this means there will be no same translation with xml:lang="$language"...
834 # if we want them both, just remove this "return"
837 if ($translate == 2) {
838 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
842 print $fh "</$nodename>";
848 for my $lang (sort keys %po_files_by_lang) {
849 if ($MULTIPLE_OUTPUT && $lang ne "$language") {
856 my $localattrs = getAttributeString($attrs, 1, $lang, \$translate);
857 my $translation = $translations{$lang, $lookup} if isWellFormedXmlFragment($translations{$lang, $lookup});
858 if ($translate && !$translation) {
859 $translation = $lookup;
862 if ($translation || $translate) {
864 $leading_space =~ s/.*\n//g;
865 print $fh $leading_space;
866 print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs, ">";
867 if ($translate == 2) {
868 translate_subnodes($fh, \@all, $lang, 1, $spacepreserve);
870 print $fh $translation;
872 print $fh "</$nodename>";
878 my $count = scalar(@all);
882 while ($index < $count) {
883 my $type = $all[$index];
884 my $rest = $all[$index+1];
885 traverse($fh, $type, $rest, $language, $spacepreserve);
888 print $fh "</$nodename>";
896 sub intltool_tree_comment
900 my $clist = $expat->{Curlist};
903 push @$clist, 1 => $data;
906 sub intltool_tree_cdatastart
909 my $clist = $expat->{Curlist};
912 push @$clist, 0 => $expat->original_string();
915 sub intltool_tree_cdataend
918 my $clist = $expat->{Curlist};
921 $clist->[$pos] .= $expat->original_string();
924 sub intltool_tree_char
928 my $clist = $expat->{Curlist};
931 # Use original_string so that we retain escaped entities
934 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
935 $clist->[$pos] .= $expat->original_string();
937 push @$clist, 0 => $expat->original_string();
941 sub intltool_tree_start
947 # Use original_string so that we retain escaped entities
948 # in attribute values. We must convert the string to an
949 # @origlist array to conform to the structure of the Tree
952 my @original_array = split /\x/, $expat->original_string();
953 my $source = $expat->original_string();
955 # Remove leading tag.
957 $source =~ s|^\s*<\s*(\S+)||s;
959 # Grab attribute key/value pairs and push onto @origlist array.
963 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
965 $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
967 push @origlist, '"' . $2 . '"';
969 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
971 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
973 push @origlist, "'" . $2 . "'";
981 my $ol = [ { @origlist } ];
983 push @{ $expat->{Lists} }, $expat->{Curlist};
984 push @{ $expat->{Curlist} }, $tag => $ol;
985 $expat->{Curlist} = $ol;
990 my $filename = shift || return;
992 die "ERROR Cannot find filename: $filename\n";
995 my $ret = eval 'require XML::Parser';
997 die "You must have XML::Parser installed to run $0\n\n";
999 my $xp = new XML::Parser(Style => 'Tree');
1000 $xp->setHandlers(Char => \&intltool_tree_char);
1001 $xp->setHandlers(Start => \&intltool_tree_start);
1002 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
1003 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
1004 my $tree = $xp->parsefile($filename);
1006 # <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
1008 # [foo, [{}, head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar, [{},
1009 # 0, "Howdy", ref, [{}]], 0, "do" ] ]
1021 die "ERROR Cannot find filename: $infile\n";
1024 print $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n};
1027 open DOCINPUT, "<${FILE}" or die;
1028 $source = <DOCINPUT>;
1031 if ($source =~ /(<!DOCTYPE.*\[.*\]\s*>)/s)
1035 elsif ($source =~ /(<!DOCTYPE[^>]*>)/s)
1045 my $language = shift || "";
1047 my $name = shift @{ $ref };
1048 my $cont = shift @{ $ref };
1050 while (!$name || "$name" eq "1") {
1051 $name = shift @{ $ref };
1052 $cont = shift @{ $ref };
1055 my $spacepreserve = 0;
1056 my $attrs = @{$cont}[0];
1057 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
1059 traverse($fh, $name, $cont, $language, $spacepreserve);
1062 sub xml_merge_output
1066 if ($MULTIPLE_OUTPUT) {
1067 for my $lang (sort keys %po_files_by_lang) {
1069 mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1071 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1072 binmode (OUTPUT) if $^O eq 'MSWin32';
1073 my $tree = readXml($FILE);
1074 print_header($FILE, \*OUTPUT);
1075 parseTree(\*OUTPUT, $tree, $lang);
1077 print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1080 open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
1081 binmode (OUTPUT) if $^O eq 'MSWin32';
1082 my $tree = readXml($FILE);
1083 print_header($FILE, \*OUTPUT);
1084 parseTree(\*OUTPUT, $tree);
1086 print "CREATED $OUTFILE\n" unless $QUIET_ARG;
1089 sub keys_merge_translations
1091 open INPUT, "<${FILE}" or die;
1092 open OUTPUT, ">${OUTFILE}" or die;
1093 binmode (OUTPUT) if $^O eq 'MSWin32';
1097 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1103 my $non_translated_line = $_;
1105 for my $lang (sort keys %po_files_by_lang)
1107 my $translation = $translations{$lang, $string};
1108 next if !$translation;
1110 $_ = $non_translated_line;
1111 s/(\w+)=.*/[$lang]$1=$translation/;
1125 sub desktop_merge_translations
1127 open INPUT, "<${FILE}" or die;
1128 open OUTPUT, ">${OUTFILE}" or die;
1129 binmode (OUTPUT) if $^O eq 'MSWin32';
1133 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1139 my $non_translated_line = $_;
1141 for my $lang (sort keys %po_files_by_lang)
1143 my $translation = $translations{$lang, $string};
1144 next if !$translation;
1146 $_ = $non_translated_line;
1147 s/(\w+)=.*/${1}[$lang]=$translation/;
1161 sub schemas_merge_translations
1166 local $/; # slurp mode
1167 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1172 open OUTPUT, ">$OUTFILE" or die;
1173 binmode (OUTPUT) if $^O eq 'MSWin32';
1175 # FIXME: support attribute translations
1177 # Empty nodes never need translation, so unmark all of them.
1178 # For example, <_foo/> is just replaced by <foo/>.
1179 $source =~ s|<\s*_($w+)\s*/>|<$1/>|g;
1181 while ($source =~ s/
1183 (\s+)(<locale\ name="C">(\s*)
1184 (<default>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/default>)?(\s*)
1185 (<short>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/short>)?(\s*)
1186 (<long>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/long>)?(\s*)
1192 my $locale_start_spaces = $2 ? $2 : '';
1193 my $default_spaces = $4 ? $4 : '';
1194 my $short_spaces = $7 ? $7 : '';
1195 my $long_spaces = $10 ? $10 : '';
1196 my $locale_end_spaces = $13 ? $13 : '';
1197 my $c_default_block = $3 ? $3 : '';
1198 my $default_string = $6 ? $6 : '';
1199 my $short_string = $9 ? $9 : '';
1200 my $long_string = $12 ? $12 : '';
1202 print OUTPUT "$locale_start_spaces$c_default_block";
1204 $default_string =~ s/\s+/ /g;
1205 $default_string = entity_decode($default_string);
1206 $short_string =~ s/\s+/ /g;
1207 $short_string = entity_decode($short_string);
1208 $long_string =~ s/\s+/ /g;
1209 $long_string = entity_decode($long_string);
1211 for my $lang (sort keys %po_files_by_lang)
1213 my $default_translation = $translations{$lang, $default_string};
1214 my $short_translation = $translations{$lang, $short_string};
1215 my $long_translation = $translations{$lang, $long_string};
1217 next if (!$default_translation && !$short_translation &&
1218 !$long_translation);
1220 print OUTPUT "\n$locale_start_spaces<locale name=\"$lang\">";
1222 print OUTPUT "$default_spaces";
1224 if ($default_translation)
1226 $default_translation = entity_encode($default_translation);
1227 print OUTPUT "<default>$default_translation</default>";
1230 print OUTPUT "$short_spaces";
1232 if ($short_translation)
1234 $short_translation = entity_encode($short_translation);
1235 print OUTPUT "<short>$short_translation</short>";
1238 print OUTPUT "$long_spaces";
1240 if ($long_translation)
1242 $long_translation = entity_encode($long_translation);
1243 print OUTPUT "<long>$long_translation</long>";
1246 print OUTPUT "$locale_end_spaces</locale>";
1250 print OUTPUT $source;
1255 sub rfc822deb_merge_translations
1258 for my $lang (keys %po_files_by_lang) {
1259 $encodings{$lang} = ($UTF8_ARG ? 'UTF-8' : get_po_encoding($po_files_by_lang{$lang}));
1264 $Text::Wrap::huge = 'overflow';
1265 $Text::Wrap::break = qr/\n|\s(?=\S)/;
1268 local $/; # slurp mode
1269 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1274 open OUTPUT, ">${OUTFILE}" or die;
1275 binmode (OUTPUT) if $^O eq 'MSWin32';
1277 while ($source =~ /(^|\n+)(_*)([^:\s]+)(:[ \t]*)(.*?)(?=\n[\S\n]|$)/sg)
1280 my $non_translated_line = $3.$4;
1282 my $underscore = length($2);
1283 next if $underscore eq 0 && $non_translated_line =~ /^#/;
1284 # Remove [] dummy strings
1285 my $stripped = $string;
1286 $stripped =~ s/\[\s[^\[\]]*\],/,/g if $underscore eq 2;
1287 $stripped =~ s/\[\s[^\[\]]*\]$//;
1288 $non_translated_line .= $stripped;
1290 print OUTPUT $sep.$non_translated_line;
1294 my @str_list = rfc822deb_split($underscore, $string);
1296 for my $lang (sort keys %po_files_by_lang)
1298 my $is_translated = 1;
1299 my $str_translated = '';
1302 for my $str (@str_list)
1304 my $translation = $translations{$lang, $str};
1312 # $translation may also contain [] dummy
1313 # strings, mostly to indicate an empty string
1314 $translation =~ s/\[\s[^\[\]]*\]$//;
1318 if ($underscore eq 2)
1320 $str_translated .= $translation;
1325 Text::Tabs::expand($translation) .
1331 if ($underscore eq 2)
1333 $str_translated .= ', ' . $translation;
1337 $str_translated .= Text::Tabs::expand(
1338 Text::Wrap::wrap(' ', ' ', $translation)) .
1344 # To fix some problems with Text::Wrap::wrap
1345 $str_translated =~ s/(\n )+\n/\n .\n/g;
1347 next unless $is_translated;
1349 $str_translated =~ s/\n \.\n$//;
1350 $str_translated =~ s/\s+$//;
1352 $_ = $non_translated_line;
1353 s/^(\w+):\s*.*/$sep${1}-$lang.$encodings{$lang}: $str_translated/s;
1366 # Debian defines a special way to deal with rfc822-style files:
1367 # when a value contain newlines, it consists of
1368 # 1. a short form (first line)
1369 # 2. a long description, all lines begin with a space,
1370 # and paragraphs are separated by a single dot on a line
1371 # This routine returns an array of all paragraphs, and reformat
1373 # When first argument is 2, the string is a comma separated list of
1377 $text =~ s/^[ \t]//mg;
1378 return (split(/, */, $text, 0)) if $type ne 1;
1379 return ($text) if $text !~ /\n/;
1381 $text =~ s/([^\n]*)\n//;
1385 for my $line (split (/\n/, $text))
1388 if ($line =~ /^\.\s*$/)
1395 elsif ($line =~ /^\s/)
1397 # Line which must not be reformatted
1398 $str .= "\n" if length ($str) && $str !~ /\n$/;
1404 # Continuation line, remove newline
1405 $str .= " " if length ($str) && $str !~ /\n$/;
1411 push(@list, $str) if length ($str);
1416 sub quoted_translation
1418 my ($lang, $string) = @_;
1420 $string =~ s/\\\"/\"/g;
1422 my $translation = $translations{$lang, $string};
1423 $translation = $string if !$translation;
1425 $translation =~ s/\"/\\\"/g;
1429 sub quoted_merge_translations
1431 if (!$MULTIPLE_OUTPUT) {
1432 print "Quoted only supports Multiple Output.\n";
1436 for my $lang (sort keys %po_files_by_lang) {
1438 mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1440 open INPUT, "<${FILE}" or die;
1441 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1442 binmode (OUTPUT) if $^O eq 'MSWin32';
1445 s/\"(([^\"]|\\\")*[^\\\"])\"/"\"" . "ed_translation($lang, $1) . "\""/ge;