4 # The Intltool Message Updater
6 # Copyright (C) 2000-2002 Free Software Foundation.
8 # Intltool is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # version 2 published by the Free Software Foundation.
12 # Intltool is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 # As a special exception to the GNU General Public License, if you
22 # distribute this file as part of a program that contains a
23 # configuration script generated by Autoconf, you may include it under
24 # the same distribution terms that you use for the rest of that program.
26 # Authors: Kenneth Christiansen <kenneth@gnu.org>
28 # Darin Adler <darin@bentspoon.com>
30 ## Release information
31 my $PROGRAM = "intltool-update";
33 my $PACKAGE = "intltool";
42 ## Scalars used by the option stuff
51 my $GETTEXT_PACKAGE = "";
54 my %po_files_by_lang = ();
56 # Regular expressions to categorize file types.
57 # FIXME: Please check if the following is correct
60 "xml(\.in)*|". # .in is not required
62 "glade2?(\.in)*|". # .in is not required
63 "scm(\.in)*|". # .in is not required
77 ## Always print as the first thing
84 "version" => \$VERSION_ARG,
85 "dist|d" => \$DIST_ARG,
87 "headers|s" => \$HEADERS_ARG,
88 "maintain|m" => \$MAINTAIN_ARG,
89 "report|r" => \$REPORT_ARG,
90 "verbose|x" => \$VERBOSE,
91 "gettext-package|g=s" => \$GETTEXT_PACKAGE,
92 ) or &print_error_invalid_option;
94 &print_help if $HELP_ARG;
95 &print_version if $VERSION_ARG;
97 my $arg_count = ($DIST_ARG > 0)
100 + ($MAINTAIN_ARG > 0)
102 &print_help if $arg_count > 1;
104 # --version and --help don't require a module name
105 my $MODULE = $GETTEXT_PACKAGE || &find_package_name;
108 if ($ARGV[0] =~ /^[a-z]/){
109 &update_po_file ($ARGV[0]);
110 &print_status ($ARGV[0]);
116 &generate_po_template;
117 } elsif ($HEADERS_ARG) {
119 } elsif ($MAINTAIN_ARG) {
121 } elsif ($REPORT_ARG) {
124 if ($ARGV[0] =~ /^[a-z]/) {
137 ## Print version information
138 print "${PROGRAM} (${PACKAGE}) $VERSION\n";
139 print "Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.\n\n";
140 print "Copyright (C) 2000-2002 Free Software Foundation, Inc.\n";
141 print "This is free software; see the source for copying conditions. There is NO\n";
142 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
148 ## Print usage information
149 print "Usage: ${PROGRAM} [OPTIONS] ...LANGCODE\n";
150 print "Updates PO template files and merge them with the translations.\n\n";
151 print " -p, --pot generate the PO template only\n";
152 print " -s, --headers generate the header files in POTFILES.in\n";
153 print " -m, --maintain search for left out files from POTFILES.in\n";
154 print " -r, --report display a status report for the module.\n";
155 print " -x, --verbose display lots of feedback\n";
156 print " --help display this help and exit\n";
157 print " --version output version information and exit\n";
158 print "\nExamples of use:\n";
159 print "${PROGRAM} --pot just creates a new PO template from the source\n";
160 print "${PROGRAM} da created new PO template and updated the da.po file\n\n";
161 print "Report bugs to bugzilla.gnome.org, module 'intltool'.\n";
169 ## Report error if the language file supplied
170 ## to the command line is non-existent
171 &print_error_not_existing("$lang.po") if ! -s "$lang.po";
173 print "Working, please wait..." unless $VERBOSE;
175 &generate_po_template;
176 &update_po_file ($lang);
177 &print_status ($lang);
180 sub determine_type ($)
185 # FIXME: Use $xml_extentions, and maybe do all this even nicer
187 "(?:xml(\.in)*|ui|oaf(?:\.in)+|server(?:\.in)+|sheet(?:\.in)+|".
188 "pong(?:\.in)+|etspec)";
190 "(?:desktop(?:\.in)+|caves(?:\.in)+|directory(?:\.in)+|".
191 "soundlist(?:\.in)+)";
193 if ($type =~ /\[type: gettext\/([^\]].*)]/) {
196 elsif ($type =~ /$xml_regex$/) {
199 elsif ($type =~ /glade2?(\.in)*$/) {
200 $gettext_type="glade";
202 elsif ($type =~ /$ini_regex$/) {
205 elsif ($type =~ /scm(\.in)*$/) {
206 $gettext_type="scheme";
208 elsif ($type =~ /keys(\.in)+$/) {
209 $gettext_type="keys";
211 else { $gettext_type=""; }
213 return "gettext\/$gettext_type";
216 sub find_leftout_files
222 @buf_potfiles_ignore,
224 @buf_allfiles_sorted,
228 ## Search and find all translatable files
230 push @buf_i18n_plain, "$File::Find::name" if /\.(c|y|cc|cpp|c\+\+|h|gob)$/
233 push @buf_i18n_xml, "$File::Find::name" if /\.($xml_extension)$/
236 push @buf_i18n_ini, "$File::Find::name" if /\.($ini_extension)$/
239 open POTFILES, "POTFILES.in" || die "$PROGRAM: there's no POTFILES.in!\n";
241 @buf_potfiles = grep /^[^#]/, <POTFILES>;
243 print "Searching for missing translatable files...\n" if $VERBOSE;
245 ## Check if we should ignore some found files, when
246 ## comparing with POTFILES.in
247 foreach my $ignore ("POTFILES.skip", "POTFILES.ignore") {
252 push @buf_potfiles_ignore, $_;
255 print "Found $ignore: Ignoring files...\n" if $VERBOSE;
256 @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
260 foreach my $file (@buf_i18n_plain)
268 # Handle continued multi-line comment.
271 next unless s-.*\*/--;
275 # Handle continued macro.
278 $in_macro = 0 unless /\\$/;
282 # Handle start of macro (or any preprocessor directive).
285 $in_macro = 1 if /^([^\\]|\\.)*\\$/;
289 # Handle comments and quoted text.
290 while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
301 elsif ($match eq "//")
307 if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
309 warn "mismatched quotes at line $. in $file\n";
318 ## Remove the first 3 chars and add newline
319 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
326 foreach my $file (@buf_i18n_xml) {
330 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
336 foreach my $file (@buf_i18n_ini){
340 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
346 @buf_allfiles_sorted = sort (@buf_allfiles);
347 @buf_potfiles_sorted = sort (@buf_potfiles);
350 foreach (@buf_potfiles_sorted) {
356 foreach (@buf_allfiles_sorted){
357 if (!exists($in2{$_})){
362 ## Save file with information about the files missing
363 ## if any, and give information about this procedure.
365 print "\n" if $VERBOSE;
366 open OUT, ">missing";
368 print "The following files contain translations and are currently not in use. Please\n";
369 print "consider adding these to the POTFILES.in file, located in the po/ directory.\n\n";
371 print "If some of these files are left out on purpose then please add them to\n";
372 print "POTFILES.skip instead of POTFILES.in. A file 'missing' containing this list\n";
373 print "of left out files has been written in the current directory.\n";
376 ## If there is nothing to complain about, notify the user
378 print "\nAll files containing translations are present in POTFILES.in.\n";
382 sub print_error_invalid_option
384 ## Handle invalid arguments
385 print "Try `${PROGRAM} --help' for more information.\n";
391 my $EXTRACT = `which intltool-extract 2>/dev/null`;
394 $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} if $ENV{"INTLTOOL_EXTRACT"};
396 ## Generate the .h header files, so we can allow glade and
397 ## xml translation support
400 print "\n *** The intltool-extract script wasn't found!"
401 ."\n *** Without it, intltool-update can not generate files.\n";
406 open FILE, "<POTFILES.in";
410 ## Find xml files in POTFILES.in and generate the
411 ## files with help from the extract script
413 my $gettext_type= &determine_type ($1);
415 if (/\.($xml_extension|$ini_extension)$/ || /^\[/){
416 $_ =~ s/^\[[^\[].*]\s*//;
417 my $filename = "../$_";
418 my $srcdir = $ENV{'top_srcdir'};
420 $filename="$srcdir/$_" ;
424 system($EXTRACT, "--update", "--type=$gettext_type", $filename);
426 system($EXTRACT, "--update", "--type=$gettext_type", "--quiet", $filename);
434 sub generate_po_template
436 ## Generate the potfiles from the POTFILES.in file
438 print "Building the $MODULE.pot...\n" if $VERBOSE;
440 move ("POTFILES.in", "POTFILES.in.old");
442 my $srcdir = $ENV{'top_srcdir'};
443 my $adddirectory = "--directory=." ;
445 $adddirectory = "--directory=$srcdir";
448 open INFILE, "<POTFILES.in.old";
449 open OUTFILE, ">POTFILES.in";
451 s/\.($xml_extension|$ini_extension)$/$&.h/;
452 s/^\[.*]\s*(.*)/$1.h/;
458 system ("xgettext", "--default-domain\=$MODULE",
465 "--files-from\=\.\/POTFILES\.in");
467 if (!-e "$MODULE.po") {
468 print "WARNING: It seems that none of the files in POTFILES.in ".
469 "contain marked strings\n";
473 system ("rm", "-f", "$MODULE.pot");
474 move ("$MODULE.po", "$MODULE.pot") || die "$PROGRAM: couldn't move $MODULE.po to $MODULE.pot.\n";
476 print "Wrote $MODULE.pot\n" if $VERBOSE;
478 move ("POTFILES.in.old", "POTFILES.in");
480 print "Removing generated header (.h) files..." if $VERBOSE;
482 open FILE, "<POTFILES.in";
487 unlink "../$_.h" if /\.($xml_extension|$ini_extension)$/;
491 print "done\n" if $VERBOSE;
498 print "Merging $lang.po with $MODULE.pot..." if $VERBOSE;
500 copy ("$lang.po", "$lang.po.old") || die "copy failed: $!";
502 # Perform merge, remove backup file and the "messages" trash file
503 # generated by gettext
504 system ("msgmerge", "$lang.po.old", "$MODULE.pot", "-o", "$lang.po");
505 unlink "$lang.po.old";
509 sub print_error_not_existing
513 ## Report error if supplied language file is non-existing
514 print "$PROGRAM: $file does not exist!\n";
515 print "Try '$PROGRAM --help' for more information.\n";
521 my @po_files = glob ("./*.po");
523 @languages = map (&po_file2lang, @po_files);
525 foreach my $lang (@languages) {
526 $po_files_by_lang{$lang} = shift (@po_files);
533 $tmp =~ s/^.*\/(.*)\.po$/$1/;
541 system ("msgfmt", "--statistics", "$lang.po");
548 &generate_po_template;
551 foreach my $lang (@languages) {
553 &update_po_file ($lang);
556 print "\n\n * Current translation support in $MODULE \n\n";
558 foreach my $lang (@languages){
560 system ("msgfmt", "--statistics", "$lang.po");
564 sub find_package_name
566 my $base_dirname = getcwd();
567 $base_dirname =~ s@.*/@@;
569 my ($conf_in, $src_dir);
571 if ($base_dirname =~ /^po(-.+)?$/) {
572 if (-f "../configure.in") {
573 $conf_in = "../configure.in";
577 open IN, "<Makefile" || die "can't open Makefile: $!";
580 if (/^top_srcdir[ \t]*=/) {
582 # print "${src_dir}\n";
584 $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
585 # print "${src_dir}\n";
587 $conf_in = "$src_dir" . "/configure.in" . "\n";
591 $conf_in || die "Cannot find top_srcdir in Makefile."
596 local $/; # slurp mode
597 open (IN, "<$conf_in") || die "can't open $conf_in: $!";
602 $name = $1 if $conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+)/m;
603 $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=(\S+)/m;
604 if ($name =~ /^[\$](\S+)/) {
605 return $1 if $conf_source =~ /^\s*$1=(\S*)/m;
607 return $name if $name;
610 print "$PROGRAM: Unable to determine package name.\n" .
611 "Make sure to run this script inside the po directory.\n";