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 = "@PACKAGE@";
38 my $VERSION = "@VERSION@";
47 my $must_end_tag = -1;
49 my $translation_depth = -1;
52 my @translation_strings = ();
53 my $leading_space = "";
55 ## Scalars used by the option stuff
59 my $XML_STYLE_ARG = 0;
60 my $KEYS_STYLE_ARG = 0;
61 my $DESKTOP_STYLE_ARG = 0;
62 my $SCHEMAS_STYLE_ARG = 0;
63 my $RFC822DEB_STYLE_ARG = 0;
64 my $QUOTED_STYLE_ARG = 0;
65 my $QUOTEDXML_STYLE_ARG = 0;
67 my $PASS_THROUGH_ARG = 0;
69 my $MULTIPLE_OUTPUT = 0;
70 my $NO_TRANSLATIONS_ARG = 0;
77 "version" => \$VERSION_ARG,
78 "quiet|q" => \$QUIET_ARG,
79 "oaf-style|o" => \$BA_STYLE_ARG, ## for compatibility
80 "ba-style|b" => \$BA_STYLE_ARG,
81 "xml-style|x" => \$XML_STYLE_ARG,
82 "keys-style|k" => \$KEYS_STYLE_ARG,
83 "desktop-style|d" => \$DESKTOP_STYLE_ARG,
84 "schemas-style|s" => \$SCHEMAS_STYLE_ARG,
85 "rfc822deb-style|r" => \$RFC822DEB_STYLE_ARG,
86 "quoted-style" => \$QUOTED_STYLE_ARG,
87 "quotedxml-style" => \$QUOTEDXML_STYLE_ARG,
88 "pass-through|p" => \$PASS_THROUGH_ARG,
89 "utf8|u" => \$UTF8_ARG,
90 "multiple-output|m" => \$MULTIPLE_OUTPUT,
91 "no-translations" => \$NO_TRANSLATIONS_ARG,
92 "cache|c=s" => \$cache_file
99 my %po_files_by_lang = ();
100 my %translations = ();
102 # Use this instead of \w for XML files to handle more possible characters.
103 my $w = "[-A-Za-z0-9._:]";
105 # XML quoted string contents
108 ## Check for options.
118 elsif ($BA_STYLE_ARG && @ARGV > 2)
123 &ba_merge_translations;
126 elsif ($XML_STYLE_ARG && (@ARGV > 2 || ($NO_TRANSLATIONS_ARG && @ARGV > 1)))
134 elsif ($KEYS_STYLE_ARG && @ARGV > 2)
139 &keys_merge_translations;
142 elsif ($DESKTOP_STYLE_ARG && @ARGV > 2)
147 &desktop_merge_translations;
150 elsif ($SCHEMAS_STYLE_ARG && @ARGV > 2)
155 &schemas_merge_translations;
158 elsif ($RFC822DEB_STYLE_ARG && @ARGV > 2)
162 &rfc822deb_merge_translations;
165 elsif (($QUOTED_STYLE_ARG || $QUOTEDXML_STYLE_ARG) && @ARGV > 2)
170 "ed_merge_translations($QUOTEDXML_STYLE_ARG);
180 ## Sub for printing release information
184 ${PROGRAM} (${PACKAGE}) ${VERSION}
185 Written by Maciej Stachowiak, Darin Adler and Kenneth Christiansen.
187 Copyright (C) 2000-2003 Free Software Foundation, Inc.
188 Copyright (C) 2000-2001 Eazel, Inc.
189 This is free software; see the source for copying conditions. There is NO
190 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
195 ## Sub for printing usage information
199 Usage: ${PROGRAM} [OPTION]... PO_DIRECTORY FILENAME OUTPUT_FILE
200 Generates an output file that includes some localized attributes from an
201 untranslated source file.
203 Mandatory options: (exactly one must be specified)
204 -b, --ba-style includes translations in the bonobo-activation style
205 -d, --desktop-style includes translations in the desktop style
206 -k, --keys-style includes translations in the keys style
207 -s, --schemas-style includes translations in the schemas style
208 -r, --rfc822deb-style includes translations in the RFC822 style
209 --quoted-style includes translations in the quoted string style
210 --quotedxml-style includes translations in the quoted xml string style
211 -x, --xml-style includes translations in the standard xml style
214 -u, --utf8 convert all strings to UTF-8 before merging
215 (default for everything except RFC822 style)
216 -p, --pass-through deprecated, does nothing and issues a warning
217 -m, --multiple-output output one localized file per locale, instead of
218 a single file containing all localized elements
219 --no-translations do not merge any translations: only generates the
220 unlocalised (English) version -- applies only
222 -c, --cache=FILE specify cache file name
223 (usually \$top_builddir/po/.intltool-merge-cache)
224 -q, --quiet suppress most messages
225 --help display this help and exit
226 --version output version information and exit
228 Report bugs to http://bugs.launchpad.net/intltool
234 ## Sub for printing error messages
237 print STDERR "Try `${PROGRAM} --help' for more information.\n";
244 print "Merging translations into $OUTFILE.\n" unless $QUIET_ARG;
250 if (!$XML_STYLE_ARG || !$NO_TRANSLATIONS_ARG) {
256 &get_translation_database;
263 # General-purpose code for looking up translations in .po files
268 $tmp =~ s/^.*\/(.*)\.po$/$1/;
274 if (my $linguas = $ENV{"LINGUAS"})
276 for my $lang (split / /, $linguas) {
277 my $po_file = $PO_DIR . "/" . $lang . ".po";
279 $po_files_by_lang{$lang} = $po_file;
285 if (open LINGUAS_FILE, "$PO_DIR/LINGUAS")
287 while (<LINGUAS_FILE>)
294 my $po_file = $PO_DIR . "/" . $lang . ".po";
296 $po_files_by_lang{$lang} = $po_file;
305 for my $po_file (glob "$PO_DIR/*.po") {
306 $po_files_by_lang{po_file2lang($po_file)} = $po_file;
314 my ($in_po_file) = @_;
317 open IN_PO_FILE, $in_po_file or die;
320 ## example: "Content-Type: text/plain; charset=ISO-8859-1\n"
321 if (/Content-Type\:.*charset=([-a-zA-Z0-9]+)\\n/)
331 print STDERR "Warning: no encoding found in $in_po_file. Assuming ISO-8859-1\n" unless $QUIET_ARG;
332 $encoding = "ISO-8859-1";
338 sub utf8_sanity_check
340 print STDERR "Warning: option --pass-through has been removed.\n" if $PASS_THROUGH_ARG;
344 sub get_translation_database
347 &get_cached_translation_database;
349 &create_translation_database;
353 sub get_newest_po_age
357 foreach my $file (values %po_files_by_lang)
359 my $file_age = -M $file;
360 $newest_age = $file_age if !$newest_age || $file_age < $newest_age;
363 $newest_age = 0 if !$newest_age;
370 print "Generating and caching the translation database\n" unless $QUIET_ARG;
372 &create_translation_database;
374 open CACHE, ">$cache_file" || die;
375 print CACHE join "\x01", %translations;
381 print "Found cached translation database\n" unless $QUIET_ARG;
384 open CACHE, "<$cache_file" || die;
390 %translations = split "\x01", $contents;
393 sub get_cached_translation_database
395 my $cache_file_age = -M $cache_file;
396 if (defined $cache_file_age)
398 if ($cache_file_age <= &get_newest_po_age)
403 print "Found too-old cached translation database\n" unless $QUIET_ARG;
411 my ($lang, $encoding, $msgctxt, $msgid, $msgstr) = @_;
413 return if !($msgid && $msgstr);
416 $msgid = "$msgctxt\004$msgid";
418 if (uc $encoding ne "UTF-8") {
419 Encode::from_to ($msgid, $encoding, "UTF-8");
420 Encode::from_to ($msgstr, $encoding, "UTF-8");
422 $translations{$lang, $msgid} = $msgstr;
425 sub create_translation_database
427 for my $lang (keys %po_files_by_lang)
429 my $po_file = $po_files_by_lang{$lang};
430 my $encoding = "UTF-8";
434 $encoding = get_po_encoding ($po_file);
435 if (uc $encoding ne "UTF-8") {
436 print "NOTICE: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
439 open PO_FILE, "<$po_file";
451 $nextfuzzy = 1 if /^#, fuzzy/;
453 if (/^msgctxt "((\\.|[^\\]+)*)"/ )
456 add_translation ($lang, $encoding,
457 $msgctxt, $msgid, $msgstr);
463 $msgctxt = unescape_po_string($1);
469 if (/^msgid "((\\.|[^\\]+)*)"/ )
472 add_translation ($lang, $encoding,
473 $msgctxt, $msgid, $msgstr);
483 $msgid = unescape_po_string($1);
490 if (/^msgstr "((\\.|[^\\]+)*)"/)
492 $msgstr = unescape_po_string($1);
498 if (/^"((\\.|[^\\]+)*)"/)
500 $msgctxt .= unescape_po_string($1) if $inmsgctxt;
501 $msgid .= unescape_po_string($1) if $inmsgid;
502 $msgstr .= unescape_po_string($1) if $inmsgstr;
505 add_translation ($lang, $encoding, $msgctxt, $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;
578 return ">" if $_ == 62;
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 $content = getXMLstring($content, $spacepreserve);
815 if (!$spacepreserve) {
816 $content =~ s/^\s+//s;
817 $content =~ s/\s+$//s;
819 if (exists $attrs->{"msgctxt"}) {
820 my $context = entity_decode ($attrs->{"msgctxt"});
821 $context =~ s/^["'](.*)["']/$1/;
822 $lookup = "$context\004$content";
827 if ($lookup || $translate == 2) {
828 my $translation = $translations{$language, $lookup} if isWellFormedXmlFragment($translations{$language, $lookup});
829 if ($MULTIPLE_OUTPUT && ($translation || $translate == 2)) {
830 $translation = $content if (!$translation);
831 print $fh " xml:lang=\"", $language, "\"" if $language;
833 if ($translate == 2) {
834 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
836 print $fh $translation;
838 print $fh "</$nodename>";
840 return; # this means there will be no same translation with xml:lang="$language"...
841 # if we want them both, just remove this "return"
844 if ($translate == 2) {
845 translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
849 print $fh "</$nodename>";
855 for my $lang (sort keys %po_files_by_lang) {
856 if ($MULTIPLE_OUTPUT && $lang ne "$language") {
863 my $localattrs = getAttributeString($attrs, 1, $lang, \$translate);
864 my $translation = $translations{$lang, $lookup} if isWellFormedXmlFragment($translations{$lang, $lookup});
865 if ($translate && !$translation) {
866 $translation = $content;
869 if ($translation || $translate) {
871 $leading_space =~ s/.*\n//g;
872 print $fh $leading_space;
873 print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs, ">";
874 if ($translate == 2) {
875 translate_subnodes($fh, \@all, $lang, 1, $spacepreserve);
877 print $fh $translation;
879 print $fh "</$nodename>";
885 my $count = scalar(@all);
889 while ($index < $count) {
890 my $type = $all[$index];
891 my $rest = $all[$index+1];
892 traverse($fh, $type, $rest, $language, $spacepreserve);
895 print $fh "</$nodename>";
903 sub intltool_tree_comment
907 my $clist = $expat->{Curlist};
910 push @$clist, 1 => $data;
913 sub intltool_tree_cdatastart
916 my $clist = $expat->{Curlist};
919 push @$clist, 0 => $expat->original_string();
922 sub intltool_tree_cdataend
925 my $clist = $expat->{Curlist};
928 $clist->[$pos] .= $expat->original_string();
931 sub intltool_tree_char
935 my $clist = $expat->{Curlist};
938 # Use original_string so that we retain escaped entities
941 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
942 $clist->[$pos] .= $expat->original_string();
944 push @$clist, 0 => $expat->original_string();
948 sub intltool_tree_start
954 # Use original_string so that we retain escaped entities
955 # in attribute values. We must convert the string to an
956 # @origlist array to conform to the structure of the Tree
959 my @original_array = split /\x/, $expat->original_string();
960 my $source = $expat->original_string();
962 # Remove leading tag.
964 $source =~ s|^\s*<\s*(\S+)||s;
966 # Grab attribute key/value pairs and push onto @origlist array.
970 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
972 $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
974 push @origlist, '"' . $2 . '"';
976 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
978 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
980 push @origlist, "'" . $2 . "'";
988 my $ol = [ { @origlist } ];
990 push @{ $expat->{Lists} }, $expat->{Curlist};
991 push @{ $expat->{Curlist} }, $tag => $ol;
992 $expat->{Curlist} = $ol;
997 my $filename = shift || return;
999 die "ERROR Cannot find filename: $filename\n";
1002 my $ret = eval 'require XML::Parser';
1004 die "You must have XML::Parser installed to run $0\n\n";
1006 my $xp = new XML::Parser(Style => 'Tree');
1007 $xp->setHandlers(Char => \&intltool_tree_char);
1008 $xp->setHandlers(Start => \&intltool_tree_start);
1009 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
1010 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
1011 my $tree = $xp->parsefile($filename);
1013 # <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
1015 # [foo, [{}, head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar, [{},
1016 # 0, "Howdy", ref, [{}]], 0, "do" ] ]
1028 die "ERROR Cannot find filename: $infile\n";
1031 print $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n};
1034 open DOCINPUT, "<${FILE}" or die;
1035 $source = <DOCINPUT>;
1038 if ($source =~ /(<!DOCTYPE.*\[.*\]\s*>)/s)
1042 elsif ($source =~ /(<!DOCTYPE[^>]*>)/s)
1052 my $language = shift || "";
1054 my $name = shift @{ $ref };
1055 my $cont = shift @{ $ref };
1057 while (!$name || "$name" eq "1") {
1058 $name = shift @{ $ref };
1059 $cont = shift @{ $ref };
1062 my $spacepreserve = 0;
1063 my $attrs = @{$cont}[0];
1064 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
1066 traverse($fh, $name, $cont, $language, $spacepreserve);
1069 sub xml_merge_output
1073 if ($MULTIPLE_OUTPUT) {
1074 for my $lang (sort keys %po_files_by_lang) {
1076 mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1078 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1079 binmode (OUTPUT) if $^O eq 'MSWin32';
1080 my $tree = readXml($FILE);
1081 print_header($FILE, \*OUTPUT);
1082 parseTree(\*OUTPUT, $tree, $lang);
1084 print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1087 mkdir "C" or -d "C" or die "Cannot create subdirectory C: $!\n";
1089 open OUTPUT, ">C/$OUTFILE" or die "Cannot open C/$OUTFILE: $!\n";
1090 binmode (OUTPUT) if $^O eq 'MSWin32';
1091 my $tree = readXml($FILE);
1092 print_header($FILE, \*OUTPUT);
1093 parseTree(\*OUTPUT, $tree);
1095 print "CREATED C/$OUTFILE\n" unless $QUIET_ARG;
1097 open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
1098 binmode (OUTPUT) if $^O eq 'MSWin32';
1099 my $tree = readXml($FILE);
1100 print_header($FILE, \*OUTPUT);
1101 parseTree(\*OUTPUT, $tree);
1103 print "CREATED $OUTFILE\n" unless $QUIET_ARG;
1107 sub keys_merge_translation
1111 if ( ! -d $lang && $MULTIPLE_OUTPUT)
1113 mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1116 open INPUT, "<${FILE}" or die "Cannot open ${FILE}: $!\n";
1117 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1118 binmode (OUTPUT) if $^O eq 'MSWin32';
1122 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1126 if (!$MULTIPLE_OUTPUT)
1130 my $non_translated_line = $_;
1132 for my $lang (sort keys %po_files_by_lang)
1134 my $translation = $translations{$lang, $string};
1135 next if !$translation;
1137 $_ = $non_translated_line;
1138 s/(\w+)=.*/[$lang]$1=$translation/;
1144 my $non_translated_line = $_;
1145 my $translation = $translations{$lang, $string};
1146 $translation = $string if !$translation;
1148 $_ = $non_translated_line;
1149 s/(\w+)=.*/$1=$translation/;
1162 print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1165 sub keys_merge_translations
1167 if ($MULTIPLE_OUTPUT)
1169 for my $lang (sort keys %po_files_by_lang)
1171 keys_merge_translation ($lang);
1173 keys_merge_translation ("C");
1177 keys_merge_translation (".");
1181 sub desktop_merge_translations
1183 open INPUT, "<${FILE}" or die;
1184 open OUTPUT, ">${OUTFILE}" or die;
1185 binmode (OUTPUT) if $^O eq 'MSWin32';
1189 if (s/^(\s*)_([A-Za-z0-9\-]+=(.*))/$1$2/)
1195 my $non_translated_line = $_;
1197 for my $lang (sort keys %po_files_by_lang)
1199 my $translation = $translations{$lang, $string};
1200 next if !$translation;
1202 $_ = $non_translated_line;
1203 s/(\w+)=.*/${1}[$lang]=$translation/;
1217 sub schemas_merge_translations
1222 local $/; # slurp mode
1223 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1228 open OUTPUT, ">$OUTFILE" or die;
1229 binmode (OUTPUT) if $^O eq 'MSWin32';
1231 # FIXME: support attribute translations
1233 # Empty nodes never need translation, so unmark all of them.
1234 # For example, <_foo/> is just replaced by <foo/>.
1235 $source =~ s|<\s*_($w+)\s*/>|<$1/>|g;
1237 while ($source =~ s/
1239 (\s+)(<locale\ name="C">(\s*)
1240 (<default>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/default>)?(\s*)
1241 (<short>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/short>)?(\s*)
1242 (<long>\s*(?:<!--[^>]*?-->\s*)?(.*?)\s*<\/long>)?(\s*)
1248 my $locale_start_spaces = $2 ? $2 : '';
1249 my $default_spaces = $4 ? $4 : '';
1250 my $short_spaces = $7 ? $7 : '';
1251 my $long_spaces = $10 ? $10 : '';
1252 my $locale_end_spaces = $13 ? $13 : '';
1253 my $c_default_block = $3 ? $3 : '';
1254 my $default_string = $6 ? $6 : '';
1255 my $short_string = $9 ? $9 : '';
1256 my $long_string = $12 ? $12 : '';
1258 print OUTPUT "$locale_start_spaces$c_default_block";
1260 $default_string =~ s/\s+/ /g;
1261 $default_string = entity_decode($default_string);
1262 $short_string =~ s/\s+/ /g;
1263 $short_string = entity_decode($short_string);
1264 $long_string =~ s/\s+/ /g;
1265 $long_string = entity_decode($long_string);
1267 for my $lang (sort keys %po_files_by_lang)
1269 my $default_translation = $translations{$lang, $default_string};
1270 my $short_translation = $translations{$lang, $short_string};
1271 my $long_translation = $translations{$lang, $long_string};
1273 next if (!$default_translation && !$short_translation &&
1274 !$long_translation);
1276 print OUTPUT "\n$locale_start_spaces<locale name=\"$lang\">";
1278 print OUTPUT "$default_spaces";
1280 if ($default_translation)
1282 $default_translation = entity_encode($default_translation);
1283 print OUTPUT "<default>$default_translation</default>";
1286 print OUTPUT "$short_spaces";
1288 if ($short_translation)
1290 $short_translation = entity_encode($short_translation);
1291 print OUTPUT "<short>$short_translation</short>";
1294 print OUTPUT "$long_spaces";
1296 if ($long_translation)
1298 $long_translation = entity_encode($long_translation);
1299 print OUTPUT "<long>$long_translation</long>";
1302 print OUTPUT "$locale_end_spaces</locale>";
1306 print OUTPUT $source;
1311 sub rfc822deb_merge_translations
1314 for my $lang (keys %po_files_by_lang) {
1315 $encodings{$lang} = ($UTF8_ARG ? 'UTF-8' : get_po_encoding($po_files_by_lang{$lang}));
1320 $Text::Wrap::huge = 'overflow';
1321 $Text::Wrap::break = qr/\n|\s(?=\S)/;
1324 local $/; # slurp mode
1325 open INPUT, "<$FILE" or die "can't open $FILE: $!";
1330 open OUTPUT, ">${OUTFILE}" or die;
1331 binmode (OUTPUT) if $^O eq 'MSWin32';
1333 while ($source =~ /(^|\n+)(_*)([^:\s]+)(:[ \t]*)(.*?)(?=\n[\S\n]|$)/sg)
1336 my $non_translated_line = $3.$4;
1338 my $underscore = length($2);
1339 next if $underscore eq 0 && $non_translated_line =~ /^#/;
1340 # Remove [] dummy strings
1341 my $stripped = $string;
1342 $stripped =~ s/\[\s[^\[\]]*\],/,/g if $underscore eq 2;
1343 $stripped =~ s/\[\s[^\[\]]*\]$//;
1344 $non_translated_line .= $stripped;
1346 print OUTPUT $sep.$non_translated_line;
1350 my @str_list = rfc822deb_split($underscore, $string);
1352 for my $lang (sort keys %po_files_by_lang)
1354 my $is_translated = 1;
1355 my $str_translated = '';
1358 for my $str (@str_list)
1360 my $translation = $translations{$lang, $str};
1368 # $translation may also contain [] dummy
1369 # strings, mostly to indicate an empty string
1370 $translation =~ s/\[\s[^\[\]]*\]$//;
1374 if ($underscore eq 2)
1376 $str_translated .= $translation;
1381 Text::Tabs::expand($translation) .
1387 if ($underscore eq 2)
1389 $str_translated .= ', ' . $translation;
1393 $str_translated .= Text::Tabs::expand(
1394 Text::Wrap::wrap(' ', ' ', $translation)) .
1400 # To fix some problems with Text::Wrap::wrap
1401 $str_translated =~ s/(\n )+\n/\n .\n/g;
1403 next unless $is_translated;
1405 $str_translated =~ s/\n \.\n$//;
1406 $str_translated =~ s/\s+$//;
1408 $_ = $non_translated_line;
1409 s/^(\w+):\s*.*/$sep${1}-$lang.$encodings{$lang}: $str_translated/s;
1422 # Debian defines a special way to deal with rfc822-style files:
1423 # when a value contain newlines, it consists of
1424 # 1. a short form (first line)
1425 # 2. a long description, all lines begin with a space,
1426 # and paragraphs are separated by a single dot on a line
1427 # This routine returns an array of all paragraphs, and reformat
1429 # When first argument is 2, the string is a comma separated list of
1433 $text =~ s/^[ \t]//mg;
1434 return (split(/, */, $text, 0)) if $type ne 1;
1435 return ($text) if $text !~ /\n/;
1437 $text =~ s/([^\n]*)\n//;
1441 for my $line (split (/\n/, $text))
1444 if ($line =~ /^\.\s*$/)
1451 elsif ($line =~ /^\s/)
1453 # Line which must not be reformatted
1454 $str .= "\n" if length ($str) && $str !~ /\n$/;
1460 # Continuation line, remove newline
1461 $str .= " " if length ($str) && $str !~ /\n$/;
1467 push(@list, $str) if length ($str);
1472 sub quoted_translation
1474 my ($xml_mode, $lang, $string) = @_;
1476 $string = entity_decode($string) if $xml_mode;
1477 $string =~ s/\\\"/\"/g;
1479 my $translation = $translations{$lang, $string};
1480 $translation = $string if !$translation;
1481 $translation = entity_encode($translation) if $xml_mode;
1482 $translation =~ s/\"/\\\"/g;
1486 sub quoted_merge_translations
1488 my ($xml_mode) = @_;
1490 if (!$MULTIPLE_OUTPUT) {
1491 print "Quoted only supports Multiple Output.\n";
1495 for my $lang (sort keys %po_files_by_lang) {
1497 mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1499 open INPUT, "<${FILE}" or die;
1500 open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1501 binmode (OUTPUT) if $^O eq 'MSWin32';
1504 s/\"(([^\"]|\\\")*[^\\\"])\"/"\"" . "ed_translation($xml_mode, $lang, $1) . "\""/ge;