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";
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;
64 my $PASS_THROUGH_ARG = 0;
66 my $MULTIPLE_OUTPUT = 0;
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
92 my %po_files_by_lang = ();
93 my %translations = ();
94 my $iconv = $ENV{"ICONV"} || $ENV{"INTLTOOL_ICONV"} || "/usr/bin/iconv";
96 # Use this instead of \w for XML files to handle more possible characters.
97 my $w = "[-A-Za-z0-9._:]";
99 # XML quoted string contents
102 ## Check for options.
112 elsif ($BA_STYLE_ARG && @ARGV > 2)
117 &ba_merge_translations;
120 elsif ($XML_STYLE_ARG && @ARGV > 2)
128 elsif ($KEYS_STYLE_ARG && @ARGV > 2)
133 &keys_merge_translations;
136 elsif ($DESKTOP_STYLE_ARG && @ARGV > 2)
141 &desktop_merge_translations;
144 elsif ($SCHEMAS_STYLE_ARG && @ARGV > 2)
149 &schemas_merge_translations;
152 elsif ($RFC822DEB_STYLE_ARG && @ARGV > 2)
156 &rfc822deb_merge_translations;
166 ## Sub for printing release information
170 ${PROGRAM} (${PACKAGE}) ${VERSION}
171 Written by Maciej Stachowiak, Darin Adler and Kenneth Christiansen.
173 Copyright (C) 2000-2003 Free Software Foundation, Inc.
174 Copyright (C) 2000-2001 Eazel, Inc.
175 This is free software; see the source for copying conditions. There is NO
176 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
181 ## Sub for printing usage information
185 Usage: ${PROGRAM} [OPTION]... PO_DIRECTORY FILENAME OUTPUT_FILE
186 Generates an output file that includes some localized attributes from an
187 untranslated source file.
189 Mandatory options: (exactly one must be specified)
190 -b, --ba-style includes translations in the bonobo-activation style
191 -d, --desktop-style includes translations in the desktop style
192 -k, --keys-style includes translations in the keys style
193 -s, --schemas-style includes translations in the schemas style
194 -r, --rfc822deb-style includes translations in the RFC822 style
195 -x, --xml-style includes translations in the standard xml style
198 -u, --utf8 convert all strings to UTF-8 before merging
199 (default for everything except RFC822 style)
200 -p, --pass-through deprecated, does nothing and issues a warning
201 -m, --multiple-output output one localized file per locale, instead of
202 a single file containing all localized elements
203 -c, --cache=FILE specify cache file name
204 (usually \$top_builddir/po/.intltool-merge-cache)
205 -q, --quiet suppress most messages
206 --help display this help and exit
207 --version output version information and exit
209 Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
210 or send email to <xml-i18n-tools\@gnome.org>.
216 ## Sub for printing error messages
219 print STDERR "Try `${PROGRAM} --help' for more information.\n";
226 print "Merging translations into $OUTFILE.\n" unless $QUIET_ARG;
237 &get_translation_database;
240 # General-purpose code for looking up translations in .po files
245 $tmp =~ s/^.*\/(.*)\.po$/$1/;
251 for my $po_file (glob "$PO_DIR/*.po") {
252 $po_files_by_lang{po_file2lang($po_file)} = $po_file;
256 sub get_local_charset
259 my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "/usr/lib/charset.alias";
261 # seek character encoding aliases in charset.alias (glib)
263 if (open CHARSET_ALIAS, $alias_file)
265 while (<CHARSET_ALIAS>)
268 return $1 if (/^\s*([-._a-zA-Z0-9]+)\s+$encoding\b/i)
274 # if not found, return input string
281 my ($in_po_file) = @_;
284 open IN_PO_FILE, $in_po_file or die;
287 ## example: "Content-Type: text/plain; charset=ISO-8859-1\n"
288 if (/Content-Type\:.*charset=([-a-zA-Z0-9]+)\\n/)
298 print STDERR "Warning: no encoding found in $in_po_file. Assuming ISO-8859-1\n" unless $QUIET_ARG;
299 $encoding = "ISO-8859-1";
302 system ("$iconv -f $encoding -t UTF-8 </dev/null 2>/dev/null");
304 $encoding = get_local_charset($encoding);
310 sub utf8_sanity_check
312 print STDERR "Warning: option --pass-through has been removed.\n" if $PASS_THROUGH_ARG;
316 sub get_translation_database
319 &get_cached_translation_database;
321 &create_translation_database;
325 sub get_newest_po_age
329 foreach my $file (values %po_files_by_lang)
331 my $file_age = -M $file;
332 $newest_age = $file_age if !$newest_age || $file_age < $newest_age;
335 $newest_age = 0 if !$newest_age;
342 print "Generating and caching the translation database\n" unless $QUIET_ARG;
344 &create_translation_database;
346 open CACHE, ">$cache_file" || die;
347 print CACHE join "\x01", %translations;
353 print "Found cached translation database\n" unless $QUIET_ARG;
356 open CACHE, "<$cache_file" || die;
362 %translations = split "\x01", $contents;
365 sub get_cached_translation_database
367 my $cache_file_age = -M $cache_file;
368 if (defined $cache_file_age)
370 if ($cache_file_age <= &get_newest_po_age)
375 print "Found too-old cached translation database\n" unless $QUIET_ARG;
381 sub create_translation_database
383 for my $lang (keys %po_files_by_lang)
385 my $po_file = $po_files_by_lang{$lang};
389 my $encoding = get_po_encoding ($po_file);
391 if (lc $encoding eq "utf-8")
393 open PO_FILE, "<$po_file";
397 print STDERR "WARNING: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
399 open PO_FILE, "$iconv -f $encoding -t UTF-8 $po_file|";
404 open PO_FILE, "<$po_file";
415 $nextfuzzy = 1 if /^#, fuzzy/;
417 if (/^msgid "((\\.|[^\\])*)"/ )
419 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
426 $msgid = unescape_po_string($1);
433 if (/^msgstr "((\\.|[^\\])*)"/)
435 $msgstr = unescape_po_string($1);
440 if (/^"((\\.|[^\\])*)"/)
442 $msgid .= unescape_po_string($1) if $inmsgid;
443 $msgstr .= unescape_po_string($1) if $inmsgstr;
446 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
454 sub unescape_one_sequence
458 return "\\" if $sequence eq "\\\\";
459 return "\"" if $sequence eq "\\\"";
460 return "\n" if $sequence eq "\\n";
461 return "\r" if $sequence eq "\\r";
462 return "\t" if $sequence eq "\\t";
463 return "\b" if $sequence eq "\\b";
464 return "\f" if $sequence eq "\\f";
465 return "\a" if $sequence eq "\\a";
466 return chr(11) if $sequence eq "\\v"; # vertical tab, see ascii(7)
468 return chr(hex($1)) if ($sequence =~ /\\x([0-9a-fA-F]{2})/);
469 return chr(oct($1)) if ($sequence =~ /\\([0-7]{3})/);
471 # FIXME: Is \0 supported as well? Kenneth and Rodney don't want it, see bug #48489
476 sub unescape_po_string
480 $string =~ s/(\\x[0-9a-fA-F]{2}|\\[0-7]{3}|\\.)/unescape_one_sequence($1)/eg;
485 ## NOTE: deal with < - < but not > - > because it seems its ok to have
486 ## > in the entity. For further info please look at #84738.
499 # entity_encode: (string)
501 # Encode the given string to XML format (encode '<' etc).
505 my ($pre_encoded) = @_;
507 my @list_of_chars = unpack ('C*', $pre_encoded);
509 # with UTF-8 we only encode minimalistic
510 return join ('', map (&entity_encode_int_minimalist, @list_of_chars));
513 sub entity_encode_int_minimalist
515 return """ if $_ == 34;
516 return "&" if $_ == 38;
517 return "'" if $_ == 39;
518 return "<" if $_ == 60;
522 sub entity_encoded_translation
524 my ($lang, $string) = @_;
526 my $translation = $translations{$lang, $string};
527 return $string if !$translation;
528 return entity_encode ($translation);
531 ## XML (bonobo-activation specific) merge code
533 sub ba_merge_translations
538 local $/; # slurp mode
539 open INPUT, "<$FILE" or die "can't open $FILE: $!";
544 open OUTPUT, ">$OUTFILE" or die "can't open $OUTFILE: $!";
546 while ($source =~ s|^(.*?)([ \t]*<\s*$w+\s+($w+\s*=\s*"$q"\s*)+/?>)([ \t]*\n)?||s)
550 my $node = $2 . "\n";
554 while (s/(\s)_($w+\s*=\s*"($q)")/$1$2/s) {
555 push @strings, entity_decode($3);
560 for my $string (@strings)
562 for my $lang (keys %po_files_by_lang)
564 $langs{$lang} = 1 if $translations{$lang, $string};
568 for my $lang (sort keys %langs)
571 s/(\sname\s*=\s*)"($q)"/$1"$2-$lang"/s;
572 s/(\s)_($w+\s*=\s*")($q)"/$1 . $2 . entity_encoded_translation($lang, $3) . '"'/seg;
577 print OUTPUT $source;
583 ## XML (non-bonobo-activation) merge code
586 # Process tag attributes
587 # Only parameter is a HASH containing attributes -> values mapping
588 sub getAttributeString
591 my $do_translate = shift || 0;
592 my $language = shift || "";
594 my $translate = shift;
595 foreach my $e (reverse(sort(keys %{ $sub }))) {
597 my $string = $sub->{$e};
600 $string =~ s/^[\s]+//;
601 $string =~ s/[\s]+$//;
603 if ($string =~ /^'.*'$/)
607 $string =~ s/^['"]//g;
608 $string =~ s/['"]$//g;
610 if ($do_translate && $key =~ /^_/) {
616 my $decode_string = entity_decode($string);
617 my $translation = $translations{$language, $decode_string};
619 $translation = entity_encode($translation);
620 $string = $translation;
623 $$translate = 2; # we still want translations for deep nesting (FIXME: this will cause
624 # problems since we might get untranslated duplicated entries, but with xml:lang set)
625 # Fix would be to set it here to eg. 3, and do a check in traverse() to see if any of the containing tags
626 # really need translation, and only emit "translation" if there is (this means parsing same data twice)
629 $$translate = 2 if ($translate && (!$$translate)); # watch not to "overwrite" if $translate == 2
633 $result .= " $key=$quote$string$quote";
638 # Returns a translatable string from XML node, it works on contents of every node in XML::Parser tree
639 # doesn't support nesting of translatable tags (i.e. <_blah>this <_doh>doesn't</_doh> work</_blah> -- besides
640 # can you define the correct semantics for this?)
646 my @list = @{ $ref };
649 my $count = scalar(@list);
650 my $attrs = $list[0];
653 while ($index < $count) {
654 my $type = $list[$index];
655 my $content = $list[$index+1];
659 # lets strip the whitespace here, and *ONLY* here
660 $content =~ s/\s+/ /gs if (!((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/)));
661 $result .= ($content);
663 #print "no cdata content when expected it\n"; # is this possible, is this ok?
664 # what to do if this happens?
665 # Did I mention that I hate XML::Parser tree style?
668 # We've got another element
670 $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
672 my $subresult = getXMLstring($content);
674 $result .= ">".$subresult . "</$type>";
687 # Translate list of nodes if necessary
688 sub translate_subnodes
692 my $language = shift || "";
693 my $singlelang = shift || 0;
695 my @nodes = @{ $content };
697 my $count = scalar(@nodes);
699 while ($index < $count) {
700 my $type = $nodes[$index];
701 my $rest = $nodes[$index+1];
703 my $oldMO = $MULTIPLE_OUTPUT;
704 $MULTIPLE_OUTPUT = 1;
705 traverse($fh, $type, $rest, $language);
706 $MULTIPLE_OUTPUT = $oldMO;
708 traverse($fh, $type, $rest, $language);
717 my $nodename = shift;
719 my $language = shift || "";
722 if ($content =~ /^[\s]*$/) {
723 $leading_space .= $content;
728 my @all = @{ $content };
729 my $attrs = shift @all;
731 my $outattr = getAttributeString($attrs, 1, $language, \$translate);
733 if ($nodename =~ /^_/) {
738 print $fh "<$nodename", $outattr;
740 $lookup = getXMLstring($content);
741 if (!((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/))) {
742 $lookup =~ s/^\s+//s;
743 $lookup =~ s/\s+$//s;
746 if ($lookup || $translate == 2) {
747 my $translation = $translations{$language, $lookup};
748 if ($MULTIPLE_OUTPUT && ($translation || $translate == 2)) {
749 $translation = $lookup if (!$translation);
750 print $fh " xml:lang=\"", $language, "\"" if $language;
752 if ($translate == 2) {
753 translate_subnodes($fh, \@all, $language, 1);
755 print $fh $translation;
757 print $fh "</$nodename>";
759 return; # this means there will be no same translation with xml:lang="$language"...
760 # if we want them both, just remove this "return"
763 if ($translate == 2) {
764 translate_subnodes($fh, \@all, $language, 1);
768 print $fh "</$nodename>";
774 for my $lang (sort keys %po_files_by_lang) {
775 if ($MULTIPLE_OUTPUT && $lang ne "$language") {
782 my $localattrs = getAttributeString($attrs, 1, $lang, \$translate);
783 my $translation = $translations{$lang, $lookup};
784 if ($translate && !$translation) {
785 $translation = $lookup;
788 if ($translation || $translate) {
790 $leading_space =~ s/.*\n//g;
791 print $fh $leading_space;
792 print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs, ">";
793 if ($translate == 2) {
794 translate_subnodes($fh, \@all, $lang, 1);
796 print $fh $translation;
798 print $fh "</$nodename>";
804 my $count = scalar(@all);
808 while ($index < $count) {
809 my $type = $all[$index];
810 my $rest = $all[$index+1];
811 traverse($fh, $type, $rest, $language);
814 print $fh "</$nodename>";
822 sub intltool_tree_cdatastart
825 my $clist = $expat->{Curlist};
828 push @$clist, 0 => $expat->original_string();
831 sub intltool_tree_cdataend
834 my $clist = $expat->{Curlist};
837 $clist->[$pos] .= $expat->original_string();
840 sub intltool_tree_char
844 my $clist = $expat->{Curlist};
847 # Use original_string so that we retain escaped entities
850 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
851 $clist->[$pos] .= $expat->original_string();
853 push @$clist, 0 => $expat->original_string();
857 sub intltool_tree_start
863 # Use original_string so that we retain escaped entities
864 # in attribute values. We must convert the string to an
865 # @origlist array to conform to the structure of the Tree
868 my @original_array = split /\x/, $expat->original_string();
869 my $source = $expat->original_string();
871 # Remove leading tag.
873 $source =~ s|^\s*<\s*(\S+)||s;
875 # Grab attribute key/value pairs and push onto @origlist array.
879 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
881 $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
883 push @origlist, '"' . $2 . '"';
885 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
887 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
889 push @origlist, "'" . $2 . "'";
897 my $ol = [ { @origlist } ];
899 push @{ $expat->{Lists} }, $expat->{Curlist};
900 push @{ $expat->{Curlist} }, $tag => $ol;
901 $expat->{Curlist} = $ol;
906 my $filename = shift || return;
908 die "ERROR Cannot find filename: $filename\n";
911 my $ret = eval 'require XML::Parser';
913 die "You must have XML::Parser installed to run $0\n\n";
915 my $xp = new XML::Parser(Style => 'Tree');
916 $xp->setHandlers(Char => \&intltool_tree_char);
917 $xp->setHandlers(Start => \&intltool_tree_start);
918 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
919 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
920 my $tree = $xp->parsefile($filename);
922 # <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
924 # [foo, [{}, head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar, [{},
925 # 0, "Howdy", ref, [{}]], 0, "do" ] ]
937 die "ERROR Cannot find filename: $infile\n";
940 print $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n};
943 open DOCINPUT, "<${FILE}" or die;
944 $source = <DOCINPUT>;
947 if ($source =~ /(<!DOCTYPE.*\[.*\]\s*>)/s)
951 elsif ($source =~ /(<!DOCTYPE[^>]*>)/s)
961 my $language = shift || "";
963 my $name = shift @{ $ref };
964 my $cont = shift @{ $ref };
965 traverse($fh, $name, $cont, $language);
972 if ($MULTIPLE_OUTPUT) {
973 for my $lang (sort keys %po_files_by_lang) {
975 mkdir $lang or die "Cannot create subdirectory $lang: $!\n";
977 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
978 my $tree = readXml($FILE);
979 print_header($FILE, \*OUTPUT);
980 parseTree(\*OUTPUT, $tree, $lang);
982 print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
985 open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
986 my $tree = readXml($FILE);
987 print_header($FILE, \*OUTPUT);
988 parseTree(\*OUTPUT, $tree);
990 print "CREATED $OUTFILE\n" unless $QUIET_ARG;
993 sub keys_merge_translations
995 open INPUT, "<${FILE}" or die;
996 open OUTPUT, ">${OUTFILE}" or die;
1000 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1006 my $non_translated_line = $_;
1008 for my $lang (sort keys %po_files_by_lang)
1010 my $translation = $translations{$lang, $string};
1011 next if !$translation;
1013 $_ = $non_translated_line;
1014 s/(\w+)=.*/[$lang]$1=$translation/;
1028 sub desktop_merge_translations
1030 open INPUT, "<${FILE}" or die;
1031 open OUTPUT, ">${OUTFILE}" or die;
1035 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1041 my $non_translated_line = $_;
1043 for my $lang (sort keys %po_files_by_lang)
1045 my $translation = $translations{$lang, $string};
1046 next if !$translation;
1048 $_ = $non_translated_line;
1049 s/(\w+)=.*/${1}[$lang]=$translation/;
1063 sub schemas_merge_translations
1068 local $/; # slurp mode
1069 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1074 open OUTPUT, ">$OUTFILE" or die;
1076 # FIXME: support attribute translations
1078 # Empty nodes never need translation, so unmark all of them.
1079 # For example, <_foo/> is just replaced by <foo/>.
1080 $source =~ s|<\s*_($w+)\s*/>|<$1/>|g;
1082 while ($source =~ s/
1084 (\s+)(<locale\ name="C">(\s*)
1085 (<default>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/default>)?(\s*)
1086 (<short>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/short>)?(\s*)
1087 (<long>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/long>)?(\s*)
1093 my $locale_start_spaces = $2 ? $2 : '';
1094 my $default_spaces = $4 ? $4 : '';
1095 my $short_spaces = $7 ? $7 : '';
1096 my $long_spaces = $10 ? $10 : '';
1097 my $locale_end_spaces = $13 ? $13 : '';
1098 my $c_default_block = $3 ? $3 : '';
1099 my $default_string = $6 ? $6 : '';
1100 my $short_string = $9 ? $9 : '';
1101 my $long_string = $12 ? $12 : '';
1103 print OUTPUT "$locale_start_spaces$c_default_block";
1105 $default_string =~ s/\s+/ /g;
1106 $default_string = entity_decode($default_string);
1107 $short_string =~ s/\s+/ /g;
1108 $short_string = entity_decode($short_string);
1109 $long_string =~ s/\s+/ /g;
1110 $long_string = entity_decode($long_string);
1112 for my $lang (sort keys %po_files_by_lang)
1114 my $default_translation = $translations{$lang, $default_string};
1115 my $short_translation = $translations{$lang, $short_string};
1116 my $long_translation = $translations{$lang, $long_string};
1118 next if (!$default_translation && !$short_translation &&
1119 !$long_translation);
1121 print OUTPUT "\n$locale_start_spaces<locale name=\"$lang\">";
1123 print OUTPUT "$default_spaces";
1125 if ($default_translation)
1127 $default_translation = entity_encode($default_translation);
1128 print OUTPUT "<default>$default_translation</default>";
1131 print OUTPUT "$short_spaces";
1133 if ($short_translation)
1135 $short_translation = entity_encode($short_translation);
1136 print OUTPUT "<short>$short_translation</short>";
1139 print OUTPUT "$long_spaces";
1141 if ($long_translation)
1143 $long_translation = entity_encode($long_translation);
1144 print OUTPUT "<long>$long_translation</long>";
1147 print OUTPUT "$locale_end_spaces</locale>";
1151 print OUTPUT $source;
1156 sub rfc822deb_merge_translations
1159 for my $lang (keys %po_files_by_lang) {
1160 $encodings{$lang} = ($UTF8_ARG ? 'UTF-8' : get_po_encoding($po_files_by_lang{$lang}));
1165 $Text::Wrap::huge = 'overflow';
1166 $Text::Wrap::break = qr/\n|\s(?=\S)/;
1169 local $/; # slurp mode
1170 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1175 open OUTPUT, ">${OUTFILE}" or die;
1177 while ($source =~ /(^|\n+)(_*)([^:\s]+)(:[ \t]*)(.*?)(?=\n[\S\n]|$)/sg)
1180 my $non_translated_line = $3.$4;
1182 my $underscore = length($2);
1183 next if $underscore eq 0 && $non_translated_line =~ /^#/;
1184 # Remove [] dummy strings
1185 my $stripped = $string;
1186 $stripped =~ s/\[\s[^\[\]]*\],/,/g if $underscore eq 2;
1187 $stripped =~ s/\[\s[^\[\]]*\]$//;
1188 $non_translated_line .= $stripped;
1190 print OUTPUT $sep.$non_translated_line;
1194 my @str_list = rfc822deb_split($underscore, $string);
1196 for my $lang (sort keys %po_files_by_lang)
1198 my $is_translated = 1;
1199 my $str_translated = '';
1202 for my $str (@str_list)
1204 my $translation = $translations{$lang, $str};
1212 # $translation may also contain [] dummy
1213 # strings, mostly to indicate an empty string
1214 $translation =~ s/\[\s[^\[\]]*\]$//;
1218 if ($underscore eq 2)
1220 $str_translated .= $translation;
1225 Text::Tabs::expand($translation) .
1231 if ($underscore eq 2)
1233 $str_translated .= ', ' . $translation;
1237 $str_translated .= Text::Tabs::expand(
1238 Text::Wrap::wrap(' ', ' ', $translation)) .
1244 # To fix some problems with Text::Wrap::wrap
1245 $str_translated =~ s/(\n )+\n/\n .\n/g;
1247 next unless $is_translated;
1249 $str_translated =~ s/\n \.\n$//;
1250 $str_translated =~ s/\s+$//;
1252 $_ = $non_translated_line;
1253 s/^(\w+):\s*.*/$sep${1}-$lang.$encodings{$lang}: $str_translated/s;
1266 # Debian defines a special way to deal with rfc822-style files:
1267 # when a value contain newlines, it consists of
1268 # 1. a short form (first line)
1269 # 2. a long description, all lines begin with a space,
1270 # and paragraphs are separated by a single dot on a line
1271 # This routine returns an array of all paragraphs, and reformat
1273 # When first argument is 2, the string is a comma separated list of
1277 $text =~ s/^[ \t]//mg;
1278 return (split(/, */, $text, 0)) if $type ne 1;
1279 return ($text) if $text !~ /\n/;
1281 $text =~ s/([^\n]*)\n//;
1285 for my $line (split (/\n/, $text))
1288 if ($line =~ /^\.\s*$/)
1295 elsif ($line =~ /^\s/)
1297 # Line which must not be reformatted
1298 $str .= "\n" if length ($str) && $str !~ /\n$/;
1304 # Continuation line, remove newline
1305 $str .= " " if length ($str) && $str !~ /\n$/;
1311 push(@list, $str) if length ($str);