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
78 ## Always print as the first thing
85 "version" => \$VERSION_ARG,
86 "dist|d" => \$DIST_ARG,
88 "headers|s" => \$HEADERS_ARG,
89 "maintain|m" => \$MAINTAIN_ARG,
90 "report|r" => \$REPORT_ARG,
91 "verbose|x" => \$VERBOSE,
92 "gettext-package|g=s" => \$GETTEXT_PACKAGE,
93 ) or &print_error_invalid_option;
95 &print_help if $HELP_ARG;
96 &print_version if $VERSION_ARG;
98 my $arg_count = ($DIST_ARG > 0)
101 + ($MAINTAIN_ARG > 0)
103 &print_help if $arg_count > 1;
105 # --version and --help don't require a module name
106 my $MODULE = $GETTEXT_PACKAGE || &find_package_name;
109 if ($ARGV[0] =~ /^[a-z]/){
110 &update_po_file ($ARGV[0]);
111 &print_status ($ARGV[0]);
117 &generate_po_template;
118 } elsif ($HEADERS_ARG) {
120 } elsif ($MAINTAIN_ARG) {
122 } elsif ($REPORT_ARG) {
125 if ($ARGV[0] =~ /^[a-z]/) {
138 ## Print version information
139 print "${PROGRAM} (${PACKAGE}) $VERSION\n";
140 print "Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.\n\n";
141 print "Copyright (C) 2000-2002 Free Software Foundation, Inc.\n";
142 print "This is free software; see the source for copying conditions. There is NO\n";
143 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
149 ## Print usage information
150 print "Usage: ${PROGRAM} [OPTIONS] ...LANGCODE\n";
151 print "Updates PO template files and merge them with the translations.\n\n";
152 print " -p, --pot generate the PO template only\n";
153 print " -s, --headers generate the header files in POTFILES.in\n";
154 print " -m, --maintain search for left out files from POTFILES.in\n";
155 print " -r, --report display a status report for the module.\n";
156 print " -x, --verbose display lots of feedback\n";
157 print " --help display this help and exit\n";
158 print " --version output version information and exit\n";
159 print "\nExamples of use:\n";
160 print "${PROGRAM} --pot just creates a new PO template from the source\n";
161 print "${PROGRAM} da created new PO template and updated the da.po file\n\n";
162 print "Report bugs to bugzilla.gnome.org, module 'intltool'.\n";
170 ## Report error if the language file supplied
171 ## to the command line is non-existent
172 &print_error_not_existing("$lang.po") if ! -s "$lang.po";
174 print "Working, please wait..." unless $VERBOSE;
176 &generate_po_template;
177 &update_po_file ($lang);
178 &print_status ($lang);
181 sub determine_type ($)
186 # FIXME: Use $xml_extentions, and maybe do all this even nicer
188 "(?:xml(\.in)*|ui|oaf(?:\.in)+|server(?:\.in)+|sheet(?:\.in)+|".
189 "pong(?:\.in)+|etspec|schemas(?:\.in)+)";
191 "(?:desktop(?:\.in)+|caves(?:\.in)+|directory(?:\.in)+|".
192 "soundlist(?:\.in)+)";
194 if ($type =~ /\[type: gettext\/([^\]].*)]/) {
197 elsif ($type =~ /schemas(\.in)+$/) {
198 $gettext_type="schemas";
200 elsif ($type =~ /$xml_regex$/) {
203 elsif ($type =~ /glade2?(\.in)*$/) {
204 $gettext_type="glade";
206 elsif ($type =~ /$ini_regex$/) {
209 elsif ($type =~ /scm(\.in)*$/) {
210 $gettext_type="scheme";
212 elsif ($type =~ /keys(\.in)+$/) {
213 $gettext_type="keys";
215 else { $gettext_type=""; }
217 return "gettext\/$gettext_type";
220 sub find_leftout_files
224 @buf_i18n_xml_unmarked,
227 @buf_potfiles_ignore,
229 @buf_allfiles_sorted,
233 ## Search and find all translatable files
235 push @buf_i18n_plain, "$File::Find::name" if /\.(c|y|cc|cpp|c\+\+|h|gob)$/
238 push @buf_i18n_xml, "$File::Find::name" if /\.($xml_extension)$/
241 push @buf_i18n_ini, "$File::Find::name" if /\.($ini_extension)$/
244 push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/
248 open POTFILES, "POTFILES.in" or die "$PROGRAM: there's no POTFILES.in!\n";
250 @buf_potfiles = grep /^[^#]/, <POTFILES>;
252 print "Searching for missing translatable files...\n" if $VERBOSE;
254 ## Check if we should ignore some found files, when
255 ## comparing with POTFILES.in
256 foreach my $ignore ("POTFILES.skip", "POTFILES.ignore") {
261 push @buf_potfiles_ignore, $_;
264 print "Found $ignore: Ignoring files...\n" if $VERBOSE;
265 @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
269 foreach my $file (@buf_i18n_plain)
277 # Handle continued multi-line comment.
280 next unless s-.*\*/--;
284 # Handle continued macro.
287 $in_macro = 0 unless /\\$/;
291 # Handle start of macro (or any preprocessor directive).
294 $in_macro = 1 if /^([^\\]|\\.)*\\$/;
298 # Handle comments and quoted text.
299 while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
310 elsif ($match eq "//")
316 if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
318 warn "mismatched quotes at line $. in $file\n";
327 ## Remove the first 3 chars and add newline
328 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
335 foreach my $file (@buf_i18n_xml) {
338 if (/\s_(.*)=\"/ || /translatable=\"yes\"/){
339 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
345 foreach my $file (@buf_i18n_ini){
349 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
355 foreach my $file (@buf_i18n_xml_unmarked){
356 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
360 @buf_allfiles_sorted = sort (@buf_allfiles);
361 @buf_potfiles_sorted = sort (@buf_potfiles);
364 foreach (@buf_potfiles_sorted) {
370 foreach (@buf_allfiles_sorted){
371 if (!exists($in2{$_})){
376 ## Save file with information about the files missing
377 ## if any, and give information about this procedure.
379 print "\n" if $VERBOSE;
380 open OUT, ">missing";
382 print "The following files contain translations and are currently not in use. Please\n";
383 print "consider adding these to the POTFILES.in file, located in the po/ directory.\n\n";
385 print "If some of these files are left out on purpose then please add them to\n";
386 print "POTFILES.skip instead of POTFILES.in. A file 'missing' containing this list\n";
387 print "of left out files has been written in the current directory.\n";
390 ## If there is nothing to complain about, notify the user
392 print "\nAll files containing translations are present in POTFILES.in.\n";
396 sub print_error_invalid_option
398 ## Handle invalid arguments
399 print "Try `${PROGRAM} --help' for more information.\n";
405 my $EXTRACT = `which intltool-extract 2>/dev/null`;
408 $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} if $ENV{"INTLTOOL_EXTRACT"};
410 ## Generate the .h header files, so we can allow glade and
411 ## xml translation support
414 print "\n *** The intltool-extract script wasn't found!"
415 ."\n *** Without it, intltool-update can not generate files.\n";
420 open FILE, "<POTFILES.in";
424 ## Find xml files in POTFILES.in and generate the
425 ## files with help from the extract script
427 my $gettext_type= &determine_type ($1);
429 if (/\.($xml_extension|$ini_extension)$/ || /^\[/){
430 $_ =~ s/^\[[^\[].*]\s*//;
431 my $filename = "../$_";
434 system($EXTRACT, "--update", "--type=$gettext_type", $filename);
436 system($EXTRACT, "--update", "--type=$gettext_type", "--quiet", $filename);
444 sub generate_po_template
446 ## Generate the potfiles from the POTFILES.in file
448 print "Building the $MODULE.pot...\n" if $VERBOSE;
450 move ("POTFILES.in", "POTFILES.in.old");
452 open INFILE, "<POTFILES.in.old";
453 open OUTFILE, ">POTFILES.in";
455 s/\.($xml_extension|$ini_extension)$/$&.h/;
456 s/^\[.*]\s*(.*)/$1.h/;
462 system ("xgettext", "--default-domain\=$MODULE",
468 "--files-from\=\.\/POTFILES\.in");
470 move ("POTFILES.in.old", "POTFILES.in");
472 print "Removing generated header (.h) files..." if $VERBOSE;
474 open FILE, "<POTFILES.in";
479 unlink "../$_.h" if /\.($xml_extension|$ini_extension)$/;
483 print "done\n" if $VERBOSE;
485 if (!-e "$MODULE.po") {
486 print "WARNING: It seems that none of the files in POTFILES.in ".
487 "contain marked strings\n";
491 system ("rm", "-f", "$MODULE.pot");
492 move ("$MODULE.po", "$MODULE.pot") or die "$PROGRAM: couldn't move $MODULE.po to $MODULE.pot.\n";
494 print "Wrote $MODULE.pot\n" if $VERBOSE;
501 print "Merging $lang.po with $MODULE.pot..." if $VERBOSE;
503 copy ("$lang.po", "$lang.po.old") || die "copy failed: $!";
505 # Perform merge, remove backup file and the "messages" trash file
506 # generated by gettext
507 system ("msgmerge", "$lang.po.old", "$MODULE.pot", "-o", "$lang.po");
508 unlink "$lang.po.old";
512 sub print_error_not_existing
516 ## Report error if supplied language file is non-existing
517 print "$PROGRAM: $file does not exist!\n";
518 print "Try '$PROGRAM --help' for more information.\n";
524 my @po_files = glob ("./*.po");
526 @languages = map (&po_file2lang, @po_files);
528 foreach my $lang (@languages) {
529 $po_files_by_lang{$lang} = shift (@po_files);
536 $tmp =~ s/^.*\/(.*)\.po$/$1/;
544 system ("msgfmt", "--statistics", "$lang.po");
551 &generate_po_template;
554 foreach my $lang (@languages) {
556 &update_po_file ($lang);
559 print "\n\n * Current translation support in $MODULE \n\n";
561 foreach my $lang (@languages){
563 system ("msgfmt", "--statistics", "$lang.po");
567 sub find_package_name
569 my $base_dirname = getcwd();
570 $base_dirname =~ s@.*/@@;
572 my ($conf_in, $src_dir);
574 if ($base_dirname =~ /^po(-.+)?$/) {
575 if (-f "../configure.in") {
576 $conf_in = "../configure.in";
577 } elsif (-f "../configure.ac") {
578 $conf_in = "../configure.ac";
582 open IN, "<Makefile" || die "can't open Makefile: $!";
585 if (/^top_srcdir[ \t]*=/) {
587 # print "${src_dir}\n";
589 $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
590 # print "${src_dir}\n";
592 $conf_in = "$src_dir" . "/configure.in" . "\n";
596 $conf_in || die "Cannot find top_srcdir in Makefile."
602 open (IN, "<$conf_in") || die "can't open $conf_in: $!";
604 if (/^(\w+)=(\S+)/) { $varhash{$1} = $2 };
607 local $/; # slurp mode
612 $name = $1 if $conf_source =~ /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m;
613 if ($conf_source =~ /^AC_INIT\([\s\[]*([^,\)\s\]]+)\]?\s*,/m) {
615 $varhash{"AC_PACKAGE_NAME"} = $1;
617 $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\s\]]+)/m;
619 $name = "\$AC_PACKAGE_NAME" if "$name" eq "AC_PACKAGE_NAME";
622 while (($name =~ /[\$](\S+)/) && ("$oldname" ne "$name")) {
624 if (exists $varhash{$1}) {
625 $name =~ s/[\$](\S+)/$varhash{$1}/;
628 return $name if $name;
631 print "$PROGRAM: Unable to determine package name.\n" .
632 "Make sure to run this script inside the po directory.\n";