~bcurtiswx/ubuntu/precise/empathy/3.4.2.1-0ubuntu1

« back to all changes in this revision

Viewing changes to intltool-update.in

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons
  • Date: 2007-05-20 15:31:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070520153142-r3auwguxdgxhktqb
Tags: upstream-0.4
ImportĀ upstreamĀ versionĀ 0.4

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 isIntltoolExtractInPath
 
567
{
 
568
    my ($file) = @_;
 
569
    # If either a file exists, or when run it returns 0 exit status
 
570
    return 1 if ((-x $file) or (system("$file >/dev/null") == 0));
 
571
    return 0;
 
572
}
 
573
 
 
574
sub GenerateHeaders
 
575
{
 
576
    my $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} || "intltool-extract";
 
577
 
 
578
    ## Generate the .h header files, so we can allow glade and
 
579
    ## xml translation support
 
580
    if (! isIntltoolExtractInPath("$EXTRACT"))
 
581
    {
 
582
        print STDERR "\n *** The intltool-extract script wasn't found!"
 
583
             ."\n *** Without it, intltool-update can not generate files.\n";
 
584
        exit;
 
585
    }
 
586
    else
 
587
    {
 
588
        open (FILE, $POTFILES_in) or die "$PROGRAM: POTFILES.in not found.\n";
 
589
        
 
590
        while (<FILE>) 
 
591
        {
 
592
           chomp;
 
593
           next if /^\[\s*encoding/;
 
594
 
 
595
           ## Find xml files in POTFILES.in and generate the
 
596
           ## files with help from the extract script
 
597
 
 
598
           my $gettext_type= &POFile_DetermineType ($1);
 
599
 
 
600
           if (/\.($xml_support|$ini_support)$/ || /^\[/)
 
601
           {
 
602
               s/^\[[^\[].*]\s*//;
 
603
 
 
604
               my $filename = "../$_";
 
605
 
 
606
               if ($VERBOSE)
 
607
               {
 
608
                   system ($EXTRACT, "--update", "--srcdir=$SRCDIR",
 
609
                           "--type=$gettext_type", $filename);
 
610
               } 
 
611
               else 
 
612
               {
 
613
                   system ($EXTRACT, "--update", "--type=$gettext_type", 
 
614
                           "--srcdir=$SRCDIR", "--quiet", $filename);
 
615
               }
 
616
           }
 
617
       }
 
618
       close FILE;
 
619
   }
 
620
}
 
621
 
 
622
#
 
623
# Generate .pot file from POTFILES.in
 
624
#
 
625
sub GeneratePOTemplate
 
626
{
 
627
    my $XGETTEXT = $ENV{"XGETTEXT"} || "@INTLTOOL_XGETTEXT@";
 
628
    my $XGETTEXT_ARGS = $ENV{"XGETTEXT_ARGS"} || '';
 
629
    chomp $XGETTEXT;
 
630
 
 
631
    if (! -x $XGETTEXT)
 
632
    {
 
633
        print STDERR " *** xgettext is not found on this system!\n".
 
634
                     " *** Without it, intltool-update can not extract strings.\n";
 
635
        exit;
 
636
    }
 
637
 
 
638
    print "Building $MODULE.pot...\n" if $VERBOSE;
 
639
 
 
640
    open INFILE, $POTFILES_in;
 
641
    unlink "POTFILES.in.temp";
 
642
    open OUTFILE, ">POTFILES.in.temp" or die("Cannot open POTFILES.in.temp for writing");
 
643
 
 
644
    my $gettext_support_nonascii = 0;
 
645
 
 
646
    # checks for GNU gettext >= 0.12
 
647
    my $dummy = `$XGETTEXT --version --from-code=UTF-8 >$devnull 2>$devnull`;
 
648
    if ($? == 0)
 
649
    {
 
650
        $gettext_support_nonascii = 1;
 
651
    }
 
652
    else
 
653
    {
 
654
        # urge everybody to upgrade gettext
 
655
        print STDERR "WARNING: This version of gettext does not support extracting non-ASCII\n".
 
656
                     "         strings. That means you should install a version of gettext\n".
 
657
                     "         that supports non-ASCII strings (such as GNU gettext >= 0.12),\n".
 
658
                     "         or have to let non-ASCII strings untranslated. (If there is any)\n";
 
659
    }
 
660
 
 
661
    my $encoding = "ASCII";
 
662
    my $forced_gettext_code;
 
663
    my @temp_headers;
 
664
    my $encoding_problem_is_reported = 0;
 
665
 
 
666
    while (<INFILE>) 
 
667
    {
 
668
        next if (/^#/ or /^\s*$/);
 
669
 
 
670
        chomp;
 
671
 
 
672
        my $gettext_code;
 
673
 
 
674
        if (/^\[\s*encoding:\s*(.*)\s*\]/)
 
675
        {
 
676
            $forced_gettext_code=$1;
 
677
        }
 
678
        elsif (/\.($xml_support|$ini_support)$/ || /^\[/)
 
679
        {
 
680
            s/^\[.*]\s*//;
 
681
            print OUTFILE "../$_.h\n";
 
682
            push @temp_headers, "../$_.h";
 
683
            $gettext_code = &TextFile_DetermineEncoding ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
 
684
        } 
 
685
        else 
 
686
        {
 
687
            print OUTFILE "$SRCDIR/../$_\n";
 
688
            $gettext_code = &TextFile_DetermineEncoding ("$SRCDIR/../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
 
689
        }
 
690
 
 
691
        next if (! $gettext_support_nonascii);
 
692
 
 
693
        if (defined $forced_gettext_code)
 
694
        {
 
695
            $encoding=$forced_gettext_code;
 
696
        }
 
697
        elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
 
698
        {
 
699
            if ($encoding eq "ASCII")
 
700
            {
 
701
                $encoding=$gettext_code;
 
702
            }
 
703
            elsif ($gettext_code ne "ASCII")
 
704
            {
 
705
                # Only report once because the message is quite long
 
706
                if (! $encoding_problem_is_reported)
 
707
                {
 
708
                    print STDERR "WARNING: You should use the same file encoding for all your project files,\n".
 
709
                                 "         but $PROGRAM thinks that most of the source files are in\n".
 
710
                                 "         $encoding encoding, while \"$_\" is (likely) in\n".
 
711
                                 "         $gettext_code encoding. If you are sure that all translatable strings\n".
 
712
                                 "         are in same encoding (say UTF-8), please \e[1m*prepend*\e[0m the following\n".
 
713
                                 "         line to POTFILES.in:\n\n".
 
714
                                 "                 [encoding: UTF-8]\n\n".
 
715
                                 "         and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .\n".
 
716
                                 "(such warning message will only be reported once.)\n";
 
717
                    $encoding_problem_is_reported = 1;
 
718
                }
 
719
            }
 
720
        }
 
721
    }
 
722
 
 
723
    close OUTFILE;
 
724
    close INFILE;
 
725
 
 
726
    unlink "$MODULE.pot";
 
727
    my @xgettext_argument=("$XGETTEXT",
 
728
                           "--add-comments",
 
729
                           "--directory\=\.",
 
730
                           "--output\=$MODULE\.pot",
 
731
                           "--files-from\=\.\/POTFILES\.in\.temp");
 
732
    my $XGETTEXT_KEYWORDS = &FindPOTKeywords;
 
733
    push @xgettext_argument, $XGETTEXT_KEYWORDS;
 
734
    my $MSGID_BUGS_ADDRESS = &FindMakevarsBugAddress;
 
735
    push @xgettext_argument, "--msgid-bugs-address\=$MSGID_BUGS_ADDRESS" if $MSGID_BUGS_ADDRESS;
 
736
    push @xgettext_argument, "--from-code\=$encoding" if ($gettext_support_nonascii);
 
737
    push @xgettext_argument, $XGETTEXT_ARGS if $XGETTEXT_ARGS;
 
738
    my $xgettext_command = join ' ', @xgettext_argument;
 
739
 
 
740
    # intercept xgettext error message
 
741
    print "Running $xgettext_command\n" if $VERBOSE;
 
742
    my $xgettext_error_msg = `$xgettext_command 2>\&1`;
 
743
    my $command_failed = $?;
 
744
 
 
745
    unlink "POTFILES.in.temp";
 
746
 
 
747
    print "Removing generated header (.h) files..." if $VERBOSE;
 
748
    unlink foreach (@temp_headers);
 
749
    print "done.\n" if $VERBOSE;
 
750
 
 
751
    if (! $command_failed)
 
752
    {
 
753
        if (! -e "$MODULE.pot")
 
754
        {
 
755
            print "None of the files in POTFILES.in contain strings marked for translation.\n" if $VERBOSE;
 
756
        }
 
757
        else
 
758
        {
 
759
            print "Wrote $MODULE.pot\n" if $VERBOSE;
 
760
        }
 
761
    }
 
762
    else
 
763
    {
 
764
        if ($xgettext_error_msg =~ /--from-code/)
 
765
        {
 
766
            # replace non-ASCII error message with a more useful one.
 
767
            print STDERR "ERROR: xgettext failed to generate PO template file because there is non-ASCII\n".
 
768
                         "       string marked for translation. Please make sure that all strings marked\n".
 
769
                         "       for translation are in uniform encoding (say UTF-8), then \e[1m*prepend*\e[0m the\n".
 
770
                         "       following line to POTFILES.in and rerun $PROGRAM:\n\n".
 
771
                         "           [encoding: UTF-8]\n\n";
 
772
        }
 
773
        else
 
774
        {
 
775
            print STDERR "$xgettext_error_msg";
 
776
            if (-e "$MODULE.pot")
 
777
            {
 
778
                # is this possible?
 
779
                print STDERR "ERROR: xgettext failed but still managed to generate PO template file.\n".
 
780
                             "       Please consult error message above if there is any.\n";
 
781
            }
 
782
            else
 
783
            {
 
784
                print STDERR "ERROR: xgettext failed to generate PO template file. Please consult\n".
 
785
                             "       error message above if there is any.\n";
 
786
            }
 
787
        }
 
788
        exit (1);
 
789
    }
 
790
}
 
791
 
 
792
sub POFile_Update
 
793
{
 
794
    -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
 
795
 
 
796
    my $MSGMERGE = $ENV{"MSGMERGE"} || "@INTLTOOL_MSGMERGE@";
 
797
    my ($lang, $outfile) = @_;
 
798
 
 
799
    print "Merging $SRCDIR/$lang.po with $MODULE.pot..." if $VERBOSE;
 
800
 
 
801
    my $infile = "$SRCDIR/$lang.po";
 
802
    $outfile = "$SRCDIR/$lang.po" if ($outfile eq "");
 
803
 
 
804
    # I think msgmerge won't overwrite old file if merge is not successful
 
805
    system ("$MSGMERGE", "-o", $outfile, $infile, "$MODULE.pot");
 
806
}
 
807
 
 
808
sub Console_WriteError_NotExisting
 
809
{
 
810
    my ($file) = @_;
 
811
 
 
812
    ## Report error if supplied language file is non-existing
 
813
    print STDERR "$PROGRAM: $file does not exist!\n";
 
814
    print STDERR "Try '$PROGRAM --help' for more information.\n";
 
815
    exit;
 
816
}
 
817
 
 
818
sub GatherPOFiles
 
819
{
 
820
    my @po_files = glob ("./*.po");
 
821
 
 
822
    @languages = map (&POFile_GetLanguage, @po_files);
 
823
 
 
824
    foreach my $lang (@languages) 
 
825
    {
 
826
        $po_files_by_lang{$lang} = shift (@po_files);
 
827
    }
 
828
}
 
829
 
 
830
sub POFile_GetLanguage ($)
 
831
{
 
832
    s/^(.*\/)?(.+)\.po$/$2/;
 
833
    return $_;
 
834
}
 
835
 
 
836
sub Console_Write_TranslationStatus
 
837
{
 
838
    my ($lang, $output_file) = @_;
 
839
    my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
 
840
 
 
841
    $output_file = "$SRCDIR/$lang.po" if ($output_file eq "");
 
842
 
 
843
    system ("$MSGFMT", "-o", "$devnull", "--verbose", $output_file);
 
844
}
 
845
 
 
846
sub Console_Write_CoverageReport
 
847
{
 
848
    my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
 
849
 
 
850
    &GatherPOFiles;
 
851
 
 
852
    foreach my $lang (@languages) 
 
853
    {
 
854
        print "$lang: ";
 
855
        &POFile_Update ($lang, "");
 
856
    }
 
857
 
 
858
    print "\n\n * Current translation support in $MODULE \n\n";
 
859
 
 
860
    foreach my $lang (@languages)
 
861
    {
 
862
        print "$lang: ";
 
863
        system ("$MSGFMT", "-o", "$devnull", "--verbose", "$SRCDIR/$lang.po");
 
864
    }
 
865
}
 
866
 
 
867
sub SubstituteVariable
 
868
{
 
869
    my ($str) = @_;
 
870
    
 
871
    # always need to rewind file whenever it has been accessed
 
872
    seek (CONF, 0, 0);
 
873
 
 
874
    # cache each variable. varhash is global to we can add
 
875
    # variables elsewhere.
 
876
    while (<CONF>)
 
877
    {
 
878
        if (/^(\w+)=(.*)$/)
 
879
        {
 
880
            ($varhash{$1} = $2) =~  s/^["'](.*)["']$/$1/;
 
881
        }
 
882
    }
 
883
    
 
884
    if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
 
885
    {
 
886
        my $rest = $3;
 
887
        my $untouched = $1;
 
888
        my $sub = "";
 
889
        # Ignore recursive definitions of variables
 
890
        $sub = $varhash{$2} if defined $varhash{$2} and $varhash{$2} !~ /\${?$2}?/;
 
891
 
 
892
        return SubstituteVariable ("$untouched$sub$rest");
 
893
    }
 
894
    
 
895
    # We're using Perl backticks ` and "echo -n" here in order to 
 
896
    # expand any shell escapes (such as backticks themselves) in every variable
 
897
    return echo_n ($str);
 
898
}
 
899
 
 
900
sub CONF_Handle_Open
 
901
{
 
902
    my $base_dirname = getcwd();
 
903
    $base_dirname =~ s@.*/@@;
 
904
 
 
905
    my ($conf_in, $src_dir);
 
906
 
 
907
    if ($base_dirname =~ /^po(-.+)?$/) 
 
908
    {
 
909
        if (-f "Makevars") 
 
910
        {
 
911
            my $makefile_source;
 
912
 
 
913
            local (*IN);
 
914
            open (IN, "<Makevars") || die "can't open Makevars: $!";
 
915
 
 
916
            while (<IN>) 
 
917
            {
 
918
                if (/^top_builddir[ \t]*=/) 
 
919
                {
 
920
                    $src_dir = $_;
 
921
                    $src_dir =~ s/^top_builddir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
 
922
 
 
923
                    chomp $src_dir;
 
924
                    if (-f "$src_dir" . "/configure.ac") {
 
925
                        $conf_in = "$src_dir" . "/configure.ac" . "\n";
 
926
                    } else {
 
927
                        $conf_in = "$src_dir" . "/configure.in" . "\n";
 
928
                    }
 
929
                    last;
 
930
                }
 
931
            }
 
932
            close IN;
 
933
 
 
934
            $conf_in || die "Cannot find top_builddir in Makevars.";
 
935
        }
 
936
        elsif (-f "../configure.ac") 
 
937
        {
 
938
            $conf_in = "../configure.ac";
 
939
        } 
 
940
        elsif (-f "../configure.in") 
 
941
        {
 
942
            $conf_in = "../configure.in";
 
943
        } 
 
944
        else 
 
945
        {
 
946
            my $makefile_source;
 
947
 
 
948
            local (*IN);
 
949
            open (IN, "<Makefile") || return;
 
950
 
 
951
            while (<IN>) 
 
952
            {
 
953
                if (/^top_srcdir[ \t]*=/) 
 
954
                {
 
955
                    $src_dir = $_;                  
 
956
                    $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
 
957
 
 
958
                    chomp $src_dir;
 
959
                    $conf_in = "$src_dir" . "/configure.in" . "\n";
 
960
 
 
961
                    last;
 
962
                }
 
963
            }
 
964
            close IN;
 
965
 
 
966
            $conf_in || die "Cannot find top_srcdir in Makefile.";
 
967
        }
 
968
 
 
969
        open (CONF, "<$conf_in");
 
970
    }
 
971
    else
 
972
    {
 
973
        print STDERR "$PROGRAM: Unable to proceed.\n" .
 
974
                     "Make sure to run this script inside the po directory.\n";
 
975
        exit;
 
976
    }
 
977
}
 
978
 
 
979
sub FindPackageName
 
980
{
 
981
    my $version;
 
982
    my $domain = &FindMakevarsDomain;
 
983
    my $name = $domain || "untitled";
 
984
 
 
985
    &CONF_Handle_Open;
 
986
 
 
987
    my $conf_source; {
 
988
        local (*IN);
 
989
        open (IN, "<&CONF") || return $name;
 
990
        seek (IN, 0, 0);
 
991
        local $/; # slurp mode
 
992
        $conf_source = <IN>;
 
993
        close IN;
 
994
    }
 
995
 
 
996
    # priority for getting package name:
 
997
    # 1. GETTEXT_PACKAGE
 
998
    # 2. first argument of AC_INIT (with >= 2 arguments)
 
999
    # 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)
 
1000
 
 
1001
    # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m 
 
1002
    # the \s makes this not work, why?
 
1003
    if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
 
1004
    {
 
1005
        ($name, $version) = ($1, $2);
 
1006
        $name    =~ s/[\[\]\s]//g;
 
1007
        $version =~ s/[\[\]\s]//g;
 
1008
        $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
 
1009
        $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
 
1010
        $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
 
1011
        $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
 
1012
    }
 
1013
    
 
1014
    if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)/m) 
 
1015
    {
 
1016
        ($name, $version) = ($1, $2);
 
1017
        $name    =~ s/[\[\]\s]//g;
 
1018
        $version =~ s/[\[\]\s]//g;
 
1019
        $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
 
1020
        $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
 
1021
        $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
 
1022
        $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
 
1023
    }
 
1024
 
 
1025
    # \s makes this not work, why?
 
1026
    $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
 
1027
    
 
1028
    # m4 macros AC_PACKAGE_NAME, AC_PACKAGE_VERSION etc. have same value
 
1029
    # as corresponding $PACKAGE_NAME, $PACKAGE_VERSION etc. shell variables.
 
1030
    $name =~ s/\bAC_PACKAGE_/\$PACKAGE_/g;
 
1031
 
 
1032
    $name = $domain if $domain;
 
1033
 
 
1034
    $name = SubstituteVariable ($name);
 
1035
    $name =~ s/^["'](.*)["']$/$1/;
 
1036
 
 
1037
    return $name if $name;
 
1038
}
 
1039
 
 
1040
 
 
1041
sub FindPOTKeywords
 
1042
{
 
1043
 
 
1044
    my $keywords = "--keyword\=\_ --keyword\=N\_ --keyword\=U\_ --keyword\=Q\_";
 
1045
    my $varname = "XGETTEXT_OPTIONS";
 
1046
    my $make_source; {
 
1047
        local (*IN);
 
1048
        open (IN, "<Makevars") || (open(IN, "<Makefile.in.in") && ($varname = "XGETTEXT_KEYWORDS")) || return $keywords;
 
1049
        seek (IN, 0, 0);
 
1050
        local $/; # slurp mode
 
1051
        $make_source = <IN>;
 
1052
        close IN;
 
1053
    }
 
1054
 
 
1055
    $keywords = $1 if $make_source =~ /^$varname[ ]*=\[?([^\n\]]+)/m;
 
1056
    
 
1057
    return $keywords;
 
1058
}
 
1059
 
 
1060
sub FindMakevarsDomain
 
1061
{
 
1062
 
 
1063
    my $domain = "";
 
1064
    my $makevars_source; { 
 
1065
        local (*IN);
 
1066
        open (IN, "<Makevars") || return $domain;
 
1067
        seek (IN, 0, 0);
 
1068
        local $/; # slurp mode
 
1069
        $makevars_source = <IN>;
 
1070
        close IN;
 
1071
    }
 
1072
 
 
1073
    $domain = $1 if $makevars_source =~ /^DOMAIN[ ]*=\[?([^\n\]\$]+)/m;
 
1074
    $domain =~ s/^\s+//;
 
1075
    $domain =~ s/\s+$//;
 
1076
    
 
1077
    return $domain;
 
1078
}
 
1079
 
 
1080
sub FindMakevarsBugAddress
 
1081
{
 
1082
 
 
1083
    my $address = "";
 
1084
    my $makevars_source; { 
 
1085
        local (*IN);
 
1086
        open (IN, "<Makevars") || return undef;
 
1087
        seek (IN, 0, 0);
 
1088
        local $/; # slurp mode
 
1089
        $makevars_source = <IN>;
 
1090
        close IN;
 
1091
    }
 
1092
 
 
1093
    $address = $1 if $makevars_source =~ /^MSGID_BUGS_ADDRESS[ ]*=\[?([^\n\]\$]+)/m;
 
1094
    $address =~ s/^\s+//;
 
1095
    $address =~ s/\s+$//;
 
1096
    
 
1097
    return $address;
 
1098
}