File: xml-i18n-update.in

package info (click to toggle)
glade-nonus 0.6.4-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,152 kB
  • ctags: 3,879
  • sloc: ansic: 54,332; sh: 6,839; makefile: 743; sed: 93
file content (516 lines) | stat: -rw-r--r-- 12,926 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!@XML_I18N_TOOLS_PERL@ -w

#  The GNOME Translation Update Tool
#
#  Copyright (C) 2000 Free Software Foundation.
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This script is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this library; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  Author(s): Kenneth Christiansen

## Release information
my $PROGRAM  = "xml-i18n-update";
my $VERSION  = "0.9";
my $_PACKAGE = "xml-i18n-tools";

## Loaded modules
use strict;
use Getopt::Long;
use Cwd;
use File::Copy;
use File::Find;

## Scalars used by the option stuff
my $LANG     	   = $ARGV[0];
my $HELP_ARG 	   = "0";
my $VERSION_ARG    = "0";
my $DIST_ARG	   = "0";
my $POT_ARG	   = "0";
my $HEADERS_ARG    = "0";
my $MAINTAIN_ARG   = "0";
my $REPORT_ARG     = "0";
my $VERBOSE	   = "0";

my @languages;
my %po_files_by_lang = ();

my $xml_extension = 
"xml(\.in)*|".		# .in is not required
"ui|".
"glade(\.in)*|".	# .in is not required
"desktop(\.in)+|".
"directory(\.in)+|".
"soundlist(\.in)+|".
"keys(\.in)+|".
"oaf(\.in)+|".
"server(\.in)+|".
"etspec|".
"pong(\.in)+";

my $PACKAGE = &find_package_name;

## Always print as the first thing
$| = 1;

## Give error if script is run without an argument
if (! $LANG){
    print "${PROGRAM}:  missing file arguments\n";
    print "Try `${PROGRAM} --help' for more information.\n";
    exit;
}

## Handle options
GetOptions (
	    "help|h|?"  	=> \$HELP_ARG,
	    "version|v" 	=> \$VERSION_ARG,
	    "dist|d"		=> \$DIST_ARG,
	    "pot|p"		=> \$POT_ARG,
	    "headers|s"		=> \$HEADERS_ARG,
	    "maintain|m"	=> \$MAINTAIN_ARG,
	    "report|r"		=> \$REPORT_ARG,
	    "verbose|x"		=> \$VERBOSE
	    ) or &invalid_option;


## Use the supplied arguments
## Check for options.
## This section will check for the different options.

sub split_on_argument {

    if ($VERSION_ARG) {
	&version;

    } elsif ($HELP_ARG) {
	&help;

    } elsif ($DIST_ARG) {
        &merging;
        &status;

    } elsif ($POT_ARG) {
        &gen_headers;
        &generate_pot;

    } elsif ($HEADERS_ARG) {
        &gen_headers;
        exit;

    } elsif ($MAINTAIN_ARG) {
        &maintain;

    } elsif ($REPORT_ARG) {
        &show_status;

    } elsif ($LANG) {
	if ($LANG =~ /^-/){ ## not an option
	    &help;
	} else {
	    &main;
 	}

    } else {
	&help;
    }
}

&split_on_argument;

sub main
{
   if(-s "$LANG.po"){
	print "Working, please wait..." unless $VERBOSE;
        &gen_headers;
	&generate_pot;
	&merging;
	&status;
   }

   ## Report error if the language file supplied
   ## to the command line is non-existent
   else {
	&not_existing;
   }
}

sub determine_type($) {
   my $type = $_;

   my $gettext_type;

   if ($type =~ /\[type: (gettext\/[^\]].*)]/) {
        $gettext_type=$1;
   }
   elsif ($type =~ /(?:xml(\.in)*|ui|oaf(?:\.in)+|pong(?:\.in)+|etspec)$/) {
        $gettext_type="gettext\/xml";
   }
   elsif ($type =~ /glade(\.in)*$/) {
        $gettext_type="gettext\/glade";
   }
   elsif ($type =~ /(?:desktop(?:\.in)+|directory(?:\.in)+|soundlist(?:\.in)+)$/) {
        $gettext_type="gettext\/ini";
   }
   elsif ($type =~ /keys(\.in)+$/) {
        $gettext_type="gettext\/keys";
   }
   else { $gettext_type=""; }

   return $gettext_type;
}

sub version{

    ## Print version information
    print "${PROGRAM} (${_PACKAGE}) $VERSION\n";
    print "Written by Kenneth Christiansen <kenneth\@gnome.org>, 2000.\n\n";
    print "Copyright (C) 2000 Free Software Foundation, Inc.\n";
    print "This is free software; see the source for copying conditions.  There is NO\n";
    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
    exit;
}

sub help
{
    ## Print usage information
    print "Usage: ./${PROGRAM} [OPTIONS] ...LANGCODE\n";
    print "Updates pot files and merge them with the translations.\n\n";
    print "  -H, --help                   shows this help page\n";
    print "  -P, --pot                    generate the pot file only\n";
    print "  -S, --headers                generate the XML headerfiles in POTFILES.in\n";
    print "  -M, --maintain               search for missing files in POTFILES.in\n";
    print "  -R, --report                 creates a status report for the module.\n";
    print "  -X, --verbose                show lots of feedback\n";
    print "  -V, --version                shows the version\n";
    print "\nExamples of use:\n";
    print "${PROGRAM} --pot    just creates a new pot file from the source\n";
    print "${PROGRAM} da       created new pot file and updated the da.po file\n\n";
    print "Report bugs to <kenneth\@gnome.org>.\n";
    exit;
}

sub maintain
{
    my (@buf_i18n_plain,
	@buf_i18n_xml,
	@buf_potfiles,
	@buf_potfiles_ignore,
	@buf_allfiles,
	@buf_allfiles_sorted,
	@buf_potfiles_sorted
    );

    ## Search and find all translatable files
    find sub { push @buf_i18n_plain, "$File::Find::name" if /\.(c|y|cc|c\+\+|h|gob)$/ }, "..";
    find sub { push @buf_i18n_xml, "$File::Find::name" if /\.($xml_extension)$/ }, "..";

    open(POTFILES, "POTFILES.in") || die "$PROGRAM:  there's no POTFILES.in!!!\n";
    @buf_potfiles = <POTFILES>;

    print "Searching for missing translatable files...\n";

    ## Check if we should ignore some found files, when
    ## comparing with POTFILES.in
    if (-s "POTFILES.skip"){
        open FILE, "POTFILES.skip";
        while (<FILE>) {
            if (/^[^#]/){
                push @buf_potfiles_ignore, $_;
            }
        }
        print "Found POTFILES.skip: Ignoring files...\n";
        @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
    }

    foreach my $file (@buf_i18n_plain){
        open FILE, "<$file";
        while (<FILE>) {
            if (/_\(\"/){
		## Remove the first 3 chars and add newline
		push @buf_allfiles, unpack("x3 A*", $file) . "\n";
                last;
            }
        }
    }

    foreach my $file (@buf_i18n_xml){
        open FILE, "<$file";
        while (<FILE>) {
            if (/\s_(.*)=\"/){
                ## Remove the first 3 chars and add newline
                push @buf_allfiles, unpack("x3 A*", $file) . "\n";
                last;
            }
        }
    }

    @buf_allfiles_sorted = sort (@buf_allfiles);
    @buf_potfiles_sorted = sort (@buf_potfiles);

    my %in2;
    foreach (@buf_potfiles_sorted) {
        $in2{$_} = 1;
    }

    my @result;

    foreach (@buf_allfiles_sorted){
        if (!exists($in2{$_})){
            push @result, $_
        }
    }

    ## Save file with information about the files missing
    ## if any, and give information about this proceedier
    if(@result){
        open OUT, ">missing";
        print OUT @result;
        print "\nHere is the result:\n\n", @result, "\n";
        print "The file \"missing\" has been placed in the current directory.\n";
        print "Files supposed to be ignored should be placed in \"POTFILES.skip\"\n";
    }

    ## If there is nothing to complain about, notice the user
    else{
        print "\nWell, it's all perfect! Congratulation!\n";
    }
}

sub invalid_option
{
    ## Handle invalid arguments
    print "${PROGRAM}: invalid option -- $LANG\n";
    print "Try `${PROGRAM} --help' for more information.\n";
    exit 1;
}

sub gen_headers
{
    my $XML_I18N_EXTRACT = `which xml-i18n-extract 2>/dev/null`;
    chomp $XML_I18N_EXTRACT;

    $XML_I18N_EXTRACT = $ENV{"XML_I18N_EXTRACT"} if $ENV{"XML_I18N_EXTRACT"};

    ## Generate the .h header files, so we can allow glade and
    ## xml translation support
    if (! -s $XML_I18N_EXTRACT)
    {
	print "\n *** The xml-i18n-extract script wasn't found!"
	     ."\n *** Without this xml-i18n-update can not generate files.\n";
	exit;
    }
    else
    {
        open FILE, "<POTFILES.in";
        while (<FILE>) {
           chomp;

           ## Find xml files in POTFILES.in and generate the
           ## files with help from the xml-i18n-extract script

	   my $gettext_type=&determine_type($1);

           if (/\.($xml_extension)$/ || /^\[/){
	       $_ =~ s/^\[[^\[].*]\s*//;
               my $filename = "../$_";

               if ($VERBOSE){
                   system($XML_I18N_EXTRACT, "--update", "--type=$gettext_type", $filename);
               } else {
	 	   system($XML_I18N_EXTRACT, "--update", "--type=$gettext_type", "--quiet", $filename);
               }
           }
       }
       close FILE;
   }
}

sub generate_pot
{
    ## Generate the potfiles from the POTFILES.in file

    print "Building the $PACKAGE.pot...\n" if $VERBOSE;

    move("POTFILES.in", "POTFILES.in.old");

    open INFILE, "<POTFILES.in.old";
    open OUTFILE, ">POTFILES.in";
    while (<INFILE>) {
        s/\.($xml_extension)$/$&.h/;
        s/^\[.*]\s*(.*)/$1.h/;
        print OUTFILE $_;
    }
    close OUTFILE;
    close INFILE;

    my $gettext_test   ="test \! -f $PACKAGE\.po \|\| \( rm -f \.\/$PACKAGE\.pot "
                       ."&& mv $PACKAGE\.po \.\/$PACKAGE\.pot \)";

    system("xgettext", "--default-domain\=$PACKAGE", "--directory\=\.\.",
	   "--add-comments", "--keyword\=\_", "--keyword\=N\_",
	   "--files-from\=\.\/POTFILES\.in");

    system($gettext_test);

    print "Wrote $PACKAGE.pot\n" if $VERBOSE;

    move("POTFILES.in.old", "POTFILES.in");

    print "Removing generated header (.h) files..." if $VERBOSE;

    open FILE, "<POTFILES.in";
    while (<FILE>)
    {
        chomp;
        unlink "../$_.h" if /\.($xml_extension)$/;
    }
    close FILE;
    print "done\n" if $VERBOSE;
}

sub merging
{
    if ($ARGV[1]){
        $LANG   = $ARGV[1];
    } else {
	$LANG   = $ARGV[0];
    }

    if ($ARGV[0] ne "--dist" && $ARGV[0] ne "-D") {
        print "Merging $LANG.po with $PACKAGE.pot..." if $VERBOSE;
    }

    &perform_merge($LANG);
    ## Remove the "messages" trash file generated by gettext
    unlink "messages";
}

sub perform_merge
{
    my ($LANG) = @_;

    copy("$LANG.po", "$LANG.po.old") || die "copy failed: $!";

    ## Preform merge
    system("msgmerge", "$LANG.po.old", "$PACKAGE.pot", "-o", "$LANG.po");

    ## Remove the backup file
    unlink "$LANG.po.old";
}

sub not_existing
{
    ## Report error if supplied language file is non-existing
    print "$PROGRAM:  sorry, $LANG.po does not exist!\n";
    print "Try `$PROGRAM --help' for more information.\n";
    exit;
}

sub gather_po_files
{
    my @po_files = glob("./*.po");

    @languages = map (&po_file2lang, @po_files);

    foreach my $lang (@languages) {
	$po_files_by_lang{$lang} = shift (@po_files);
    }
}

sub po_file2lang
{
    my $tmp = $_;
    $tmp =~ s/^.*\/(.*)\.po$/$1/;
    return $tmp;
}

sub status
{
    ## Print statistics
    system("msgfmt", "--statistics", "$LANG.po");
    print "\n";
}

sub show_status
{
    &gen_headers;
    &generate_pot;
    &gather_po_files;

    foreach my $lang (@languages){
	print "$lang: ";
	&perform_merge($lang);
    }

    print "\n\n * Current translation support in $PACKAGE \n\n";

    foreach my $lang (@languages){
        print "$lang: ";
 	## Print statistics
	system("msgfmt", "--statistics", "$lang.po");
    }
}

sub find_package_name
{
    my $base_dirname = getcwd();
    $base_dirname =~ s@.*/@@;

    my ($conf_in, $src_dir);

    if ($base_dirname eq "po") {
        if (-f "../configure.in") {
            $conf_in = "../configure.in";
        } else {
	    my $makefile_source;
	    local (*IN);
	    open (IN, "<Makefile") || die "can't open Makefile: $!";

	    while (<IN>) {
		if (/^top_srcdir[ \t]*=/) {
		    $src_dir = $_;
		    # print "${src_dir}\n";

		    $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
		    # print "${src_dir}\n";
		    chomp $src_dir;
		    $conf_in = "$src_dir" . "/configure.in" . "\n";
		    last;
		}
	    }
	    $conf_in || die "Cannot find top_srcdir in Makefile."
        }

        my $conf_source; {
           local (*IN);
           local $/; # slurp mode
           open (IN, "<$conf_in") || die "can't open $conf_in: $!";
           $conf_source = <IN>;
        }

        if ($conf_source =~ /AM_INIT_AUTOMAKE\(([^,]*),(.*)/) {
            my $package_name = $1;
            if ($package_name =~ /^[\$](.*)/){
                if ($conf_source =~ /$1=(.*)/) {
                    $package_name = $1;
                }
            }
	    return $package_name;
        }
    }

    print "$PROGRAM: Unable to determine package name.\n" .
	  "Make sure to run this script inside the po directory.\n";
    exit;
}