1 #!/scratchbox/tools/bin/perl -w
2 # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4 -*-
5 # The Intltool Message Merger
7 # Copyright (C) 2000, 2003 Free Software Foundation.
8 # Copyright (C) 2000, 2001 Eazel, Inc
10 # Intltool is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # version 2 published by the Free Software Foundation.
14 # Intltool is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # As a special exception to the GNU General Public License, if you
24 # distribute this file as part of a program that contains a
25 # configuration script generated by Autoconf, you may include it under
26 # the same distribution terms that you use for the rest of that program.
28 # Authors: Maciej Stachowiak <mjs@noisehavoc.org>
29 # Kenneth Christiansen <kenneth@gnu.org>
30 # Darin Adler <darin@bentspoon.com>
32 # Proper XML UTF-8'ification written by Cyrille Chepelov <chepelov@calixo.net>
35 ## Release information
36 my $PROGRAM = "intltool-merge";
37 my $PACKAGE = "intltool";
38 my $VERSION = "0.35.0";
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";
95 my $devnull = ($^O
eq 'MSWin32' ?
'NUL:' : '/dev/null');
97 # Use this instead of \w for XML files to handle more possible characters.
98 my $w = "[-A-Za-z0-9._:]";
100 # XML quoted string contents
103 ## Check for options.
113 elsif ($BA_STYLE_ARG && @ARGV > 2)
118 &ba_merge_translations
;
121 elsif ($XML_STYLE_ARG && @ARGV > 2)
129 elsif ($KEYS_STYLE_ARG && @ARGV > 2)
134 &keys_merge_translations
;
137 elsif ($DESKTOP_STYLE_ARG && @ARGV > 2)
142 &desktop_merge_translations
;
145 elsif ($SCHEMAS_STYLE_ARG && @ARGV > 2)
150 &schemas_merge_translations
;
153 elsif ($RFC822DEB_STYLE_ARG && @ARGV > 2)
157 &rfc822deb_merge_translations
;
167 ## Sub for printing release information
171 ${PROGRAM
} (${PACKAGE
}) ${VERSION
}
172 Written by Maciej Stachowiak
, Darin Adler
and Kenneth Christiansen
.
174 Copyright
(C
) 2000-2003 Free Software Foundation
, Inc
.
175 Copyright
(C
) 2000-2001 Eazel
, Inc
.
176 This is free software
; see the source
for copying conditions
. There is NO
177 warranty
; not even
for MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE
.
182 ## Sub for printing usage information
186 Usage
: ${PROGRAM
} [OPTION
]... PO_DIRECTORY FILENAME OUTPUT_FILE
187 Generates an output file that includes some localized attributes from an
188 untranslated source file
.
190 Mandatory options
: (exactly one must be specified
)
191 -b
, --ba
-style includes translations
in the bonobo
-activation style
192 -d
, --desktop
-style includes translations
in the desktop style
193 -k
, --keys-style includes translations
in the
keys style
194 -s
, --schemas
-style includes translations
in the schemas style
195 -r
, --rfc822deb
-style includes translations
in the RFC822 style
196 -x
, --xml
-style includes translations
in the standard xml style
199 -u
, --utf8 convert all strings to UTF
-8 before merging
200 (default for everything except RFC822 style
)
201 -p
, --pass
-through deprecated
, does nothing
and issues a warning
202 -m
, --multiple
-output output one localized file per locale
, instead of
203 a single file containing all localized elements
204 -c
, --cache
=FILE specify cache file name
205 (usually \
$top_builddir/po/.intltool
-merge
-cache
)
206 -q
, --quiet suppress most messages
207 --help display this help
and exit
208 --version output version information
and exit
210 Report bugs to http
://bugzilla
.gnome
.org
/ (product name
"$PACKAGE")
211 or send email to
<xml
-i18n
-tools\
@gnome.org
>.
217 ## Sub for printing error messages
220 print STDERR
"Try `${PROGRAM} --help' for more information.\n";
227 print "Merging translations into $OUTFILE.\n" unless $QUIET_ARG;
238 &get_translation_database
;
241 # General-purpose code for looking up translations in .po files
246 $tmp =~ s/^.*\/(.*)\.po$/$1/;
252 for my $po_file (glob "$PO_DIR/*.po") {
253 $po_files_by_lang{po_file2lang
($po_file)} = $po_file;
257 sub get_local_charset
260 my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "/usr/lib/charset.alias";
262 # seek character encoding aliases in charset.alias (glib)
264 if (open CHARSET_ALIAS
, $alias_file)
266 while (<CHARSET_ALIAS
>)
269 return $1 if (/^\s*([-._a-zA-Z0-9]+)\s+$encoding\b/i)
275 # if not found, return input string
282 my ($in_po_file) = @_;
285 open IN_PO_FILE
, $in_po_file or die;
288 ## example: "Content-Type: text/plain; charset=ISO-8859-1\n"
289 if (/Content-Type\:.*charset=([-a-zA-Z0-9]+)\\n/)
299 print STDERR
"Warning: no encoding found in $in_po_file. Assuming ISO-8859-1\n" unless $QUIET_ARG;
300 $encoding = "ISO-8859-1";
303 system ("$iconv -f $encoding -t UTF-8 <$devnull 2>$devnull");
305 $encoding = get_local_charset
($encoding);
311 sub utf8_sanity_check
313 print STDERR
"Warning: option --pass-through has been removed.\n" if $PASS_THROUGH_ARG;
317 sub get_translation_database
320 &get_cached_translation_database
;
322 &create_translation_database
;
326 sub get_newest_po_age
330 foreach my $file (values %po_files_by_lang)
332 my $file_age = -M
$file;
333 $newest_age = $file_age if !$newest_age || $file_age < $newest_age;
336 $newest_age = 0 if !$newest_age;
343 print "Generating and caching the translation database\n" unless $QUIET_ARG;
345 &create_translation_database
;
347 open CACHE
, ">$cache_file" || die;
348 print CACHE
join "\x01", %translations;
354 print "Found cached translation database\n" unless $QUIET_ARG;
357 open CACHE
, "<$cache_file" || die;
363 %translations = split "\x01", $contents;
366 sub get_cached_translation_database
368 my $cache_file_age = -M
$cache_file;
369 if (defined $cache_file_age)
371 if ($cache_file_age <= &get_newest_po_age
)
376 print "Found too-old cached translation database\n" unless $QUIET_ARG;
382 sub create_translation_database
384 for my $lang (keys %po_files_by_lang)
386 my $po_file = $po_files_by_lang{$lang};
390 my $encoding = get_po_encoding
($po_file);
392 if (lc $encoding eq "utf-8")
394 open PO_FILE
, "<$po_file";
398 print "NOTICE: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
400 open PO_FILE
, "$iconv -f $encoding -t UTF-8 $po_file|";
405 open PO_FILE
, "<$po_file";
416 $nextfuzzy = 1 if /^#, fuzzy/;
418 if (/^msgid "((\\.|[^\\])*)"/ )
420 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
427 $msgid = unescape_po_string
($1);
434 if (/^msgstr "((\\.|[^\\])*)"/)
436 $msgstr = unescape_po_string
($1);
441 if (/^"((\\.|[^\\])*)"/)
443 $msgid .= unescape_po_string
($1) if $inmsgid;
444 $msgstr .= unescape_po_string
($1) if $inmsgstr;
447 $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
455 sub unescape_one_sequence
459 return "\\" if $sequence eq "\\\\";
460 return "\"" if $sequence eq "\\\"";
461 return "\n" if $sequence eq "\\n";
462 return "\r" if $sequence eq "\\r";
463 return "\t" if $sequence eq "\\t";
464 return "\b" if $sequence eq "\\b";
465 return "\f" if $sequence eq "\\f";
466 return "\a" if $sequence eq "\\a";
467 return chr(11) if $sequence eq "\\v"; # vertical tab, see ascii(7)
469 return chr(hex($1)) if ($sequence =~ /\\x([0-9a-fA-F]{2})/);
470 return chr(oct($1)) if ($sequence =~ /\\([0-7]{3})/);
472 # FIXME: Is \0 supported as well? Kenneth and Rodney don't want it, see bug #48489
477 sub unescape_po_string
481 $string =~ s/(\\x[0-9a-fA-F]{2}|\\[0-7]{3}|\\.)/unescape_one_sequence($1)/eg;
486 ## NOTE: deal with < - < but not > - > because it seems its ok to have
487 ## > in the entity. For further info please look at #84738.
500 # entity_encode: (string)
502 # Encode the given string to XML format (encode '<' etc).
506 my ($pre_encoded) = @_;
508 my @list_of_chars = unpack ('C*', $pre_encoded);
510 # with UTF-8 we only encode minimalistic
511 return join ('', map (&entity_encode_int_minimalist
, @list_of_chars));
514 sub entity_encode_int_minimalist
516 return """ if $_ == 34;
517 return "&" if $_ == 38;
518 return "'" if $_ == 39;
519 return "<" if $_ == 60;
523 sub entity_encoded_translation
525 my ($lang, $string) = @_;
527 my $translation = $translations{$lang, $string};
528 return $string if !$translation;
529 return entity_encode
($translation);
532 ## XML (bonobo-activation specific) merge code
534 sub ba_merge_translations
539 local $/; # slurp mode
540 open INPUT
, "<$FILE" or die "can't open $FILE: $!";
545 open OUTPUT
, ">$OUTFILE" or die "can't open $OUTFILE: $!";
546 # Binmode so that selftest works ok if using a native Win32 Perl...
547 binmode (OUTPUT
) if $^O
eq 'MSWin32';
549 while ($source =~ s
|^(.*?
)([ \t]*<\s
*$w+\s
+($w+\s
*=\s
*"$q"\s
*)+/?
>)([ \t]*\n)?
||s
)
553 my $node = $2 . "\n";
557 while (s/(\s)_($w+\s*=\s*"($q)")/$1$2/s) {
558 push @strings, entity_decode
($3);
563 for my $string (@strings)
565 for my $lang (keys %po_files_by_lang)
567 $langs{$lang} = 1 if $translations{$lang, $string};
571 for my $lang (sort keys %langs)
574 s/(\sname\s*=\s*)"($q)"/$1"$2-$lang"/s;
575 s/(\s)_($w+\s*=\s*")($q)"/$1 . $2 . entity_encoded_translation($lang, $3) . '"'/seg;
580 print OUTPUT
$source;
586 ## XML (non-bonobo-activation) merge code
589 # Process tag attributes
590 # Only parameter is a HASH containing attributes -> values mapping
591 sub getAttributeString
594 my $do_translate = shift || 0;
595 my $language = shift || "";
597 my $translate = shift;
598 foreach my $e (reverse(sort(keys %{ $sub }))) {
600 my $string = $sub->{$e};
603 $string =~ s/^[\s]+//;
604 $string =~ s/[\s]+$//;
606 if ($string =~ /^'.*'$/)
610 $string =~ s/^['"]//g;
611 $string =~ s/['"]$//g;
613 if ($do_translate && $key =~ /^_/) {
617 my $decode_string = entity_decode
($string);
618 my $translation = $translations{$language, $decode_string};
620 $translation = entity_encode
($translation);
621 $string = $translation;
625 $$translate = 2 if ($translate && (!$$translate)); # watch not to "overwrite" $translate
629 $result .= " $key=$quote$string$quote";
634 # Returns a translatable string from XML node, it works on contents of every node in XML::Parser tree
638 my $spacepreserve = shift || 0;
639 my @list = @
{ $ref };
642 my $count = scalar(@list);
643 my $attrs = $list[0];
646 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
647 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
649 while ($index < $count) {
650 my $type = $list[$index];
651 my $content = $list[$index+1];
655 # lets strip the whitespace here, and *ONLY* here
656 $content =~ s/\s+/ /gs if (!$spacepreserve);
659 } elsif ( "$type" ne "1" ) {
660 # We've got another element
662 $result .= getAttributeString
(@
{$content}[0], 0); # no nested translatable elements
664 my $subresult = getXMLstring
($content, $spacepreserve);
666 $result .= ">".$subresult . "</$type>";
679 # Translate list of nodes if necessary
680 sub translate_subnodes
684 my $language = shift || "";
685 my $singlelang = shift || 0;
686 my $spacepreserve = shift || 0;
688 my @nodes = @
{ $content };
690 my $count = scalar(@nodes);
692 while ($index < $count) {
693 my $type = $nodes[$index];
694 my $rest = $nodes[$index+1];
696 my $oldMO = $MULTIPLE_OUTPUT;
697 $MULTIPLE_OUTPUT = 1;
698 traverse
($fh, $type, $rest, $language, $spacepreserve);
699 $MULTIPLE_OUTPUT = $oldMO;
701 traverse
($fh, $type, $rest, $language, $spacepreserve);
707 sub isWellFormedXmlFragment
709 my $ret = eval 'require XML::Parser';
711 die "You must have XML::Parser installed to run $0\n\n";
714 my $fragment = shift;
715 return 0 if (!$fragment);
717 $fragment = "<root>$fragment</root>";
718 my $xp = new XML
::Parser
(Style
=> 'Tree');
720 eval { $tree = $xp->parse($fragment); };
727 my $nodename = shift;
729 my $language = shift || "";
730 my $spacepreserve = shift || 0;
733 if ($content =~ /^[\s]*$/) {
734 $leading_space .= $content;
739 my @all = @
{ $content };
740 my $attrs = shift @all;
742 my $outattr = getAttributeString
($attrs, 1, $language, \
$translate);
744 if ($nodename =~ /^_/) {
750 $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
751 $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
753 print $fh "<$nodename", $outattr;
755 $lookup = getXMLstring
($content, $spacepreserve);
756 if (!$spacepreserve) {
757 $lookup =~ s/^\s+//s;
758 $lookup =~ s/\s+$//s;
761 if ($lookup || $translate == 2) {
762 my $translation = $translations{$language, $lookup} if isWellFormedXmlFragment
($translations{$language, $lookup});
763 if ($MULTIPLE_OUTPUT && ($translation || $translate == 2)) {
764 $translation = $lookup if (!$translation);
765 print $fh " xml:lang=\"", $language, "\"" if $language;
767 if ($translate == 2) {
768 translate_subnodes
($fh, \
@all, $language, 1, $spacepreserve);
770 print $fh $translation;
772 print $fh "</$nodename>";
774 return; # this means there will be no same translation with xml:lang="$language"...
775 # if we want them both, just remove this "return"
778 if ($translate == 2) {
779 translate_subnodes
($fh, \
@all, $language, 1, $spacepreserve);
783 print $fh "</$nodename>";
789 for my $lang (sort keys %po_files_by_lang) {
790 if ($MULTIPLE_OUTPUT && $lang ne "$language") {
797 my $localattrs = getAttributeString
($attrs, 1, $lang, \
$translate);
798 my $translation = $translations{$lang, $lookup} if isWellFormedXmlFragment
($translations{$lang, $lookup});
799 if ($translate && !$translation) {
800 $translation = $lookup;
803 if ($translation || $translate) {
805 $leading_space =~ s/.*\n//g;
806 print $fh $leading_space;
807 print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs, ">";
808 if ($translate == 2) {
809 translate_subnodes
($fh, \
@all, $lang, 1, $spacepreserve);
811 print $fh $translation;
813 print $fh "</$nodename>";
819 my $count = scalar(@all);
823 while ($index < $count) {
824 my $type = $all[$index];
825 my $rest = $all[$index+1];
826 traverse
($fh, $type, $rest, $language, $spacepreserve);
829 print $fh "</$nodename>";
837 sub intltool_tree_comment
841 my $clist = $expat->{Curlist
};
844 push @
$clist, 1 => $data;
847 sub intltool_tree_cdatastart
850 my $clist = $expat->{Curlist
};
853 push @
$clist, 0 => $expat->original_string();
856 sub intltool_tree_cdataend
859 my $clist = $expat->{Curlist
};
862 $clist->[$pos] .= $expat->original_string();
865 sub intltool_tree_char
869 my $clist = $expat->{Curlist
};
872 # Use original_string so that we retain escaped entities
875 if ($pos > 0 and $clist->[$pos - 1] eq '0') {
876 $clist->[$pos] .= $expat->original_string();
878 push @
$clist, 0 => $expat->original_string();
882 sub intltool_tree_start
888 # Use original_string so that we retain escaped entities
889 # in attribute values. We must convert the string to an
890 # @origlist array to conform to the structure of the Tree
893 my @original_array = split /\x/, $expat->original_string();
894 my $source = $expat->original_string();
896 # Remove leading tag.
898 $source =~ s
|^\s
*<\s
*(\S
+)||s
;
900 # Grab attribute key/value pairs and push onto @origlist array.
904 if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
906 $source =~ s
|^\s
*([\w
:-]+)\s
*[=]\s
*["]([^"]*)["]||s;
908 push @origlist, '"' . $2 . '"';
910 elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
912 $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
914 push @origlist, "'" . $2 . "'";
922 my $ol = [ { @origlist } ];
924 push @{ $expat->{Lists} }, $expat->{Curlist};
925 push @{ $expat->{Curlist} }, $tag => $ol;
926 $expat->{Curlist} = $ol;
931 my $filename = shift || return;
933 die "ERROR Cannot find filename
: $filename\n";
936 my $ret = eval 'require XML::Parser';
938 die "You must have XML
::Parser installed to run
$0\n\n";
940 my $xp = new XML::Parser(Style => 'Tree');
941 $xp->setHandlers(Char => \&intltool_tree_char);
942 $xp->setHandlers(Start => \&intltool_tree_start);
943 $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
944 $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
945 my $tree = $xp->parsefile($filename);
947 # <foo><head id="a
">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
949 # [foo, [{}, head, [{id => "a
"}, 0, "Hello
", em, [{}, 0, "there
"]], bar, [{},
950 # 0, "Howdy
", ref, [{}]], 0, "do" ] ]
962 die "ERROR Cannot find filename
: $infile\n";
965 print $fh qq{<?xml version="1.0" encoding="UTF
-8"?>\n};
968 open DOCINPUT, "<${FILE
}" or die;
969 $source = <DOCINPUT>;
972 if ($source =~ /(<!DOCTYPE.*\[.*\]\s*>)/s)
976 elsif ($source =~ /(<!DOCTYPE[^>]*>)/s)
986 my $language = shift || "";
988 my $name = shift @{ $ref };
989 my $cont = shift @{ $ref };
991 while (!$name || "$name" eq "1") {
992 $name = shift @{ $ref };
993 $cont = shift @{ $ref };
996 my $spacepreserve = 0;
997 my $attrs = @{$cont}[0];
998 $spacepreserve = 1 if ((exists $attrs->{"xml
:space
"}) && ($attrs->{"xml
:space
"} =~ /^["']?preserve["']?
$/));
1000 traverse
($fh, $name, $cont, $language, $spacepreserve);
1003 sub xml_merge_output
1007 if ($MULTIPLE_OUTPUT) {
1008 for my $lang (sort keys %po_files_by_lang) {
1010 mkdir $lang or die "Cannot create subdirectory $lang: $!\n";
1012 open OUTPUT
, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1013 binmode (OUTPUT
) if $^O
eq 'MSWin32';
1014 my $tree = readXml
($FILE);
1015 print_header
($FILE, \
*OUTPUT
);
1016 parseTree
(\
*OUTPUT
, $tree, $lang);
1018 print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1021 open OUTPUT
, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
1022 binmode (OUTPUT
) if $^O
eq 'MSWin32';
1023 my $tree = readXml
($FILE);
1024 print_header
($FILE, \
*OUTPUT
);
1025 parseTree
(\
*OUTPUT
, $tree);
1027 print "CREATED $OUTFILE\n" unless $QUIET_ARG;
1030 sub keys_merge_translations
1032 open INPUT
, "<${FILE}" or die;
1033 open OUTPUT
, ">${OUTFILE}" or die;
1034 binmode (OUTPUT
) if $^O
eq 'MSWin32';
1038 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1044 my $non_translated_line = $_;
1046 for my $lang (sort keys %po_files_by_lang)
1048 my $translation = $translations{$lang, $string};
1049 next if !$translation;
1051 $_ = $non_translated_line;
1052 s/(\w+)=.*/[$lang]$1=$translation/;
1066 sub desktop_merge_translations
1068 open INPUT
, "<${FILE}" or die;
1069 open OUTPUT
, ">${OUTFILE}" or die;
1070 binmode (OUTPUT
) if $^O
eq 'MSWin32';
1074 if (s/^(\s*)_(\w+=(.*))/$1$2/)
1080 my $non_translated_line = $_;
1082 for my $lang (sort keys %po_files_by_lang)
1084 my $translation = $translations{$lang, $string};
1085 next if !$translation;
1087 $_ = $non_translated_line;
1088 s/(\w+)=.*/${1}[$lang]=$translation/;
1102 sub schemas_merge_translations
1107 local $/; # slurp mode
1108 open INPUT
, "<$FILE" or die "can't open $FILE: $!";
1113 open OUTPUT
, ">$OUTFILE" or die;
1114 binmode (OUTPUT
) if $^O
eq 'MSWin32';
1116 # FIXME: support attribute translations
1118 # Empty nodes never need translation, so unmark all of them.
1119 # For example, <_foo/> is just replaced by <foo/>.
1120 $source =~ s
|<\s
*_
($w+)\s
*/>|<$1/>|g
;
1122 while ($source =~ s
/
1124 (\s
+)(<locale\ name
="C">(\s
*)
1125 (<default>\s
*(?
:<!--[^>]*?
-->\s
*)?
(.*?
)\s
*<\
/default>)?
(\s
*)
1126 (<short
>\s
*(?
:<!--[^>]*?
-->\s
*)?
(.*?
)\s
*<\
/short
>)?
(\s
*)
1127 (<long
>\s
*(?
:<!--[^>]*?
-->\s
*)?
(.*?
)\s
*<\
/long
>)?
(\s
*)
1133 my $locale_start_spaces = $2 ?
$2 : '';
1134 my $default_spaces = $4 ?
$4 : '';
1135 my $short_spaces = $7 ?
$7 : '';
1136 my $long_spaces = $10 ?
$10 : '';
1137 my $locale_end_spaces = $13 ?
$13 : '';
1138 my $c_default_block = $3 ?
$3 : '';
1139 my $default_string = $6 ?
$6 : '';
1140 my $short_string = $9 ?
$9 : '';
1141 my $long_string = $12 ?
$12 : '';
1143 print OUTPUT
"$locale_start_spaces$c_default_block";
1145 $default_string =~ s/\s+/ /g;
1146 $default_string = entity_decode
($default_string);
1147 $short_string =~ s/\s+/ /g;
1148 $short_string = entity_decode
($short_string);
1149 $long_string =~ s/\s+/ /g;
1150 $long_string = entity_decode
($long_string);
1152 for my $lang (sort keys %po_files_by_lang)
1154 my $default_translation = $translations{$lang, $default_string};
1155 my $short_translation = $translations{$lang, $short_string};
1156 my $long_translation = $translations{$lang, $long_string};
1158 next if (!$default_translation && !$short_translation &&
1159 !$long_translation);
1161 print OUTPUT
"\n$locale_start_spaces<locale name=\"$lang\">";
1163 print OUTPUT
"$default_spaces";
1165 if ($default_translation)
1167 $default_translation = entity_encode
($default_translation);
1168 print OUTPUT
"<default>$default_translation</default>";
1171 print OUTPUT
"$short_spaces";
1173 if ($short_translation)
1175 $short_translation = entity_encode
($short_translation);
1176 print OUTPUT
"<short>$short_translation</short>";
1179 print OUTPUT
"$long_spaces";
1181 if ($long_translation)
1183 $long_translation = entity_encode
($long_translation);
1184 print OUTPUT
"<long>$long_translation</long>";
1187 print OUTPUT
"$locale_end_spaces</locale>";
1191 print OUTPUT
$source;
1196 sub rfc822deb_merge_translations
1199 for my $lang (keys %po_files_by_lang) {
1200 $encodings{$lang} = ($UTF8_ARG ?
'UTF-8' : get_po_encoding
($po_files_by_lang{$lang}));
1205 $Text::Wrap
::huge
= 'overflow';
1206 $Text::Wrap
::break = qr/\n|\s(?=\S)/;
1209 local $/; # slurp mode
1210 open INPUT
, "<$FILE" or die "can't open $FILE: $!";
1215 open OUTPUT
, ">${OUTFILE}" or die;
1216 binmode (OUTPUT
) if $^O
eq 'MSWin32';
1218 while ($source =~ /(^|\n+)(_*)([^:\s]+)(:[ \t]*)(.*?)(?=\n[\S\n]|$)/sg)
1221 my $non_translated_line = $3.$4;
1223 my $underscore = length($2);
1224 next if $underscore eq 0 && $non_translated_line =~ /^#/;
1225 # Remove [] dummy strings
1226 my $stripped = $string;
1227 $stripped =~ s/\[\s[^\[\]]*\],/,/g if $underscore eq 2;
1228 $stripped =~ s/\[\s[^\[\]]*\]$//;
1229 $non_translated_line .= $stripped;
1231 print OUTPUT
$sep.$non_translated_line;
1235 my @str_list = rfc822deb_split
($underscore, $string);
1237 for my $lang (sort keys %po_files_by_lang)
1239 my $is_translated = 1;
1240 my $str_translated = '';
1243 for my $str (@str_list)
1245 my $translation = $translations{$lang, $str};
1253 # $translation may also contain [] dummy
1254 # strings, mostly to indicate an empty string
1255 $translation =~ s/\[\s[^\[\]]*\]$//;
1259 if ($underscore eq 2)
1261 $str_translated .= $translation;
1266 Text
::Tabs
::expand
($translation) .
1272 if ($underscore eq 2)
1274 $str_translated .= ', ' . $translation;
1278 $str_translated .= Text
::Tabs
::expand
(
1279 Text
::Wrap
::wrap
(' ', ' ', $translation)) .
1285 # To fix some problems with Text::Wrap::wrap
1286 $str_translated =~ s/(\n )+\n/\n .\n/g;
1288 next unless $is_translated;
1290 $str_translated =~ s/\n \.\n$//;
1291 $str_translated =~ s/\s+$//;
1293 $_ = $non_translated_line;
1294 s/^(\w+):\s*.*/$sep${1}-$lang.$encodings{$lang}: $str_translated/s;
1307 # Debian defines a special way to deal with rfc822-style files:
1308 # when a value contain newlines, it consists of
1309 # 1. a short form (first line)
1310 # 2. a long description, all lines begin with a space,
1311 # and paragraphs are separated by a single dot on a line
1312 # This routine returns an array of all paragraphs, and reformat
1314 # When first argument is 2, the string is a comma separated list of
1318 $text =~ s/^[ \t]//mg;
1319 return (split(/, */, $text, 0)) if $type ne 1;
1320 return ($text) if $text !~ /\n/;
1322 $text =~ s/([^\n]*)\n//;
1326 for my $line (split (/\n/, $text))
1329 if ($line =~ /^\.\s*$/)
1336 elsif ($line =~ /^\s/)
1338 # Line which must not be reformatted
1339 $str .= "\n" if length ($str) && $str !~ /\n$/;
1345 # Continuation line, remove newline
1346 $str .= " " if length ($str) && $str !~ /\n$/;
1352 push(@list, $str) if length ($str);