~ubuntu-branches/ubuntu/karmic/xarchiver/karmic

« back to all changes in this revision

Viewing changes to intltool-merge.in

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-11-07 14:54:00 UTC
  • mfrom: (1.1.7 upstream) (2.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20081107145400-z0j3jmgads8coae2
Tags: 0.5.1-1
MergingĀ upstreamĀ versionĀ 0.5.1.

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.34.2";
 
38
my $VERSION = "0.37.1";
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;
 
64
my $QUOTEDXML_STYLE_ARG = 0;
63
65
my $QUIET_ARG = 0;
64
66
my $PASS_THROUGH_ARG = 0;
65
67
my $UTF8_ARG = 0;
79
81
 "desktop-style|d" => \$DESKTOP_STYLE_ARG,
80
82
 "schemas-style|s" => \$SCHEMAS_STYLE_ARG,
81
83
 "rfc822deb-style|r" => \$RFC822DEB_STYLE_ARG,
 
84
 "quoted-style" => \$QUOTED_STYLE_ARG,
 
85
 "quotedxml-style" => \$QUOTEDXML_STYLE_ARG,
82
86
 "pass-through|p" => \$PASS_THROUGH_ARG,
83
87
 "utf8|u" => \$UTF8_ARG,
84
88
 "multiple-output|m" => \$MULTIPLE_OUTPUT,
91
95
 
92
96
my %po_files_by_lang = ();
93
97
my %translations = ();
94
 
my $iconv = $ENV{"ICONV"} || $ENV{"INTLTOOL_ICONV"} || "@INTLTOOL_ICONV@";
 
98
my $iconv = $ENV{"ICONV"} || "iconv";
95
99
my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
96
100
 
 
101
sub isProgramInPath
 
102
{
 
103
    my ($file) = @_;
 
104
    # If either a file exists, or when run it returns 0 exit status
 
105
    return 1 if ((-x $file) or (system("$file -l >$devnull") == 0));
 
106
    return 0;
 
107
}
 
108
 
 
109
if (! isProgramInPath ("$iconv"))
 
110
{
 
111
        print STDERR " *** iconv is not found on this system!\n".
 
112
                     " *** Without it, intltool-merge can not convert encodings.\n";
 
113
        exit;
 
114
}
 
115
 
97
116
# Use this instead of \w for XML files to handle more possible characters.
98
117
my $w = "[-A-Za-z0-9._:]";
99
118
 
131
150
        &utf8_sanity_check;
132
151
        &preparation;
133
152
        &print_message;
134
 
        &keys_merge_translations;
 
153
        &keys_merge_translations;
135
154
        &finalize;
136
155
137
156
elsif ($DESKTOP_STYLE_ARG && @ARGV > 2) 
157
176
        &rfc822deb_merge_translations;
158
177
        &finalize;
159
178
 
179
elsif (($QUOTED_STYLE_ARG || $QUOTEDXML_STYLE_ARG) && @ARGV > 2)
 
180
{
 
181
        &utf8_sanity_check;
 
182
        &preparation;
 
183
        &print_message;
 
184
        &quoted_merge_translations($QUOTEDXML_STYLE_ARG);
 
185
        &finalize;
 
186
160
187
else 
161
188
{
162
189
        &print_help;
193
220
  -k, --keys-style       includes translations in the keys style
194
221
  -s, --schemas-style    includes translations in the schemas style
195
222
  -r, --rfc822deb-style  includes translations in the RFC822 style
 
223
      --quoted-style     includes translations in the quoted string style
 
224
      --quotedxml-style  includes translations in the quoted xml string style
196
225
  -x, --xml-style        includes translations in the standard xml style
197
226
 
198
227
Other options:
249
278
 
250
279
sub gather_po_files
251
280
{
252
 
    for my $po_file (glob "$PO_DIR/*.po") {
253
 
        $po_files_by_lang{po_file2lang($po_file)} = $po_file;
 
281
    if (my $linguas = $ENV{"LINGUAS"})
 
282
    {
 
283
        for my $lang (split / /, $linguas) {
 
284
            my $po_file = $PO_DIR . "/" . $lang . ".po";
 
285
            if (-e $po_file) {
 
286
                $po_files_by_lang{$lang} = $po_file;
 
287
            }
 
288
        }
 
289
    }
 
290
    else
 
291
    {
 
292
        if (open LINGUAS_FILE, "$PO_DIR/LINGUAS")
 
293
        {
 
294
            while (<LINGUAS_FILE>)
 
295
            {
 
296
                next if /^#/;
 
297
 
 
298
                for my $lang (split)
 
299
                {
 
300
                    chomp ($lang);
 
301
                    my $po_file = $PO_DIR . "/" . $lang . ".po";
 
302
                    if (-e $po_file) {
 
303
                        $po_files_by_lang{$lang} = $po_file;
 
304
                    }
 
305
                }
 
306
            }
 
307
 
 
308
            close LINGUAS_FILE;
 
309
        }
 
310
        else
 
311
        {
 
312
            for my $po_file (glob "$PO_DIR/*.po") {
 
313
                $po_files_by_lang{po_file2lang($po_file)} = $po_file;
 
314
            }
 
315
        }
254
316
    }
255
317
}
256
318
 
395
457
            } 
396
458
            else 
397
459
            {
398
 
                print STDERR "WARNING: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
 
460
                print "NOTICE: $po_file is not in UTF-8 but $encoding, converting...\n" unless $QUIET_ARG;;
399
461
 
400
462
                open PO_FILE, "$iconv -f $encoding -t UTF-8 $po_file|"; 
401
463
            }
415
477
        {
416
478
            $nextfuzzy = 1 if /^#, fuzzy/;
417
479
       
418
 
            if (/^msgid "((\\.|[^\\])*)"/ ) 
 
480
            if (/^msgid "((\\.|[^\\]+)*)"/ ) 
419
481
            {
420
482
                $translations{$lang, $msgid} = $msgstr if $inmsgstr && $msgid && $msgstr;
421
483
                $msgid = "";
431
493
                $nextfuzzy = 0;
432
494
            }
433
495
 
434
 
            if (/^msgstr "((\\.|[^\\])*)"/) 
 
496
            if (/^msgstr "((\\.|[^\\]+)*)"/) 
435
497
            {
436
498
                $msgstr = unescape_po_string($1);
437
499
                $inmsgstr = 1;
438
500
                $inmsgid = 0;
439
501
            }
440
502
 
441
 
            if (/^"((\\.|[^\\])*)"/) 
 
503
            if (/^"((\\.|[^\\]+)*)"/) 
442
504
            {
443
505
                $msgid .= unescape_po_string($1) if $inmsgid;
444
506
                $msgstr .= unescape_po_string($1) if $inmsgstr;
483
545
    return $string;
484
546
}
485
547
 
486
 
## NOTE: deal with < - &lt; but not > - &gt;  because it seems its ok to have 
487
 
## > in the entity. For further info please look at #84738.
488
548
sub entity_decode
489
549
{
490
550
    local ($_) = @_;
491
551
 
492
552
    s/&apos;/'/g; # '
493
553
    s/&quot;/"/g; # "
 
554
    s/&lt;/</g;
 
555
    s/&gt;/>/g;
494
556
    s/&amp;/&/g;
495
 
    s/&lt;/</g;
496
557
 
497
558
    return $_;
498
559
}
517
578
    return "&amp;" if $_ == 38;
518
579
    return "&apos;" if $_ == 39;
519
580
    return "&lt;" if $_ == 60;
 
581
    return "&gt;" if $_ == 62;
520
582
    return chr $_;
521
583
}
522
584
 
1006
1068
 
1007
1069
    if ($MULTIPLE_OUTPUT) {
1008
1070
        for my $lang (sort keys %po_files_by_lang) {
1009
 
            if ( ! -e $lang ) {
1010
 
                mkdir $lang or die "Cannot create subdirectory $lang: $!\n";
 
1071
            if ( ! -d $lang ) {
 
1072
                mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
1011
1073
            }
1012
1074
            open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1013
1075
            binmode (OUTPUT) if $^O eq 'MSWin32';
1017
1079
            close OUTPUT;
1018
1080
            print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1019
1081
        }
1020
 
    } 
1021
 
    open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
 
1082
        if ( ! -d "C" ) {
 
1083
            mkdir "C" or -d "C" or die "Cannot create subdirectory C: $!\n";
 
1084
        }
 
1085
        open OUTPUT, ">C/$OUTFILE" or die "Cannot open C/$OUTFILE: $!\n";
 
1086
        binmode (OUTPUT) if $^O eq 'MSWin32';
 
1087
        my $tree = readXml($FILE);
 
1088
        print_header($FILE, \*OUTPUT);
 
1089
        parseTree(\*OUTPUT, $tree);
 
1090
        close OUTPUT;
 
1091
        print "CREATED C/$OUTFILE\n" unless $QUIET_ARG;
 
1092
    } else {
 
1093
        open OUTPUT, ">$OUTFILE" or die "Cannot open $OUTFILE: $!\n";
 
1094
        binmode (OUTPUT) if $^O eq 'MSWin32';
 
1095
        my $tree = readXml($FILE);
 
1096
        print_header($FILE, \*OUTPUT);
 
1097
        parseTree(\*OUTPUT, $tree);
 
1098
        close OUTPUT;
 
1099
        print "CREATED $OUTFILE\n" unless $QUIET_ARG;
 
1100
    }
 
1101
}
 
1102
 
 
1103
sub keys_merge_translation
 
1104
{
 
1105
    my ($lang) = @_;
 
1106
 
 
1107
    if ( ! -d $lang && $MULTIPLE_OUTPUT)
 
1108
    {
 
1109
        mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
 
1110
    }
 
1111
 
 
1112
    open INPUT, "<${FILE}" or die "Cannot open ${FILE}: $!\n";
 
1113
    open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
1022
1114
    binmode (OUTPUT) if $^O eq 'MSWin32';
1023
 
    my $tree = readXml($FILE);
1024
 
    print_header($FILE, \*OUTPUT);
1025
 
    parseTree(\*OUTPUT, $tree);
 
1115
 
 
1116
    while (<INPUT>)
 
1117
    {
 
1118
        if (s/^(\s*)_(\w+=(.*))/$1$2/)
 
1119
        {
 
1120
            my $string = $3;
 
1121
 
 
1122
            if (!$MULTIPLE_OUTPUT)
 
1123
            {
 
1124
                print OUTPUT;
 
1125
 
 
1126
                my $non_translated_line = $_;
 
1127
 
 
1128
                for my $lang (sort keys %po_files_by_lang)
 
1129
                {
 
1130
                    my $translation = $translations{$lang, $string};
 
1131
                    next if !$translation;
 
1132
 
 
1133
                    $_ = $non_translated_line;
 
1134
                    s/(\w+)=.*/[$lang]$1=$translation/;
 
1135
                    print OUTPUT;
 
1136
                }
 
1137
            }
 
1138
            else
 
1139
            {
 
1140
                my $non_translated_line = $_;
 
1141
                my $translation = $translations{$lang, $string};
 
1142
                $translation = $string if !$translation;
 
1143
 
 
1144
                $_ = $non_translated_line;
 
1145
                s/(\w+)=.*/$1=$translation/;
 
1146
                print OUTPUT;
 
1147
            }
 
1148
        }
 
1149
        else
 
1150
        {
 
1151
            print OUTPUT;
 
1152
        }
 
1153
    }
 
1154
 
1026
1155
    close OUTPUT;
1027
 
    print "CREATED $OUTFILE\n" unless $QUIET_ARG;
 
1156
    close INPUT;
 
1157
 
 
1158
    print "CREATED $lang/$OUTFILE\n" unless $QUIET_ARG;
1028
1159
}
1029
1160
 
1030
1161
sub keys_merge_translations
1031
1162
{
1032
 
    open INPUT, "<${FILE}" or die;
1033
 
    open OUTPUT, ">${OUTFILE}" or die;
1034
 
    binmode (OUTPUT) if $^O eq 'MSWin32';
1035
 
 
1036
 
    while (<INPUT>) 
 
1163
    if ($MULTIPLE_OUTPUT)
1037
1164
    {
1038
 
        if (s/^(\s*)_(\w+=(.*))/$1$2/)  
1039
 
        {
1040
 
            my $string = $3;
1041
 
 
1042
 
            print OUTPUT;
1043
 
 
1044
 
            my $non_translated_line = $_;
1045
 
 
1046
 
            for my $lang (sort keys %po_files_by_lang) 
1047
 
            {
1048
 
                my $translation = $translations{$lang, $string};
1049
 
                next if !$translation;
1050
 
 
1051
 
                $_ = $non_translated_line;
1052
 
                s/(\w+)=.*/[$lang]$1=$translation/;
1053
 
                print OUTPUT;
1054
 
            }
1055
 
        } 
1056
 
        else 
1057
 
        {
1058
 
            print OUTPUT;
 
1165
        for my $lang (sort keys %po_files_by_lang)
 
1166
        {
 
1167
            keys_merge_translation ($lang);
1059
1168
        }
1060
 
    }
1061
 
 
1062
 
    close OUTPUT;
1063
 
    close INPUT;
 
1169
        keys_merge_translation ("C");
 
1170
    }
 
1171
    else
 
1172
    {
 
1173
        keys_merge_translation (".");
 
1174
    }
1064
1175
}
1065
1176
 
1066
1177
sub desktop_merge_translations
1354
1465
    return @list;
1355
1466
}
1356
1467
 
 
1468
sub quoted_translation
 
1469
{
 
1470
    my ($xml_mode, $lang, $string) = @_;
 
1471
 
 
1472
    $string = entity_decode($string) if $xml_mode;
 
1473
    $string =~ s/\\\"/\"/g;
 
1474
 
 
1475
    my $translation = $translations{$lang, $string};
 
1476
    $translation = $string if !$translation;
 
1477
    $translation = entity_encode($translation) if $xml_mode;
 
1478
    $translation =~ s/\"/\\\"/g;
 
1479
    return $translation
 
1480
}
 
1481
 
 
1482
sub quoted_merge_translations
 
1483
{
 
1484
    my ($xml_mode) = @_;
 
1485
 
 
1486
    if (!$MULTIPLE_OUTPUT) {
 
1487
        print "Quoted only supports Multiple Output.\n";
 
1488
        exit(1);
 
1489
    }
 
1490
 
 
1491
    for my $lang (sort keys %po_files_by_lang) {
 
1492
        if ( ! -d $lang ) {
 
1493
            mkdir $lang or -d $lang or die "Cannot create subdirectory $lang: $!\n";
 
1494
        }
 
1495
        open INPUT, "<${FILE}" or die;
 
1496
        open OUTPUT, ">$lang/$OUTFILE" or die "Cannot open $lang/$OUTFILE: $!\n";
 
1497
        binmode (OUTPUT) if $^O eq 'MSWin32';
 
1498
        while (<INPUT>) 
 
1499
        {
 
1500
            s/\"(([^\"]|\\\")*[^\\\"])\"/"\"" . &quoted_translation($xml_mode, $lang, $1) . "\""/ge;
 
1501
            print OUTPUT;
 
1502
        }
 
1503
        close OUTPUT;
 
1504
        close INPUT;
 
1505
    }
 
1506
}