~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to intltool-merge.in

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2006-12-15 23:08:17 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215230817-exr5ks1hd73s3tlk
Tags: 8.2.2-1
* New upstream bugfix release, fixes among other things the support for
  the version of gnucap shipped in etch.
* Add missing dependency on python-gtk2 (Closes: #396523).
* Removed reference to non-existent sound file from memory.c (upstream
  fix - impacts 8.2 as well).  
* Now suggests gnuchess, gnucap, and tuxpaint.
* Updated extended description for the main package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
## Release information
36
36
my $PROGRAM = "intltool-merge";
37
37
my $PACKAGE = "intltool";
38
 
my $VERSION = "0.32.1";
 
38
my $VERSION = "0.35.0";
39
39
 
40
40
## Loaded modules
41
41
use strict; 
60
60
my $DESKTOP_STYLE_ARG = 0;
61
61
my $SCHEMAS_STYLE_ARG = 0;
62
62
my $RFC822DEB_STYLE_ARG = 0;
 
63
my $QUOTED_STYLE_ARG = 0;
63
64
my $QUIET_ARG = 0;
64
65
my $PASS_THROUGH_ARG = 0;
65
66
my $UTF8_ARG = 0;
79
80
 "desktop-style|d" => \$DESKTOP_STYLE_ARG,
80
81
 "schemas-style|s" => \$SCHEMAS_STYLE_ARG,
81
82
 "rfc822deb-style|r" => \$RFC822DEB_STYLE_ARG,
 
83
 "quoted-style" => \$QUOTED_STYLE_ARG,
82
84
 "pass-through|p" => \$PASS_THROUGH_ARG,
83
85
 "utf8|u" => \$UTF8_ARG,
84
86
 "multiple-output|m" => \$MULTIPLE_OUTPUT,
92
94
my %po_files_by_lang = ();
93
95
my %translations = ();
94
96
my $iconv = $ENV{"ICONV"} || $ENV{"INTLTOOL_ICONV"} || "@INTLTOOL_ICONV@";
 
97
my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
95
98
 
96
99
# Use this instead of \w for XML files to handle more possible characters.
97
100
my $w = "[-A-Za-z0-9._:]";
156
159
        &rfc822deb_merge_translations;
157
160
        &finalize;
158
161
 
162
elsif ($QUOTED_STYLE_ARG && @ARGV > 2) 
 
163
{
 
164
        &utf8_sanity_check;
 
165
        &preparation;
 
166
        &print_message;
 
167
        &quoted_merge_translations;
 
168
        &finalize;
 
169
159
170
else 
160
171
{
161
172
        &print_help;
192
203
  -k, --keys-style       includes translations in the keys style
193
204
  -s, --schemas-style    includes translations in the schemas style
194
205
  -r, --rfc822deb-style  includes translations in the RFC822 style
 
206
      --quoted-style     includes translations in the quoted string style
195
207
  -x, --xml-style        includes translations in the standard xml style
196
208
 
197
209
Other options:
256
268
sub get_local_charset
257
269
{
258
270
    my ($encoding) = @_;
259
 
    my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "/usr/lib/charset.alias";
 
271
    my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "@INTLTOOL_LIBDIR@/charset.alias";
260
272
 
261
273
    # seek character encoding aliases in charset.alias (glib)
262
274
 
299
311
        $encoding = "ISO-8859-1";
300
312
    }
301
313
 
302
 
    system ("$iconv -f $encoding -t UTF-8 </dev/null 2>/dev/null");
 
314
    system ("$iconv -f $encoding -t UTF-8 <$devnull 2>$devnull");
303
315
    if ($?) {
304
316
        $encoding = get_local_charset($encoding);
305
317
    }
394
406
            } 
395
407
            else 
396
408
            {
397
 
                print STDERR "WARNING: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
 
409
                print "NOTICE: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
398
410
 
399
411
                open PO_FILE, "$iconv -f $encoding -t UTF-8 $po_file|"; 
400
412
            }
542
554
    }
543
555
 
544
556
    open OUTPUT, ">$OUTFILE" or die "can't open $OUTFILE: $!";
 
557
    # Binmode so that selftest works ok if using a native Win32 Perl...
 
558
    binmode (OUTPUT) if $^O eq 'MSWin32';
545
559
 
546
560
    while ($source =~ s|^(.*?)([ \t]*<\s*$w+\s+($w+\s*=\s*"$q"\s*)+/?>)([ \t]*\n)?||s) 
547
561
    {
610
624
        if ($do_translate && $key =~ /^_/) {
611
625
            $key =~ s|^_||g;
612
626
            if ($language) {
613
 
                
614
627
                # Handle translation
615
 
                #
616
628
                my $decode_string = entity_decode($string);
617
629
                my $translation = $translations{$language, $decode_string};
618
630
                if ($translation) {
619
631
                    $translation = entity_encode($translation);
620
632
                    $string = $translation;
621
 
                    $$translate = 2;
622
 
                }
 
633
                }
 
634
                $$translate = 2;
623
635
            } else {
624
 
                 $$translate = 1 if ($translate && (!$$translate)); # watch not to "overwrite" if $translate == 2
 
636
                 $$translate = 2 if ($translate && (!$$translate)); # watch not to "overwrite" $translate
625
637
            }
626
638
        }
627
639
        
631
643
}
632
644
 
633
645
# Returns a translatable string from XML node, it works on contents of every node in XML::Parser tree
634
 
#   doesn't support nesting of translatable tags (i.e. <_blah>this <_doh>doesn't</_doh> work</_blah> -- besides
635
 
#   can you define the correct semantics for this?)
636
 
#
637
 
 
638
646
sub getXMLstring
639
647
{
640
648
    my $ref = shift;
 
649
    my $spacepreserve = shift || 0;
641
650
    my @list = @{ $ref };
642
651
    my $result = "";
643
652
 
645
654
    my $attrs = $list[0];
646
655
    my $index = 1;
647
656
 
 
657
    $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
 
658
    $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
 
659
 
648
660
    while ($index < $count) {
649
661
        my $type = $list[$index];
650
662
        my $content = $list[$index+1];
652
664
            # We've got CDATA
653
665
            if ($content) {
654
666
                # lets strip the whitespace here, and *ONLY* here
655
 
                $content =~ s/\s+/ /gs if (!((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/)));
656
 
                $result .= ($content);
657
 
            } else {
658
 
                #print "no cdata content when expected it\n"; # is this possible, is this ok?
659
 
                # what to do if this happens?
660
 
                # Did I mention that I hate XML::Parser tree style?
 
667
                $content =~ s/\s+/ /gs if (!$spacepreserve);
 
668
                $result .= $content;
661
669
            }
662
 
        } else {
 
670
        } elsif ( "$type" ne "1" ) {
663
671
            # We've got another element
664
672
            $result .= "<$type";
665
673
            $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
666
674
            if ($content) {
667
 
                my $subresult = getXMLstring($content);
 
675
                my $subresult = getXMLstring($content, $spacepreserve);
668
676
                if ($subresult) {
669
677
                    $result .= ">".$subresult . "</$type>";
670
678
                } else {
679
687
    return $result;
680
688
}
681
689
 
 
690
# Translate list of nodes if necessary
 
691
sub translate_subnodes
 
692
{
 
693
    my $fh = shift;
 
694
    my $content = shift;
 
695
    my $language = shift || "";
 
696
    my $singlelang = shift || 0;
 
697
    my $spacepreserve = shift || 0;
 
698
 
 
699
    my @nodes = @{ $content };
 
700
 
 
701
    my $count = scalar(@nodes);
 
702
    my $index = 0;
 
703
    while ($index < $count) {
 
704
        my $type = $nodes[$index];
 
705
        my $rest = $nodes[$index+1];
 
706
        if ($singlelang) {
 
707
            my $oldMO = $MULTIPLE_OUTPUT;
 
708
            $MULTIPLE_OUTPUT = 1;
 
709
            traverse($fh, $type, $rest, $language, $spacepreserve);
 
710
            $MULTIPLE_OUTPUT = $oldMO;
 
711
        } else {
 
712
            traverse($fh, $type, $rest, $language, $spacepreserve);
 
713
        }
 
714
        $index += 2;
 
715
    }
 
716
}
 
717
 
 
718
sub isWellFormedXmlFragment
 
719
{
 
720
    my $ret = eval 'require XML::Parser';
 
721
    if(!$ret) {
 
722
        die "You must have XML::Parser installed to run $0\n\n";
 
723
    } 
 
724
 
 
725
    my $fragment = shift;
 
726
    return 0 if (!$fragment);
 
727
 
 
728
    $fragment = "<root>$fragment</root>";
 
729
    my $xp = new XML::Parser(Style => 'Tree');
 
730
    my $tree = 0;
 
731
    eval { $tree = $xp->parse($fragment); };
 
732
    return $tree;
 
733
}
 
734
 
682
735
sub traverse
683
736
{
684
737
    my $fh = shift; 
685
738
    my $nodename = shift;
686
739
    my $content = shift;
687
740
    my $language = shift || "";
 
741
    my $spacepreserve = shift || 0;
688
742
 
689
743
    if (!$nodename) {
690
744
        if ($content =~ /^[\s]*$/) {
703
757
            $nodename =~ s/^_//;
704
758
        }
705
759
        my $lookup = '';
 
760
 
 
761
        $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
 
762
        $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
 
763
 
706
764
        print $fh "<$nodename", $outattr;
707
765
        if ($translate) {
708
 
            $lookup = getXMLstring($content);
709
 
            if (!((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/))) {
 
766
            $lookup = getXMLstring($content, $spacepreserve);
 
767
            if (!$spacepreserve) {
710
768
                $lookup =~ s/^\s+//s;
711
769
                $lookup =~ s/\s+$//s;
712
770
            }
713
771
 
714
772
            if ($lookup || $translate == 2) {
715
 
                my $translation = $translations{$language, $lookup};
 
773
                my $translation = $translations{$language, $lookup} if isWellFormedXmlFragment($translations{$language, $lookup});
716
774
                if ($MULTIPLE_OUTPUT && ($translation || $translate == 2)) {
717
775
                    $translation = $lookup if (!$translation);
718
 
                    print $fh " xml:lang=\"", $language, "\"";
719
 
                    print $fh ">", $translation, "</$nodename>";
 
776
                    print $fh " xml:lang=\"", $language, "\"" if $language;
 
777
                    print $fh ">";
 
778
                    if ($translate == 2) {
 
779
                        translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
 
780
                    } else {
 
781
                        print $fh $translation;
 
782
                    }
 
783
                    print $fh "</$nodename>";
 
784
 
720
785
                    return; # this means there will be no same translation with xml:lang="$language"...
721
786
                            # if we want them both, just remove this "return"
722
787
                } else {
723
 
                    print $fh ">$lookup</$nodename>";
 
788
                    print $fh ">";
 
789
                    if ($translate == 2) {
 
790
                        translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
 
791
                    } else {
 
792
                        print $fh $lookup;
 
793
                    }
 
794
                    print $fh "</$nodename>";
724
795
                }
725
796
            } else {
726
797
                print $fh "/>";
735
806
                        #
736
807
                        my $translate = 0;
737
808
                        my $localattrs = getAttributeString($attrs, 1, $lang, \$translate);
738
 
                        my $translation = $translations{$lang, $lookup};
 
809
                        my $translation = $translations{$lang, $lookup} if isWellFormedXmlFragment($translations{$lang, $lookup});
739
810
                        if ($translate && !$translation) {
740
811
                            $translation = $lookup;
741
812
                        }
742
813
 
743
814
                        if ($translation || $translate) {
744
 
                            $translation = ($translation);
745
815
                            print $fh "\n";
746
816
                            $leading_space =~ s/.*\n//g;
747
817
                            print $fh $leading_space;
748
 
                            print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs;
749
 
                            print $fh ">", $translation , "</$nodename>";
 
818
                            print $fh "<", $nodename, " xml:lang=\"", $lang, "\"", $localattrs, ">";
 
819
                            if ($translate == 2) {
 
820
                               translate_subnodes($fh, \@all, $lang, 1, $spacepreserve);
 
821
                            } else {
 
822
                                print $fh $translation;
 
823
                            }
 
824
                            print $fh "</$nodename>";
750
825
                        }
751
826
                    }
752
827
            }
755
830
            my $count = scalar(@all);
756
831
            if ($count > 0) {
757
832
                print $fh ">";
758
 
            } else {
759
 
                print $fh "/>";
760
 
            }
761
 
            my $index = 0;
762
 
            while ($index < $count) {
763
 
                my $type = $all[$index];
764
 
                my $rest = $all[$index+1];
765
 
                traverse($fh, $type, $rest, $language);
766
 
                $index += 2;
767
 
            }
768
 
            if ($count > 0) {
 
833
                my $index = 0;
 
834
                while ($index < $count) {
 
835
                    my $type = $all[$index];
 
836
                    my $rest = $all[$index+1];
 
837
                    traverse($fh, $type, $rest, $language, $spacepreserve);
 
838
                    $index += 2;
 
839
                }
769
840
                print $fh "</$nodename>";
 
841
            } else {
 
842
                print $fh "/>";
770
843
            }
771
844
        }
772
845
    }
773
846
}
774
847
 
 
848
sub intltool_tree_comment
 
849
{
 
850
    my $expat = shift;
 
851
    my $data  = shift;
 
852
    my $clist = $expat->{Curlist};
 
853
    my $pos   = $#$clist;
 
854
 
 
855
    push @$clist, 1 => $data;
 
856
}
 
857
 
 
858
sub intltool_tree_cdatastart
 
859
{
 
860
    my $expat    = shift;
 
861
    my $clist = $expat->{Curlist};
 
862
    my $pos   = $#$clist;
 
863
 
 
864
    push @$clist, 0 => $expat->original_string();
 
865
}
 
866
 
 
867
sub intltool_tree_cdataend
 
868
{
 
869
    my $expat    = shift;
 
870
    my $clist = $expat->{Curlist};
 
871
    my $pos   = $#$clist;
 
872
 
 
873
    $clist->[$pos] .= $expat->original_string();
 
874
}
 
875
 
775
876
sub intltool_tree_char
776
877
{
777
878
    my $expat = shift;
850
951
    my $xp = new XML::Parser(Style => 'Tree');
851
952
    $xp->setHandlers(Char => \&intltool_tree_char);
852
953
    $xp->setHandlers(Start => \&intltool_tree_start);
 
954
    $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
 
955
    $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
853
956
    my $tree = $xp->parsefile($filename);
854
957
 
855
958
# <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
895
998
 
896
999
    my $name = shift @{ $ref };
897
1000
    my $cont = shift @{ $ref };
898
 
    traverse($fh, $name, $cont, $language);
 
1001
    
 
1002
    while (!$name || "$name" eq "1") {
 
1003
        $name = shift @{ $ref };
 
1004
        $cont = shift @{ $ref };
 
1005
    }
 
1006
 
 
1007
    my $spacepreserve = 0;
 
1008
    my $attrs = @{$cont}[0];
 
1009
    $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
 
1010
 
 
1011
    traverse($fh, $name, $cont, $language, $spacepreserve);
899
1012
}
900
1013
 
901
1014
sub xml_merge_output
908
1021
                mkdir $lang or die "Cannot create subdirectory $lang: $!\n";
909
1022
            }
910
1023
            open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
 
1024
            binmode (OUTPUT) if $^O eq 'MSWin32';
911
1025
            my $tree = readXml($FILE);
912
1026
            print_header($FILE, \*OUTPUT);
913
1027
            parseTree(\*OUTPUT, $tree, $lang);
916
1030
        }
917
1031
    } 
918
1032
    open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
 
1033
    binmode (OUTPUT) if $^O eq 'MSWin32';
919
1034
    my $tree = readXml($FILE);
920
1035
    print_header($FILE, \*OUTPUT);
921
1036
    parseTree(\*OUTPUT, $tree);
927
1042
{
928
1043
    open INPUT, "<${FILE}" or die;
929
1044
    open OUTPUT, ">${OUTFILE}" or die;
 
1045
    binmode (OUTPUT) if $^O eq 'MSWin32';
930
1046
 
931
1047
    while (<INPUT>) 
932
1048
    {
962
1078
{
963
1079
    open INPUT, "<${FILE}" or die;
964
1080
    open OUTPUT, ">${OUTFILE}" or die;
 
1081
    binmode (OUTPUT) if $^O eq 'MSWin32';
965
1082
 
966
1083
    while (<INPUT>) 
967
1084
    {
1005
1122
    }
1006
1123
 
1007
1124
    open OUTPUT, ">$OUTFILE" or die;
 
1125
    binmode (OUTPUT) if $^O eq 'MSWin32';
1008
1126
 
1009
1127
    # FIXME: support attribute translations
1010
1128
 
1106
1224
    }
1107
1225
 
1108
1226
    open OUTPUT, ">${OUTFILE}" or die;
 
1227
    binmode (OUTPUT) if $^O eq 'MSWin32';
1109
1228
 
1110
1229
    while ($source =~ /(^|\n+)(_*)([^:\s]+)(:[ \t]*)(.*?)(?=\n[\S\n]|$)/sg)
1111
1230
    {
1246
1365
    return @list;
1247
1366
}
1248
1367
 
 
1368
sub quoted_translation
 
1369
{
 
1370
    my ($lang, $string) = @_;
 
1371
 
 
1372
    $string =~ s/\\\"/\"/g;
 
1373
 
 
1374
    my $translation = $translations{$lang, $string};
 
1375
    $translation = $string if !$translation;
 
1376
 
 
1377
    $translation =~ s/\"/\\\"/g;
 
1378
    return $translation
 
1379
}
 
1380
 
 
1381
sub quoted_merge_translations
 
1382
{
 
1383
    if (!$MULTIPLE_OUTPUT) {
 
1384
        print "Quoted only supports Multiple Output.\n";
 
1385
        exit(1);
 
1386
    }
 
1387
 
 
1388
    for my $lang (sort keys %po_files_by_lang) {
 
1389
        if ( ! -e $lang ) {
 
1390
            mkdir $lang or die "Cannot create subdirectory $lang: $!\n";
 
1391
        }
 
1392
        open INPUT, "<${FILE}" or die;
 
1393
        open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
 
1394
        binmode (OUTPUT) if $^O eq 'MSWin32';
 
1395
        while (<INPUT>) 
 
1396
        {
 
1397
            s/\"(([^\"]|\\\")*[^\\\"])\"/"\"" . &quoted_translation($lang, $1) . "\""/ge;
 
1398
            print OUTPUT;
 
1399
        }
 
1400
        close OUTPUT;
 
1401
        close INPUT;
 
1402
    }
 
1403
}