~bratsche/ubuntu/maverick/monodevelop/disable-appmenu

« back to all changes in this revision

Viewing changes to intltool-update.in

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-03-29 23:36:33 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080329233633-wq5p1rktg8ek3vxc
Tags: 1.0+dfsg-1ubuntu1
* Merge from Debian unstable. (LP: #209012) Remaining Ubuntu changes:
+ debian/control: 
  - Build-dep on xulrunner-1.9-dev instead of firefox-dev
  - Depend on xulrunner-1.9 instead of firefox
+ don't do any MOZILLA_HOME/MOZILLA_FIVE_HOME business which isn't needed
  for xulrunner-1.9 using the standalone glue anymore (gecko gil uses
  standalone by default)
  - add debian/patches/use_xulrunner_1.9.dpatch
  - update debian/patches/00list
* Remove build-dep on libxul-dev and the dep on libxul0d
  since we are using xulrunner1.9 instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!@INTLTOOL_PERL@ -w
2
 
# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
3
 
 
4
 
#
5
 
#  The Intltool Message Updater
6
 
#
7
 
#  Copyright (C) 2000-2003 Free Software Foundation.
8
 
#
9
 
#  Intltool is free software; you can redistribute it and/or
10
 
#  modify it under the terms of the GNU General Public License 
11
 
#  version 2 published by the Free Software Foundation.
12
 
#
13
 
#  Intltool is distributed in the hope that it will be useful,
14
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
#  General Public License for more details.
17
 
#
18
 
#  You should have received a copy of the GNU General Public License
19
 
#  along with this program; if not, write to the Free Software
20
 
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
 
#
22
 
#  As a special exception to the GNU General Public License, if you
23
 
#  distribute this file as part of a program that contains a
24
 
#  configuration script generated by Autoconf, you may include it under
25
 
#  the same distribution terms that you use for the rest of that program.
26
 
#
27
 
#  Authors: Kenneth Christiansen <kenneth@gnu.org>
28
 
#           Maciej Stachowiak
29
 
#           Darin Adler <darin@bentspoon.com>
30
 
 
31
 
## Release information
32
 
my $PROGRAM = "intltool-update";
33
 
my $VERSION = "0.35.5";
34
 
my $PACKAGE = "intltool";
35
 
 
36
 
## Loaded modules
37
 
use strict;
38
 
use Getopt::Long;
39
 
use Cwd;
40
 
use File::Copy;
41
 
use File::Find;
42
 
 
43
 
## Scalars used by the option stuff
44
 
my $HELP_ARG       = 0;
45
 
my $VERSION_ARG    = 0;
46
 
my $DIST_ARG       = 0;
47
 
my $POT_ARG        = 0;
48
 
my $HEADERS_ARG    = 0;
49
 
my $MAINTAIN_ARG   = 0;
50
 
my $REPORT_ARG     = 0;
51
 
my $VERBOSE        = 0;
52
 
my $GETTEXT_PACKAGE = "";
53
 
my $OUTPUT_FILE    = "";
54
 
 
55
 
my @languages;
56
 
my %varhash = ();
57
 
my %po_files_by_lang = ();
58
 
 
59
 
# Regular expressions to categorize file types.
60
 
# FIXME: Please check if the following is correct
61
 
 
62
 
my $xml_support =
63
 
"xml(?:\\.in)*|".       # http://www.w3.org/XML/ (Note: .in is not required)
64
 
"ui|".                  # Bonobo specific - User Interface desc. files
65
 
"lang|".                # ?
66
 
"glade2?(?:\\.in)*|".   # Glade specific - User Interface desc. files (Note: .in is not required)
67
 
"scm(?:\\.in)*|".       # ? (Note: .in is not required)
68
 
"oaf(?:\\.in)+|".       # DEPRECATED: Replaces by Bonobo .server files 
69
 
"etspec|".              # ?
70
 
"server(?:\\.in)+|".    # Bonobo specific
71
 
"sheet(?:\\.in)+|".     # ?
72
 
"schemas(?:\\.in)+|".   # GConf specific
73
 
"pong(?:\\.in)+|".      # DEPRECATED: PONG is not used [by GNOME] any longer.
74
 
"kbd(?:\\.in)+";        # GOK specific. 
75
 
 
76
 
my $ini_support =
77
 
"icon(?:\\.in)+|".      # http://www.freedesktop.org/Standards/icon-theme-spec
78
 
"desktop(?:\\.in)+|".   # http://www.freedesktop.org/Standards/menu-spec
79
 
"caves(?:\\.in)+|".     # GNOME Games specific
80
 
"directory(?:\\.in)+|". # http://www.freedesktop.org/Standards/menu-spec
81
 
"soundlist(?:\\.in)+|". # GNOME specific
82
 
"keys(?:\\.in)+|".      # GNOME Mime database specific
83
 
"theme(?:\\.in)+|".     # http://www.freedesktop.org/Standards/icon-theme-spec
84
 
"service(?:\\.in)+";    # DBus specific
85
 
 
86
 
my $buildin_gettext_support = 
87
 
"c|y|cs|cc|cpp|c\\+\\+|h|hh|gob|py";
88
 
 
89
 
## Always flush buffer when printing
90
 
$| = 1;
91
 
 
92
 
## Sometimes the source tree will be rooted somewhere else.
93
 
my $SRCDIR = $ENV{"srcdir"} || ".";
94
 
my $POTFILES_in;
95
 
 
96
 
$POTFILES_in = "<$SRCDIR/POTFILES.in";
97
 
 
98
 
my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
99
 
 
100
 
## Handle options
101
 
GetOptions 
102
 
(
103
 
 "help"                => \$HELP_ARG,
104
 
 "version"             => \$VERSION_ARG,
105
 
 "dist|d"              => \$DIST_ARG,
106
 
 "pot|p"               => \$POT_ARG,
107
 
 "headers|s"           => \$HEADERS_ARG,
108
 
 "maintain|m"          => \$MAINTAIN_ARG,
109
 
 "report|r"            => \$REPORT_ARG,
110
 
 "verbose|x"           => \$VERBOSE,
111
 
 "gettext-package|g=s" => \$GETTEXT_PACKAGE,
112
 
 "output-file|o=s"     => \$OUTPUT_FILE,
113
 
 ) or &Console_WriteError_InvalidOption;
114
 
 
115
 
&Console_Write_IntltoolHelp if $HELP_ARG;
116
 
&Console_Write_IntltoolVersion if $VERSION_ARG;
117
 
 
118
 
my $arg_count = ($DIST_ARG > 0)
119
 
    + ($POT_ARG > 0)
120
 
    + ($HEADERS_ARG > 0)
121
 
    + ($MAINTAIN_ARG > 0)
122
 
    + ($REPORT_ARG > 0);
123
 
 
124
 
&Console_Write_IntltoolHelp if $arg_count > 1;
125
 
 
126
 
my $PKGNAME = FindPackageName ();
127
 
 
128
 
# --version and --help don't require a module name
129
 
my $MODULE = $GETTEXT_PACKAGE || $PKGNAME || "unknown";
130
 
 
131
 
if ($POT_ARG)
132
 
{
133
 
    &GenerateHeaders;
134
 
    &GeneratePOTemplate;
135
 
}
136
 
elsif ($HEADERS_ARG)
137
 
{
138
 
    &GenerateHeaders;
139
 
}
140
 
elsif ($MAINTAIN_ARG)
141
 
{
142
 
    &FindLeftoutFiles;
143
 
}
144
 
elsif ($REPORT_ARG)
145
 
{
146
 
    &GenerateHeaders;
147
 
    &GeneratePOTemplate;
148
 
    &Console_Write_CoverageReport;
149
 
}
150
 
elsif ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
151
 
{
152
 
    my $lang = $ARGV[0];
153
 
 
154
 
    ## Report error if the language file supplied
155
 
    ## to the command line is non-existent
156
 
    &Console_WriteError_NotExisting("$SRCDIR/$lang.po")
157
 
        if ! -s "$SRCDIR/$lang.po";
158
 
 
159
 
    if (!$DIST_ARG)
160
 
    {
161
 
        print "Working, please wait..." if $VERBOSE;
162
 
        &GenerateHeaders;
163
 
        &GeneratePOTemplate;
164
 
    }
165
 
    &POFile_Update ($lang, $OUTPUT_FILE);
166
 
    &Console_Write_TranslationStatus ($lang, $OUTPUT_FILE);
167
 
168
 
else 
169
 
{
170
 
    &Console_Write_IntltoolHelp;
171
 
}
172
 
 
173
 
exit;
174
 
 
175
 
#########
176
 
 
177
 
sub Console_Write_IntltoolVersion
178
 
{
179
 
    print <<_EOF_;
180
 
${PROGRAM} (${PACKAGE}) $VERSION
181
 
Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.
182
 
 
183
 
Copyright (C) 2000-2003 Free Software Foundation, Inc.
184
 
This is free software; see the source for copying conditions.  There is NO
185
 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
186
 
_EOF_
187
 
    exit;
188
 
}
189
 
 
190
 
sub Console_Write_IntltoolHelp
191
 
{
192
 
    print <<_EOF_;
193
 
Usage: ${PROGRAM} [OPTION]... LANGCODE
194
 
Updates PO template files and merge them with the translations.
195
 
 
196
 
Mode of operation (only one is allowed):
197
 
  -p, --pot                   generate the PO template only
198
 
  -s, --headers               generate the header files in POTFILES.in
199
 
  -m, --maintain              search for left out files from POTFILES.in
200
 
  -r, --report                display a status report for the module
201
 
  -d, --dist                  merge LANGCODE.po with existing PO template
202
 
 
203
 
Extra options:
204
 
  -g, --gettext-package=NAME  override PO template name, useful with --pot
205
 
  -o, --output-file=FILE      write merged translation to FILE
206
 
  -x, --verbose               display lots of feedback
207
 
      --help                  display this help and exit
208
 
      --version               output version information and exit
209
 
 
210
 
Examples of use:
211
 
${PROGRAM} --pot    just create a new PO template
212
 
${PROGRAM} xy       create new PO template and merge xy.po with it
213
 
 
214
 
Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
215
 
or send email to <xml-i18n-tools\@gnome.org>.
216
 
_EOF_
217
 
    exit;
218
 
}
219
 
 
220
 
sub echo_n
221
 
{
222
 
    my $str = shift;
223
 
    my $ret = `echo "$str"`;
224
 
 
225
 
    $ret =~ s/\n$//; # do we need the "s" flag?
226
 
 
227
 
    return $ret;
228
 
}
229
 
 
230
 
sub POFile_DetermineType ($) 
231
 
{
232
 
   my $type = $_;
233
 
   my $gettext_type;
234
 
 
235
 
   my $xml_regex     = "(?:" . $xml_support . ")";
236
 
   my $ini_regex     = "(?:" . $ini_support . ")";
237
 
   my $buildin_regex = "(?:" . $buildin_gettext_support . ")";
238
 
 
239
 
   if ($type =~ /\[type: gettext\/([^\]].*)]/) 
240
 
   {
241
 
        $gettext_type=$1;
242
 
   }
243
 
   elsif ($type =~ /schemas(\.in)+$/) 
244
 
   {
245
 
        $gettext_type="schemas";
246
 
   }
247
 
   elsif ($type =~ /glade2?(\.in)*$/) 
248
 
   {
249
 
       $gettext_type="glade";
250
 
   }
251
 
   elsif ($type =~ /scm(\.in)*$/) 
252
 
   {
253
 
       $gettext_type="scheme";
254
 
   }
255
 
   elsif ($type =~ /keys(\.in)+$/) 
256
 
   {
257
 
       $gettext_type="keys";
258
 
   }
259
 
 
260
 
   # bucket types
261
 
 
262
 
   elsif ($type =~ /$xml_regex$/) 
263
 
   {
264
 
       $gettext_type="xml";
265
 
   }
266
 
   elsif ($type =~ /$ini_regex$/) 
267
 
   { 
268
 
       $gettext_type="ini";
269
 
   }
270
 
   elsif ($type =~ /$buildin_regex$/) 
271
 
   {
272
 
       $gettext_type="buildin";
273
 
   }
274
 
   else
275
 
   { 
276
 
       $gettext_type="unknown"; 
277
 
   }
278
 
 
279
 
   return "gettext\/$gettext_type";
280
 
}
281
 
 
282
 
sub TextFile_DetermineEncoding ($) 
283
 
{
284
 
    my $gettext_code="ASCII"; # All files are ASCII by default
285
 
    my $filetype=`file $_ | cut -d ' ' -f 2`;
286
 
 
287
 
    if ($? eq "0")
288
 
    {
289
 
        if ($filetype =~ /^(ISO|UTF)/)
290
 
        {
291
 
            chomp ($gettext_code = $filetype);
292
 
        }
293
 
        elsif ($filetype =~ /^XML/)
294
 
        {
295
 
            $gettext_code="UTF-8"; # We asume that .glade and other .xml files are UTF-8
296
 
        }
297
 
    }
298
 
 
299
 
    return $gettext_code;
300
 
}
301
 
 
302
 
sub isNotValidMissing
303
 
{
304
 
    my ($file) = @_;
305
 
 
306
 
    return if $file =~ /^\{arch\}\/.*$/;
307
 
    return if $file =~ /^$varhash{"PACKAGE"}-$varhash{"VERSION"}\/.*$/;
308
 
}
309
 
 
310
 
sub FindLeftoutFiles
311
 
{
312
 
    my (@buf_i18n_plain,
313
 
        @buf_i18n_xml,
314
 
        @buf_i18n_xml_unmarked,
315
 
        @buf_i18n_ini,
316
 
        @buf_potfiles,
317
 
        @buf_potfiles_ignore,
318
 
        @buf_allfiles,
319
 
        @buf_allfiles_sorted,
320
 
        @buf_potfiles_sorted,
321
 
        @buf_potfiles_ignore_sorted
322
 
    );
323
 
 
324
 
    ## Search and find all translatable files
325
 
    find sub { 
326
 
        push @buf_i18n_plain,        "$File::Find::name" if /\.($buildin_gettext_support)$/;
327
 
        push @buf_i18n_xml,          "$File::Find::name" if /\.($xml_support)$/;
328
 
        push @buf_i18n_ini,          "$File::Find::name" if /\.($ini_support)$/;
329
 
        push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/;
330
 
        }, "..";
331
 
 
332
 
 
333
 
    open POTFILES, $POTFILES_in or die "$PROGRAM:  there's no POTFILES.in!\n";
334
 
    @buf_potfiles = grep !/^(#|\s*$)/, <POTFILES>;
335
 
    close POTFILES;
336
 
 
337
 
    foreach (@buf_potfiles) {
338
 
        s/^\[.*]\s*//;
339
 
    }
340
 
 
341
 
    print "Searching for missing translatable files...\n" if $VERBOSE;
342
 
 
343
 
    ## Check if we should ignore some found files, when
344
 
    ## comparing with POTFILES.in
345
 
    foreach my $ignore ("POTFILES.skip", "POTFILES.ignore")
346
 
    {
347
 
        (-s "$SRCDIR/$ignore") or next;
348
 
 
349
 
        if ("$ignore" eq "POTFILES.ignore")
350
 
        {
351
 
            print "The usage of POTFILES.ignore is deprecated. Please consider moving the\n".
352
 
                  "content of this file to POTFILES.skip.\n";
353
 
        }
354
 
 
355
 
        print "Found $ignore: Ignoring files...\n" if $VERBOSE;
356
 
        open FILE, "<$SRCDIR/$ignore" or die "ERROR: Failed to open $SRCDIR/$ignore!\n";
357
 
            
358
 
        while (<FILE>)
359
 
        {
360
 
            push @buf_potfiles_ignore, $_ unless /^(#|\s*$)/;
361
 
        }
362
 
        close FILE;
363
 
 
364
 
        @buf_potfiles_ignore_sorted = sort (@buf_potfiles_ignore);
365
 
    }
366
 
 
367
 
    foreach my $file (@buf_i18n_plain)
368
 
    {
369
 
        my $in_comment = 0;
370
 
        my $in_macro = 0;
371
 
 
372
 
        open FILE, "<$file";
373
 
        while (<FILE>)
374
 
        {
375
 
            # Handle continued multi-line comment.
376
 
            if ($in_comment)
377
 
            {
378
 
                next unless s-.*\*/--;
379
 
                $in_comment = 0;
380
 
            }
381
 
 
382
 
            # Handle continued macro.
383
 
            if ($in_macro)
384
 
            {
385
 
                $in_macro = 0 unless /\\$/;
386
 
                next;
387
 
            }
388
 
 
389
 
            # Handle start of macro (or any preprocessor directive).
390
 
            if (/^\s*\#/)
391
 
            {
392
 
                $in_macro = 1 if /^([^\\]|\\.)*\\$/;
393
 
                next;
394
 
            }
395
 
 
396
 
            # Handle comments and quoted text.
397
 
            while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
398
 
            {
399
 
                my $match = $1;
400
 
                if ($match eq "/*")
401
 
                {
402
 
                    if (!s-/\*.*?\*/--)
403
 
                    {
404
 
                        s-/\*.*--;
405
 
                        $in_comment = 1;
406
 
                    }
407
 
                }
408
 
                elsif ($match eq "//")
409
 
                {
410
 
                    s-//.*--;
411
 
                }
412
 
                else # ' or "
413
 
                {
414
 
                    if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
415
 
                    {
416
 
                        warn "mismatched quotes at line $. in $file\n";
417
 
                        s-$match.*--;
418
 
                    }
419
 
                }
420
 
            }       
421
 
 
422
 
            if (/\w\.GetString *\(QUOTEDTEXT/)
423
 
            {
424
 
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
425
 
                    ## Remove the first 3 chars and add newline
426
 
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
427
 
                }
428
 
                last;
429
 
            }
430
 
 
431
 
            ## N_ Q_ and _ are the three macros defined in gi8n.h
432
 
            if (/[NQ]?_ *\(QUOTEDTEXT/)
433
 
            {
434
 
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
435
 
                    ## Remove the first 3 chars and add newline
436
 
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
437
 
                }
438
 
                last;
439
 
            }
440
 
        }
441
 
        close FILE;
442
 
    }
443
 
 
444
 
    foreach my $file (@buf_i18n_xml) 
445
 
    {
446
 
        open FILE, "<$file";
447
 
        
448
 
        while (<FILE>) 
449
 
        {
450
 
            # FIXME: share the pattern matching code with intltool-extract
451
 
            if (/\s_[-A-Za-z0-9._:]+\s*=\s*\"([^"]+)\"/ || /<_[^>]+>/ || /translatable=\"yes\"/)
452
 
            {
453
 
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
454
 
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
455
 
                }
456
 
                last;
457
 
            }
458
 
        }
459
 
        close FILE;
460
 
    }
461
 
 
462
 
    foreach my $file (@buf_i18n_ini)
463
 
    {
464
 
        open FILE, "<$file";
465
 
        while (<FILE>) 
466
 
        {
467
 
            if (/_(.*)=/)
468
 
            {
469
 
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
470
 
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
471
 
                }
472
 
                last;
473
 
            }
474
 
        }
475
 
        close FILE;
476
 
    }
477
 
 
478
 
    foreach my $file (@buf_i18n_xml_unmarked)
479
 
    {
480
 
        if (defined isNotValidMissing (unpack("x3 A*", $file))) {
481
 
            push @buf_allfiles, unpack("x3 A*", $file) . "\n";
482
 
        }
483
 
    }
484
 
 
485
 
 
486
 
    @buf_allfiles_sorted = sort (@buf_allfiles);
487
 
    @buf_potfiles_sorted = sort (@buf_potfiles);
488
 
 
489
 
    my %in2;
490
 
    foreach (@buf_potfiles_sorted) 
491
 
    {
492
 
        $in2{$_} = 1;
493
 
    }
494
 
 
495
 
    foreach (@buf_potfiles_ignore_sorted) 
496
 
    {
497
 
        $in2{$_} = 1;
498
 
    }
499
 
 
500
 
    my @result;
501
 
 
502
 
    foreach (@buf_allfiles_sorted)
503
 
    {
504
 
        if (!exists($in2{$_}))
505
 
        {
506
 
            push @result, $_
507
 
        }
508
 
    }
509
 
 
510
 
    my @buf_potfiles_notexist;
511
 
 
512
 
    foreach (@buf_potfiles_sorted)
513
 
    {
514
 
        chomp (my $dummy = $_);
515
 
        if ("$dummy" ne "" and !(-f "$SRCDIR/../$dummy" or -f "../$dummy"))
516
 
        {
517
 
            push @buf_potfiles_notexist, $_;
518
 
        }
519
 
    }
520
 
 
521
 
    ## Save file with information about the files missing
522
 
    ## if any, and give information about this procedure.
523
 
    if (@result + @buf_potfiles_notexist > 0)
524
 
    {
525
 
        if (@result) 
526
 
        {
527
 
            print "\n" if $VERBOSE;
528
 
            unlink "missing";
529
 
            open OUT, ">missing";
530
 
            print OUT @result;
531
 
            close OUT;
532
 
            warn "\e[1mThe following files contain translations and are currently not in use. Please\e[0m\n".
533
 
                 "\e[1mconsider adding these to the POTFILES.in file, located in the po/ directory.\e[0m\n\n";
534
 
            print STDERR @result, "\n";
535
 
            warn "If some of these files are left out on purpose then please add them to\n".
536
 
                 "POTFILES.skip instead of POTFILES.in. A file \e[1m'missing'\e[0m containing this list\n".
537
 
                 "of left out files has been written in the current directory.\n";
538
 
        }
539
 
        if (@buf_potfiles_notexist)
540
 
        {
541
 
            unlink "notexist";
542
 
            open OUT, ">notexist";
543
 
            print OUT @buf_potfiles_notexist;
544
 
            close OUT;
545
 
            warn "\n" if ($VERBOSE or @result);
546
 
            warn "\e[1mThe following files do not exist anymore:\e[0m\n\n";
547
 
            warn @buf_potfiles_notexist, "\n";
548
 
            warn "Please remove them from POTFILES.in. A file \e[1m'notexist'\e[0m\n".
549
 
                 "containing this list of absent files has been written in the current directory.\n";
550
 
        }
551
 
    }
552
 
 
553
 
    ## If there is nothing to complain about, notify the user
554
 
    else {
555
 
        print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
556
 
    }
557
 
}
558
 
 
559
 
sub Console_WriteError_InvalidOption
560
 
{
561
 
    ## Handle invalid arguments
562
 
    print STDERR "Try `${PROGRAM} --help' for more information.\n";
563
 
    exit 1;
564
 
}
565
 
 
566
 
sub GenerateHeaders
567
 
{
568
 
    my $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} || "intltool-extract";
569
 
 
570
 
    ## Generate the .h header files, so we can allow glade and
571
 
    ## xml translation support
572
 
    if (0)
573
 
    {
574
 
        print STDERR "\n *** The intltool-extract script wasn't found!"
575
 
             ."\n *** Without it, intltool-update can not generate files.\n";
576
 
        exit;
577
 
    }
578
 
    else
579
 
    {
580
 
        open (FILE, $POTFILES_in) or die "$PROGRAM: POTFILES.in not found.\n";
581
 
        
582
 
        while (<FILE>) 
583
 
        {
584
 
           chomp;
585
 
           next if /^\[\s*encoding/;
586
 
 
587
 
           ## Find xml files in POTFILES.in and generate the
588
 
           ## files with help from the extract script
589
 
 
590
 
           my $gettext_type= &POFile_DetermineType ($1);
591
 
 
592
 
           if (/\.($xml_support|$ini_support)$/ || /^\[/)
593
 
           {
594
 
               s/^\[[^\[].*]\s*//;
595
 
 
596
 
               my $filename = "../$_";
597
 
 
598
 
               if ($VERBOSE)
599
 
               {
600
 
                   system ($EXTRACT, "--update", "--srcdir=$SRCDIR",
601
 
                           "--type=$gettext_type", $filename);
602
 
               } 
603
 
               else 
604
 
               {
605
 
                   system ($EXTRACT, "--update", "--type=$gettext_type", 
606
 
                           "--srcdir=$SRCDIR", "--quiet", $filename);
607
 
               }
608
 
           }
609
 
       }
610
 
       close FILE;
611
 
   }
612
 
}
613
 
 
614
 
#
615
 
# Generate .pot file from POTFILES.in
616
 
#
617
 
sub GeneratePOTemplate
618
 
{
619
 
    my $XGETTEXT = $ENV{"XGETTEXT"} || "@INTLTOOL_XGETTEXT@";
620
 
    my $XGETTEXT_ARGS = $ENV{"XGETTEXT_ARGS"} || '';
621
 
    chomp $XGETTEXT;
622
 
 
623
 
    if (! -x $XGETTEXT)
624
 
    {
625
 
        print STDERR " *** xgettext is not found on this system!\n".
626
 
                     " *** Without it, intltool-update can not extract strings.\n";
627
 
        exit;
628
 
    }
629
 
 
630
 
    print "Building $MODULE.pot...\n" if $VERBOSE;
631
 
 
632
 
    open INFILE, $POTFILES_in;
633
 
    unlink "POTFILES.in.temp";
634
 
    open OUTFILE, ">POTFILES.in.temp" or die("Cannot open POTFILES.in.temp for writing");
635
 
 
636
 
    my $gettext_support_nonascii = 0;
637
 
 
638
 
    # checks for GNU gettext >= 0.12
639
 
    my $dummy = `$XGETTEXT --version --from-code=UTF-8 >$devnull 2>$devnull`;
640
 
    if ($? == 0)
641
 
    {
642
 
        $gettext_support_nonascii = 1;
643
 
    }
644
 
    else
645
 
    {
646
 
        # urge everybody to upgrade gettext
647
 
        print STDERR "WARNING: This version of gettext does not support extracting non-ASCII\n".
648
 
                     "         strings. That means you should install a version of gettext\n".
649
 
                     "         that supports non-ASCII strings (such as GNU gettext >= 0.12),\n".
650
 
                     "         or have to let non-ASCII strings untranslated. (If there is any)\n";
651
 
    }
652
 
 
653
 
    my $encoding = "ASCII";
654
 
    my $forced_gettext_code;
655
 
    my @temp_headers;
656
 
    my $encoding_problem_is_reported = 0;
657
 
 
658
 
    while (<INFILE>) 
659
 
    {
660
 
        next if (/^#/ or /^\s*$/);
661
 
 
662
 
        chomp;
663
 
 
664
 
        my $gettext_code;
665
 
 
666
 
        if (/^\[\s*encoding:\s*(.*)\s*\]/)
667
 
        {
668
 
            $forced_gettext_code=$1;
669
 
        }
670
 
        elsif (/\.($xml_support|$ini_support)$/ || /^\[/)
671
 
        {
672
 
            s/^\[.*]\s*//;
673
 
            print OUTFILE "../$_.h\n";
674
 
            push @temp_headers, "../$_.h";
675
 
            $gettext_code = &TextFile_DetermineEncoding ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
676
 
        } 
677
 
        else 
678
 
        {
679
 
            print OUTFILE "$SRCDIR/../$_\n";
680
 
            $gettext_code = &TextFile_DetermineEncoding ("$SRCDIR/../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
681
 
        }
682
 
 
683
 
        next if (! $gettext_support_nonascii);
684
 
 
685
 
        if (defined $forced_gettext_code)
686
 
        {
687
 
            $encoding=$forced_gettext_code;
688
 
        }
689
 
        elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
690
 
        {
691
 
            if ($encoding eq "ASCII")
692
 
            {
693
 
                $encoding=$gettext_code;
694
 
            }
695
 
            elsif ($gettext_code ne "ASCII")
696
 
            {
697
 
                # Only report once because the message is quite long
698
 
                if (! $encoding_problem_is_reported)
699
 
                {
700
 
                    print STDERR "WARNING: You should use the same file encoding for all your project files,\n".
701
 
                                 "         but $PROGRAM thinks that most of the source files are in\n".
702
 
                                 "         $encoding encoding, while \"$_\" is (likely) in\n".
703
 
                                 "         $gettext_code encoding. If you are sure that all translatable strings\n".
704
 
                                 "         are in same encoding (say UTF-8), please \e[1m*prepend*\e[0m the following\n".
705
 
                                 "         line to POTFILES.in:\n\n".
706
 
                                 "                 [encoding: UTF-8]\n\n".
707
 
                                 "         and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .\n".
708
 
                                 "(such warning message will only be reported once.)\n";
709
 
                    $encoding_problem_is_reported = 1;
710
 
                }
711
 
            }
712
 
        }
713
 
    }
714
 
 
715
 
    close OUTFILE;
716
 
    close INFILE;
717
 
 
718
 
    unlink "$MODULE.pot";
719
 
    my @xgettext_argument=("$XGETTEXT",
720
 
                           "--add-comments",
721
 
                           "--directory\=\.",
722
 
                           "--output\=$MODULE\.pot",
723
 
                           "--files-from\=\.\/POTFILES\.in\.temp");
724
 
    my $XGETTEXT_KEYWORDS = &FindPOTKeywords;
725
 
    push @xgettext_argument, $XGETTEXT_KEYWORDS;
726
 
    my $MSGID_BUGS_ADDRESS = &FindMakevarsBugAddress;
727
 
    push @xgettext_argument, "--msgid-bugs-address\=$MSGID_BUGS_ADDRESS" if $MSGID_BUGS_ADDRESS;
728
 
    push @xgettext_argument, "--from-code\=$encoding" if ($gettext_support_nonascii);
729
 
    push @xgettext_argument, $XGETTEXT_ARGS if $XGETTEXT_ARGS;
730
 
    my $xgettext_command = join ' ', @xgettext_argument;
731
 
 
732
 
    # intercept xgettext error message
733
 
    print "Running $xgettext_command\n" if $VERBOSE;
734
 
    my $xgettext_error_msg = `$xgettext_command 2>\&1`;
735
 
    my $command_failed = $?;
736
 
 
737
 
    unlink "POTFILES.in.temp";
738
 
 
739
 
    print "Removing generated header (.h) files..." if $VERBOSE;
740
 
    unlink foreach (@temp_headers);
741
 
    print "done.\n" if $VERBOSE;
742
 
 
743
 
    if (! $command_failed)
744
 
    {
745
 
        if (! -e "$MODULE.pot")
746
 
        {
747
 
            print "None of the files in POTFILES.in contain strings marked for translation.\n" if $VERBOSE;
748
 
        }
749
 
        else
750
 
        {
751
 
            print "Wrote $MODULE.pot\n" if $VERBOSE;
752
 
        }
753
 
    }
754
 
    else
755
 
    {
756
 
        if ($xgettext_error_msg =~ /--from-code/)
757
 
        {
758
 
            # replace non-ASCII error message with a more useful one.
759
 
            print STDERR "ERROR: xgettext failed to generate PO template file because there is non-ASCII\n".
760
 
                         "       string marked for translation. Please make sure that all strings marked\n".
761
 
                         "       for translation are in uniform encoding (say UTF-8), then \e[1m*prepend*\e[0m the\n".
762
 
                         "       following line to POTFILES.in and rerun $PROGRAM:\n\n".
763
 
                         "           [encoding: UTF-8]\n\n";
764
 
        }
765
 
        else
766
 
        {
767
 
            print STDERR "$xgettext_error_msg";
768
 
            if (-e "$MODULE.pot")
769
 
            {
770
 
                # is this possible?
771
 
                print STDERR "ERROR: xgettext failed but still managed to generate PO template file.\n".
772
 
                             "       Please consult error message above if there is any.\n";
773
 
            }
774
 
            else
775
 
            {
776
 
                print STDERR "ERROR: xgettext failed to generate PO template file. Please consult\n".
777
 
                             "       error message above if there is any.\n";
778
 
            }
779
 
        }
780
 
        exit (1);
781
 
    }
782
 
}
783
 
 
784
 
sub POFile_Update
785
 
{
786
 
    -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
787
 
 
788
 
    my $MSGMERGE = $ENV{"MSGMERGE"} || "@INTLTOOL_MSGMERGE@";
789
 
    my ($lang, $outfile) = @_;
790
 
 
791
 
    print "Merging $SRCDIR/$lang.po with $MODULE.pot..." if $VERBOSE;
792
 
 
793
 
    my $infile = "$SRCDIR/$lang.po";
794
 
    $outfile = "$SRCDIR/$lang.po" if ($outfile eq "");
795
 
 
796
 
    # I think msgmerge won't overwrite old file if merge is not successful
797
 
    system ("$MSGMERGE", "-o", $outfile, $infile, "$MODULE.pot");
798
 
}
799
 
 
800
 
sub Console_WriteError_NotExisting
801
 
{
802
 
    my ($file) = @_;
803
 
 
804
 
    ## Report error if supplied language file is non-existing
805
 
    print STDERR "$PROGRAM: $file does not exist!\n";
806
 
    print STDERR "Try '$PROGRAM --help' for more information.\n";
807
 
    exit;
808
 
}
809
 
 
810
 
sub GatherPOFiles
811
 
{
812
 
    my @po_files = glob ("./*.po");
813
 
 
814
 
    @languages = map (&POFile_GetLanguage, @po_files);
815
 
 
816
 
    foreach my $lang (@languages) 
817
 
    {
818
 
        $po_files_by_lang{$lang} = shift (@po_files);
819
 
    }
820
 
}
821
 
 
822
 
sub POFile_GetLanguage ($)
823
 
{
824
 
    s/^(.*\/)?(.+)\.po$/$2/;
825
 
    return $_;
826
 
}
827
 
 
828
 
sub Console_Write_TranslationStatus
829
 
{
830
 
    my ($lang, $output_file) = @_;
831
 
    my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
832
 
 
833
 
    $output_file = "$SRCDIR/$lang.po" if ($output_file eq "");
834
 
 
835
 
    system ("$MSGFMT", "-o", "$devnull", "--verbose", $output_file);
836
 
}
837
 
 
838
 
sub Console_Write_CoverageReport
839
 
{
840
 
    my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
841
 
 
842
 
    &GatherPOFiles;
843
 
 
844
 
    foreach my $lang (@languages) 
845
 
    {
846
 
        print "$lang: ";
847
 
        &POFile_Update ($lang, "");
848
 
    }
849
 
 
850
 
    print "\n\n * Current translation support in $MODULE \n\n";
851
 
 
852
 
    foreach my $lang (@languages)
853
 
    {
854
 
        print "$lang: ";
855
 
        system ("$MSGFMT", "-o", "$devnull", "--verbose", "$SRCDIR/$lang.po");
856
 
    }
857
 
}
858
 
 
859
 
sub SubstituteVariable
860
 
{
861
 
    my ($str) = @_;
862
 
    
863
 
    # always need to rewind file whenever it has been accessed
864
 
    seek (CONF, 0, 0);
865
 
 
866
 
    # cache each variable. varhash is global to we can add
867
 
    # variables elsewhere.
868
 
    while (<CONF>)
869
 
    {
870
 
        if (/^(\w+)=(.*)$/)
871
 
        {
872
 
            ($varhash{$1} = $2) =~  s/^["'](.*)["']$/$1/;
873
 
        }
874
 
    }
875
 
    
876
 
    if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
877
 
    {
878
 
        my $rest = $3;
879
 
        my $untouched = $1;
880
 
        my $sub = "";
881
 
        # Ignore recursive definitions of variables
882
 
        $sub = $varhash{$2} if defined $varhash{$2} and $varhash{$2} !~ /\${?$2}?/;
883
 
 
884
 
        return SubstituteVariable ("$untouched$sub$rest");
885
 
    }
886
 
    
887
 
    # We're using Perl backticks ` and "echo -n" here in order to 
888
 
    # expand any shell escapes (such as backticks themselves) in every variable
889
 
    return echo_n ($str);
890
 
}
891
 
 
892
 
sub CONF_Handle_Open
893
 
{
894
 
    my $base_dirname = getcwd();
895
 
    $base_dirname =~ s@.*/@@;
896
 
 
897
 
    my ($conf_in, $src_dir);
898
 
 
899
 
    if ($base_dirname =~ /^po(-.+)?$/) 
900
 
    {
901
 
        if (-f "Makevars") 
902
 
        {
903
 
            my $makefile_source;
904
 
 
905
 
            local (*IN);
906
 
            open (IN, "<Makevars") || die "can't open Makevars: $!";
907
 
 
908
 
            while (<IN>) 
909
 
            {
910
 
                if (/^top_builddir[ \t]*=/) 
911
 
                {
912
 
                    $src_dir = $_;
913
 
                    $src_dir =~ s/^top_builddir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
914
 
 
915
 
                    chomp $src_dir;
916
 
                    if (-f "$src_dir" . "/configure.ac") {
917
 
                        $conf_in = "$src_dir" . "/configure.ac" . "\n";
918
 
                    } else {
919
 
                        $conf_in = "$src_dir" . "/configure.in" . "\n";
920
 
                    }
921
 
                    last;
922
 
                }
923
 
            }
924
 
            close IN;
925
 
 
926
 
            $conf_in || die "Cannot find top_builddir in Makevars.";
927
 
        }
928
 
        elsif (-f "../configure.ac") 
929
 
        {
930
 
            $conf_in = "../configure.ac";
931
 
        } 
932
 
        elsif (-f "../configure.in") 
933
 
        {
934
 
            $conf_in = "../configure.in";
935
 
        } 
936
 
        else 
937
 
        {
938
 
            my $makefile_source;
939
 
 
940
 
            local (*IN);
941
 
            open (IN, "<Makefile") || return;
942
 
 
943
 
            while (<IN>) 
944
 
            {
945
 
                if (/^top_srcdir[ \t]*=/) 
946
 
                {
947
 
                    $src_dir = $_;                  
948
 
                    $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
949
 
 
950
 
                    chomp $src_dir;
951
 
                    $conf_in = "$src_dir" . "/configure.in" . "\n";
952
 
 
953
 
                    last;
954
 
                }
955
 
            }
956
 
            close IN;
957
 
 
958
 
            $conf_in || die "Cannot find top_srcdir in Makefile.";
959
 
        }
960
 
 
961
 
        open (CONF, "<$conf_in");
962
 
    }
963
 
    else
964
 
    {
965
 
        print STDERR "$PROGRAM: Unable to proceed.\n" .
966
 
                     "Make sure to run this script inside the po directory.\n";
967
 
        exit;
968
 
    }
969
 
}
970
 
 
971
 
sub FindPackageName
972
 
{
973
 
    my $version;
974
 
    my $domain = &FindMakevarsDomain;
975
 
    my $name = $domain || "untitled";
976
 
 
977
 
    &CONF_Handle_Open;
978
 
 
979
 
    my $conf_source; {
980
 
        local (*IN);
981
 
        open (IN, "<&CONF") || return $name;
982
 
        seek (IN, 0, 0);
983
 
        local $/; # slurp mode
984
 
        $conf_source = <IN>;
985
 
        close IN;
986
 
    }
987
 
 
988
 
    # priority for getting package name:
989
 
    # 1. GETTEXT_PACKAGE
990
 
    # 2. first argument of AC_INIT (with >= 2 arguments)
991
 
    # 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)
992
 
 
993
 
    # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m 
994
 
    # the \s makes this not work, why?
995
 
    if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
996
 
    {
997
 
        ($name, $version) = ($1, $2);
998
 
        $name    =~ s/[\[\]\s]//g;
999
 
        $version =~ s/[\[\]\s]//g;
1000
 
        $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
1001
 
        $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
1002
 
        $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
1003
 
        $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
1004
 
    }
1005
 
    
1006
 
    if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)/m) 
1007
 
    {
1008
 
        ($name, $version) = ($1, $2);
1009
 
        $name    =~ s/[\[\]\s]//g;
1010
 
        $version =~ s/[\[\]\s]//g;
1011
 
        $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
1012
 
        $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
1013
 
        $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
1014
 
        $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
1015
 
    }
1016
 
 
1017
 
    # \s makes this not work, why?
1018
 
    $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
1019
 
    
1020
 
    # m4 macros AC_PACKAGE_NAME, AC_PACKAGE_VERSION etc. have same value
1021
 
    # as corresponding $PACKAGE_NAME, $PACKAGE_VERSION etc. shell variables.
1022
 
    $name =~ s/\bAC_PACKAGE_/\$PACKAGE_/g;
1023
 
 
1024
 
    $name = $domain if $domain;
1025
 
 
1026
 
    $name = SubstituteVariable ($name);
1027
 
    $name =~ s/^["'](.*)["']$/$1/;
1028
 
 
1029
 
    return $name if $name;
1030
 
}
1031
 
 
1032
 
 
1033
 
sub FindPOTKeywords
1034
 
{
1035
 
 
1036
 
    my $keywords = "--keyword\=\_ --keyword\=N\_ --keyword\=U\_ --keyword\=Q\_";
1037
 
    my $varname = "XGETTEXT_OPTIONS";
1038
 
    my $make_source; {
1039
 
        local (*IN);
1040
 
        open (IN, "<Makevars") || (open(IN, "<Makefile.in.in") && ($varname = "XGETTEXT_KEYWORDS")) || return $keywords;
1041
 
        seek (IN, 0, 0);
1042
 
        local $/; # slurp mode
1043
 
        $make_source = <IN>;
1044
 
        close IN;
1045
 
    }
1046
 
 
1047
 
    $keywords = $1 if $make_source =~ /^$varname[ ]*=\[?([^\n\]]+)/m;
1048
 
    
1049
 
    return $keywords;
1050
 
}
1051
 
 
1052
 
sub FindMakevarsDomain
1053
 
{
1054
 
 
1055
 
    my $domain = "";
1056
 
    my $makevars_source; { 
1057
 
        local (*IN);
1058
 
        open (IN, "<Makevars") || return $domain;
1059
 
        seek (IN, 0, 0);
1060
 
        local $/; # slurp mode
1061
 
        $makevars_source = <IN>;
1062
 
        close IN;
1063
 
    }
1064
 
 
1065
 
    $domain = $1 if $makevars_source =~ /^DOMAIN[ ]*=\[?([^\n\]\$]+)/m;
1066
 
    $domain =~ s/^\s+//;
1067
 
    $domain =~ s/\s+$//;
1068
 
    
1069
 
    return $domain;
1070
 
}
1071
 
 
1072
 
sub FindMakevarsBugAddress
1073
 
{
1074
 
 
1075
 
    my $address = "";
1076
 
    my $makevars_source; { 
1077
 
        local (*IN);
1078
 
        open (IN, "<Makevars") || return undef;
1079
 
        seek (IN, 0, 0);
1080
 
        local $/; # slurp mode
1081
 
        $makevars_source = <IN>;
1082
 
        close IN;
1083
 
    }
1084
 
 
1085
 
    $address = $1 if $makevars_source =~ /^MSGID_BUGS_ADDRESS[ ]*=\[?([^\n\]\$]+)/m;
1086
 
    $address =~ s/^\s+//;
1087
 
    $address =~ s/\s+$//;
1088
 
    
1089
 
    return $address;
1090
 
}