1 #!@XML_I18N_TOOLS_PERL@ -w
3 # The GNOME Translation Update Tool
5 # Copyright (C) 2000 Free Software Foundation.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
12 # This script 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 library; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 # Author(s): Kenneth Christiansen
23 ## Release information
24 my $PROGRAM = "xml-i18n-update";
26 my $_PACKAGE = "xml-i18n-tools";
35 ## Scalars used by the option stuff
38 my $VERSION_ARG = "0";
41 my $HEADERS_ARG = "0";
42 my $MAINTAIN_ARG = "0";
47 my %po_files_by_lang = ();
50 "xml(\.in)*|". # .in is not required
52 "glade(\.in)*|". # .in is not required
62 my $PACKAGE = &find_package_name;
64 ## Always print as the first thing
67 ## Give error if script is run without an argument
69 print "${PROGRAM}: missing file arguments\n";
70 print "Try `${PROGRAM} --help' for more information.\n";
76 "help|h|?" => \$HELP_ARG,
77 "version|v" => \$VERSION_ARG,
78 "dist|d" => \$DIST_ARG,
80 "headers|s" => \$HEADERS_ARG,
81 "maintain|m" => \$MAINTAIN_ARG,
82 "report|r" => \$REPORT_ARG,
83 "verbose|x" => \$VERBOSE
87 ## Use the supplied arguments
89 ## This section will check for the different options.
91 sub split_on_argument {
107 } elsif ($HEADERS_ARG) {
111 } elsif ($MAINTAIN_ARG) {
114 } elsif ($REPORT_ARG) {
118 if ($LANG =~ /^-/){ ## not an option
134 print "Working, please wait..." unless $VERBOSE;
141 ## Report error if the language file supplied
142 ## to the command line is non-existent
148 sub determine_type($) {
153 if ($type =~ /\[type: (gettext\/[^\]].*)]/) {
156 elsif ($type =~ /(?:xml(\.in)*|ui|oaf(?:\.in)+|pong(?:\.in)+|etspec)$/) {
157 $gettext_type="gettext\/xml";
159 elsif ($type =~ /glade(\.in)*$/) {
160 $gettext_type="gettext\/glade";
162 elsif ($type =~ /(?:desktop(?:\.in)+|directory(?:\.in)+|soundlist(?:\.in)+)$/) {
163 $gettext_type="gettext\/ini";
165 elsif ($type =~ /keys(\.in)+$/) {
166 $gettext_type="gettext\/keys";
168 else { $gettext_type=""; }
170 return $gettext_type;
175 ## Print version information
176 print "${PROGRAM} (${_PACKAGE}) $VERSION\n";
177 print "Written by Kenneth Christiansen <kenneth\@gnome.org>, 2000.\n\n";
178 print "Copyright (C) 2000 Free Software Foundation, Inc.\n";
179 print "This is free software; see the source for copying conditions. There is NO\n";
180 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
186 ## Print usage information
187 print "Usage: ./${PROGRAM} [OPTIONS] ...LANGCODE\n";
188 print "Updates pot files and merge them with the translations.\n\n";
189 print " -H, --help shows this help page\n";
190 print " -P, --pot generate the pot file only\n";
191 print " -S, --headers generate the XML headerfiles in POTFILES.in\n";
192 print " -M, --maintain search for missing files in POTFILES.in\n";
193 print " -R, --report creates a status report for the module.\n";
194 print " -X, --verbose show lots of feedback\n";
195 print " -V, --version shows the version\n";
196 print "\nExamples of use:\n";
197 print "${PROGRAM} --pot just creates a new pot file from the source\n";
198 print "${PROGRAM} da created new pot file and updated the da.po file\n\n";
199 print "Report bugs to <kenneth\@gnome.org>.\n";
208 @buf_potfiles_ignore,
210 @buf_allfiles_sorted,
214 ## Search and find all translatable files
215 find sub { push @buf_i18n_plain, "$File::Find::name" if /\.(c|y|cc|c\+\+|h|gob)$/ }, "..";
216 find sub { push @buf_i18n_xml, "$File::Find::name" if /\.($xml_extension)$/ }, "..";
218 open(POTFILES, "POTFILES.in") || die "$PROGRAM: there's no POTFILES.in!!!\n";
219 @buf_potfiles = <POTFILES>;
221 print "Searching for missing translatable files...\n";
223 ## Check if we should ignore some found files, when
224 ## comparing with POTFILES.in
225 if (-s "POTFILES.skip"){
226 open FILE, "POTFILES.skip";
229 push @buf_potfiles_ignore, $_;
232 print "Found POTFILES.skip: Ignoring files...\n";
233 @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
236 foreach my $file (@buf_i18n_plain){
240 ## Remove the first 3 chars and add newline
241 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
247 foreach my $file (@buf_i18n_xml){
251 ## Remove the first 3 chars and add newline
252 push @buf_allfiles, unpack("x3 A*", $file) . "\n";
258 @buf_allfiles_sorted = sort (@buf_allfiles);
259 @buf_potfiles_sorted = sort (@buf_potfiles);
262 foreach (@buf_potfiles_sorted) {
268 foreach (@buf_allfiles_sorted){
269 if (!exists($in2{$_})){
274 ## Save file with information about the files missing
275 ## if any, and give information about this proceedier
277 open OUT, ">missing";
279 print "\nHere is the result:\n\n", @result, "\n";
280 print "The file \"missing\" has been placed in the current directory.\n";
281 print "Files supposed to be ignored should be placed in \"POTFILES.skip\"\n";
284 ## If there is nothing to complain about, notice the user
286 print "\nWell, it's all perfect! Congratulation!\n";
292 ## Handle invalid arguments
293 print "${PROGRAM}: invalid option -- $LANG\n";
294 print "Try `${PROGRAM} --help' for more information.\n";
300 my $XML_I18N_EXTRACT = `which xml-i18n-extract 2>/dev/null`;
301 chomp $XML_I18N_EXTRACT;
303 $XML_I18N_EXTRACT = $ENV{"XML_I18N_EXTRACT"} if $ENV{"XML_I18N_EXTRACT"};
305 ## Generate the .h header files, so we can allow glade and
306 ## xml translation support
307 if (! -s $XML_I18N_EXTRACT)
309 print "\n *** The xml-i18n-extract script wasn't found!"
310 ."\n *** Without this xml-i18n-update can not generate files.\n";
315 open FILE, "<POTFILES.in";
319 ## Find xml files in POTFILES.in and generate the
320 ## files with help from the xml-i18n-extract script
322 my $gettext_type=&determine_type($1);
324 if (/\.($xml_extension)$/ || /^\[/){
325 $_ =~ s/^\[[^\[].*]\s*//;
326 my $filename = "../$_";
329 system($XML_I18N_EXTRACT, "--update", "--type=$gettext_type", $filename);
331 system($XML_I18N_EXTRACT, "--update", "--type=$gettext_type", "--quiet", $filename);
341 ## Generate the potfiles from the POTFILES.in file
343 print "Building the $PACKAGE.pot...\n" if $VERBOSE;
345 move("POTFILES.in", "POTFILES.in.old");
347 open INFILE, "<POTFILES.in.old";
348 open OUTFILE, ">POTFILES.in";
350 s/\.($xml_extension)$/$&.h/;
351 s/^\[.*]\s*(.*)/$1.h/;
357 my $gettext_test ="test \! -f $PACKAGE\.po \|\| \( rm -f \.\/$PACKAGE\.pot "
358 ."&& mv $PACKAGE\.po \.\/$PACKAGE\.pot \)";
360 system("xgettext", "--default-domain\=$PACKAGE", "--directory\=\.\.",
361 "--add-comments", "--keyword\=\_", "--keyword\=N\_",
362 "--files-from\=\.\/POTFILES\.in");
364 system($gettext_test);
366 print "Wrote $PACKAGE.pot\n" if $VERBOSE;
368 move("POTFILES.in.old", "POTFILES.in");
370 print "Removing generated header (.h) files..." if $VERBOSE;
372 open FILE, "<POTFILES.in";
376 unlink "../$_.h" if /\.($xml_extension)$/;
379 print "done\n" if $VERBOSE;
390 if ($ARGV[0] ne "--dist" && $ARGV[0] ne "-D") {
391 print "Merging $LANG.po with $PACKAGE.pot..." if $VERBOSE;
394 &perform_merge($LANG);
395 ## Remove the "messages" trash file generated by gettext
403 copy("$LANG.po", "$LANG.po.old") || die "copy failed: $!";
406 system("msgmerge", "$LANG.po.old", "$PACKAGE.pot", "-o", "$LANG.po");
408 ## Remove the backup file
409 unlink "$LANG.po.old";
414 ## Report error if supplied language file is non-existing
415 print "$PROGRAM: sorry, $LANG.po does not exist!\n";
416 print "Try `$PROGRAM --help' for more information.\n";
422 my @po_files = glob("./*.po");
424 @languages = map (&po_file2lang, @po_files);
426 foreach my $lang (@languages) {
427 $po_files_by_lang{$lang} = shift (@po_files);
434 $tmp =~ s/^.*\/(.*)\.po$/$1/;
441 system("msgfmt", "--statistics", "$LANG.po");
451 foreach my $lang (@languages){
453 &perform_merge($lang);
456 print "\n\n * Current translation support in $PACKAGE \n\n";
458 foreach my $lang (@languages){
461 system("msgfmt", "--statistics", "$lang.po");
465 sub find_package_name
467 my $base_dirname = getcwd();
468 $base_dirname =~ s@.*/@@;
470 my ($conf_in, $src_dir);
472 if ($base_dirname eq "po") {
473 if (-f "../configure.in") {
474 $conf_in = "../configure.in";
478 open (IN, "<Makefile") || die "can't open Makefile: $!";
481 if (/^top_srcdir[ \t]*=/) {
483 # print "${src_dir}\n";
485 $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
486 # print "${src_dir}\n";
488 $conf_in = "$src_dir" . "/configure.in" . "\n";
492 $conf_in || die "Cannot find top_srcdir in Makefile."
497 local $/; # slurp mode
498 open (IN, "<$conf_in") || die "can't open $conf_in: $!";
502 if ($conf_source =~ /AM_INIT_AUTOMAKE\(([^,]*),(.*)/) {
503 my $package_name = $1;
504 if ($package_name =~ /^[\$](.*)/){
505 if ($conf_source =~ /$1=(.*)/) {
509 return $package_name;
513 print "$PROGRAM: Unable to determine package name.\n" .
514 "Make sure to run this script inside the po directory.\n";