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 = "";
55 my $conf_file; # remove later
57 my %po_files_by_lang = ();
59 # Regular expressions to categorize file types.
60 # FIXME: Please check if the following is correct
63 "xml(\.in)*|". # .in is not required
66 "glade2?(\.in)*|". # .in is not required
67 "scm(\.in)*|". # .in is not required
83 ## Always print as the first thing
90 "version" => \$VERSION_ARG,
91 "dist|d" => \$DIST_ARG,
93 "headers|s" => \$HEADERS_ARG,
94 "maintain|m" => \$MAINTAIN_ARG,
95 "report|r" => \$REPORT_ARG,
96 "verbose|x" => \$VERBOSE,
97 "gettext-package|g=s" => \$GETTEXT_PACKAGE,
98 "output-file|o=s" => \$OUTPUT_FILE,
99 ) or &print_error_invalid_option;
101 &print_help if $HELP_ARG;
102 &print_version if $VERSION_ARG;
104 my $arg_count = ($DIST_ARG > 0)
107 + ($MAINTAIN_ARG > 0)
110 &print_help if $arg_count > 1;
112 # --version and --help don't require a module name
113 my $MODULE = $GETTEXT_PACKAGE || &find_package_name;
117 if ($ARGV[0] =~ /^[a-z]/)
119 &update_po_file ($ARGV[0], $OUTPUT_FILE);
120 &print_status ($ARGV[0], $OUTPUT_FILE);
130 &generate_po_template;
136 elsif ($MAINTAIN_ARG)
146 if ($ARGV[0] =~ /^[a-z]/)
148 &main ($ARGV[0], $OUTPUT_FILE);
163 ${PROGRAM} (${PACKAGE}) $VERSION
164 Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.
166 Copyright (C) 2000-2003 Free Software Foundation, Inc.
167 This is free software; see the source for copying conditions. There is NO
168 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
176 Usage: ${PROGRAM} [OPTION]... LANGCODE
177 Updates PO template files and merge them with the translations.
179 Mode of operation (only one is allowed):
180 -p, --pot generate the PO template only
181 -s, --headers generate the header files in POTFILES.in
182 -m, --maintain search for left out files from POTFILES.in
183 -r, --report display a status report for the module
184 -d, --dist merge LANGCODE.po with existing PO template
187 -g, --gettext-package=NAME override PO template name, useful with --pot
188 -o, --output-file=FILE write merged translation to FILE
189 -x, --verbose display lots of feedback
190 --help display this help and exit
191 --version output version information and exit
194 ${PROGRAM} --pot just create a new PO template
195 ${PROGRAM} xy create new PO template and merge xy.po with it
197 Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
198 or send email to <xml-i18n-tools\@gnome.org>.
205 my ($lang, $output_file) = @_;
207 ## Report error if the language file supplied
208 ## to the command line is non-existent
209 &print_error_not_existing("$lang.po") if ! -s "$lang.po";
211 print "Working, please wait..." if $VERBOSE;
213 &generate_po_template;
214 &update_po_file ($lang, $output_file);
215 &print_status ($lang, $output_file);
218 sub determine_type ($)
223 # FIXME: Use $xml_extentions, and maybe do all this even nicer
225 "(?:xml(\.in)*|ui|lang|oaf(?:\.in)+|server(?:\.in)+|sheet(?:\.in)+|".
226 "pong(?:\.in)+|etspec|schemas(?:\.in)+)";
228 "(?:desktop(?:\.in)+|theme(?:\.in)+|caves(?:\.in)+|directory(?:\.in)+|".
229 "soundlist(?:\.in)+)";
231 if ($type =~ /\[type: gettext\/([^\]].*)]/)
235 elsif ($type =~ /schemas(\.in)+$/)
237 $gettext_type="schemas";
239 elsif ($type =~ /$xml_regex$/)
243 elsif ($type =~ /glade2?(\.in)*$/)
245 $gettext_type="glade";
247 elsif ($type =~ /$ini_regex$/)
251 elsif ($type =~ /scm(\.in)*$/)
253 $gettext_type="scheme";
255 elsif ($type =~ /keys(\.in)+$/)
257 $gettext_type="keys";
264 return "gettext\/$gettext_type";
267 sub find_leftout_files
271 @buf_i18n_xml_unmarked,
274 @buf_potfiles_ignore,
276 @buf_allfiles_sorted,
280 ## Search and find all translatable files
282 push @buf_i18n_plain, "$File::Find::name" if /\.(c|y|cc|cpp|c\+\+|h|gob)$/
285 push @buf_i18n_xml, "$File::Find::name" if /\.($xml_extension)$/
288 push @buf_i18n_ini, "$File::Find::name" if /\.($ini_extension)$/
291 push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/
295 open POTFILES, "POTFILES.in" or die "$PROGRAM: there's no POTFILES.in!\n";
297 @buf_potfiles = grep /^[^#]/, <POTFILES>;
298 foreach (@buf_potfiles) {
302 print "Searching for missing translatable files...\n" if $VERBOSE;
304 ## Check if we should ignore some found files, when
305 ## comparing with POTFILES.in
306 foreach my $ignore ("POTFILES.skip", "POTFILES.ignore")
316 push @buf_potfiles_ignore, $_;
319 print "Found $ignore: Ignoring files...\n" if $VERBOSE;
320 @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
324 foreach my $file (@buf_i18n_plain)
332 # Handle continued multi-line comment.
335 next unless s-.*\*/--;
339 # Handle continued macro.
342 $in_macro = 0 unless /\\$/;
346 # Handle start of macro (or any preprocessor directive).
349 $in_macro = 1 if /^([^\\]|\\.)*\\$/;
353 # Handle comments and quoted text.
354 while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
365 elsif ($match eq "//")
371 if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
373 warn "mismatched quotes at line $. in $file\n";
381 ## Remove the first 3 chars and add newline
382 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
389 foreach my $file (@buf_i18n_xml)
395 if (/\s_(.*)=\"/ || /translatable=\"yes\"/)
397 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
403 foreach my $file (@buf_i18n_ini)
410 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
416 foreach my $file (@buf_i18n_xml_unmarked)
418 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
422 @buf_allfiles_sorted = sort (@buf_allfiles);
423 @buf_potfiles_sorted = sort (@buf_potfiles);
426 foreach (@buf_potfiles_sorted)
433 foreach (@buf_allfiles_sorted)
435 if (!exists($in2{$_}))
441 ## Save file with information about the files missing
442 ## if any, and give information about this procedure.
445 print "\n" if $VERBOSE;
446 open OUT, ">missing";
448 print STDERR "The following files contain translations and are currently not in use. Please\n";
449 print STDERR "consider adding these to the POTFILES.in file, located in the po/ directory.\n\n";
450 print STDERR @result, "\n";
451 print STDERR "If some of these files are left out on purpose then please add them to\n";
452 print STDERR "POTFILES.skip instead of POTFILES.in. A file 'missing' containing this list\n";
453 print STDERR "of left out files has been written in the current directory.\n";
456 ## If there is nothing to complain about, notify the user
458 print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
462 sub print_error_invalid_option
464 ## Handle invalid arguments
465 print STDERR "Try `${PROGRAM} --help' for more information.\n";
471 my $EXTRACT = `which intltool-extract 2>/dev/null`;
474 $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} if $ENV{"INTLTOOL_EXTRACT"};
476 ## Generate the .h header files, so we can allow glade and
477 ## xml translation support
480 print STDERR "\n *** The intltool-extract script wasn't found!"
481 ."\n *** Without it, intltool-update can not generate files.\n";
486 open (FILE, "<POTFILES.in") or die "$PROGRAM: POTFILES.in not found.\n";
492 ## Find xml files in POTFILES.in and generate the
493 ## files with help from the extract script
495 my $gettext_type= &determine_type ($1);
497 if (/\.($xml_extension|$ini_extension)$/ || /^\[/)
501 my $filename = "../$_";
505 system ($EXTRACT, "--update",
506 "--type=$gettext_type", $filename);
510 system ($EXTRACT, "--update", "--type=$gettext_type",
511 "--quiet", $filename);
519 sub generate_po_template
521 ## Generate the potfiles from the POTFILES.in file
523 print "Building the $MODULE.pot...\n" if $VERBOSE;
525 move ("POTFILES.in", "POTFILES.in.old");
527 open INFILE, "<POTFILES.in.old";
528 open OUTFILE, ">POTFILES.in";
533 if (/\.($xml_extension|$ini_extension)$/ || /^\[/)
536 print OUTFILE "$_.h\n";
540 print OUTFILE "$_\n";
547 system ("xgettext", "--default-domain\=$MODULE",
553 "--files-from\=\.\/POTFILES\.in");
555 move ("POTFILES.in.old", "POTFILES.in");
557 print "Removing generated header (.h) files..." if $VERBOSE;
559 open FILE, "<POTFILES.in";
564 unlink "../$_.h" if /\.($xml_extension|$ini_extension)$/;
568 print "done\n" if $VERBOSE;
570 if (!-e "$MODULE.po")
572 print STDERR "WARNING: Either none of the files in POTFILES.in contain marked strings,\n".
573 " or xgettext failed to generate PO template file. Please consult\n".
574 " error message above if there is any.\n";
578 system ("rm", "-f", "$MODULE.pot");
579 move ("$MODULE.po", "$MODULE.pot") or
580 die "$PROGRAM: couldn't move $MODULE.po to $MODULE.pot.\n";
582 print "Wrote $MODULE.pot\n" if $VERBOSE;
587 -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
589 my ($lang, $output_file) = @_;
590 my ($infile, $outfile);
592 print "Merging $lang.po with $MODULE.pot..." if $VERBOSE;
594 if ($output_file ne "")
596 $infile = "$lang.po";
597 $outfile = $output_file;
601 copy ("$lang.po", "$lang.po.old") || die "copy failed: $!";
602 $infile = "$lang.po.old";
603 $outfile = "$lang.po";
606 # Perform merge, remove backup file and the "messages" trash file
607 # generated by gettext
608 system ("msgmerge", "-o", $outfile, $infile, "$MODULE.pot");
610 if ($output_file eq "")
617 sub print_error_not_existing
621 ## Report error if supplied language file is non-existing
622 print STDERR "$PROGRAM: $file does not exist!\n";
623 print STDERR "Try '$PROGRAM --help' for more information.\n";
629 my @po_files = glob ("./*.po");
631 @languages = map (&po_file2lang, @po_files);
633 foreach my $lang (@languages)
635 $po_files_by_lang{$lang} = shift (@po_files);
642 $tmp =~ s/^.*\/(.*)\.po$/$1/;
648 my ($lang, $output_file) = @_;
650 if ($output_file eq "")
652 $output_file = "$lang.po";
655 system ("msgfmt", "-o", "/dev/null", "--statistics", $output_file);
662 &generate_po_template;
665 foreach my $lang (@languages)
668 &update_po_file ($lang, "");
671 print "\n\n * Current translation support in $MODULE \n\n";
673 foreach my $lang (@languages)
676 system ("msgfmt", "-o", "/dev/null", "--statistics", "$lang.po");
684 # cache each variable. varhash is global to we can add
685 # variables elsewhere.
694 if ($str =~ /^(.*)\${?([A-Z]+)}?(.*)$/)
698 my $sub = $varhash{$2};
700 return substitute_var ("$untouched$sub$rest");
707 my $base_dirname = getcwd();
708 $base_dirname =~ s@.*/@@;
710 my ($conf_in, $src_dir);
712 if ($base_dirname =~ /^po(-.+)?$/)
714 if (-f "../configure.in")
716 $conf_in = "../configure.in";
718 elsif (-f "../configure.ac")
720 $conf_in = "../configure.ac";
727 open IN, "<Makefile" || die "can't open Makefile: $!";
731 if (/^top_srcdir[ \t]*=/)
734 $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
737 $conf_in = "$src_dir" . "/configure.in" . "\n";
743 $conf_file || die "Cannot find top_srcdir in Makefile.";
746 $conf_file = $conf_in;
747 open (CONF, "<$conf_in") || die "can't open $conf_in: $!";
751 print STDERR "$PROGRAM: Unable to proceed.\n" .
752 "Make sure to run this script inside the po directory.\n";
757 sub find_package_name
763 open (IN, "<$conf_file") || die "can't open $conf_file: $!";
765 local $/; # slurp mode
769 my $name = "untitled";
771 # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m
772 # the \s makes this not work, why?
773 if ($conf_source =~ /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\]]+)/m)
778 if ($conf_source =~ /^AC_INIT\([\s\[]*([^,\)\s\]]+)\]?\s*,/m)
781 $varhash{"AC_PACKAGE_NAME"} = $1;
784 # \s makes this not work, why?
785 $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
787 $name = "\$AC_PACKAGE_NAME" if "$name" eq "AC_PACKAGE_NAME";
789 $name = substitute_var ($name);
791 return $name if $name;