4
# The Intltool Message Updater
6
# Copyright (C) 2000-2003 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";
32
my $VERSION = "0.27.1";
33
my $PACKAGE = "intltool";
42
## Scalars used by the option stuff
51
my $GETTEXT_PACKAGE = "";
56
my %po_files_by_lang = ();
58
# Regular expressions to categorize file types.
59
# FIXME: Please check if the following is correct
62
"xml(\.in)*|". # .in is not required
65
"glade2?(\.in)*|". # .in is not required
66
"scm(\.in)*|". # .in is not required
82
## Always flush buffer when printing
89
"version" => \$VERSION_ARG,
90
"dist|d" => \$DIST_ARG,
92
"headers|s" => \$HEADERS_ARG,
93
"maintain|m" => \$MAINTAIN_ARG,
94
"report|r" => \$REPORT_ARG,
95
"verbose|x" => \$VERBOSE,
96
"gettext-package|g=s" => \$GETTEXT_PACKAGE,
97
"output-file|o=s" => \$OUTPUT_FILE,
98
) or &print_error_invalid_option;
100
&print_help if $HELP_ARG;
101
&print_version if $VERSION_ARG;
103
my $arg_count = ($DIST_ARG > 0)
106
+ ($MAINTAIN_ARG > 0)
109
&print_help if $arg_count > 1;
111
# --version and --help don't require a module name
112
my $MODULE = $GETTEXT_PACKAGE || &find_package_name;
117
&generate_po_template;
123
elsif ($MAINTAIN_ARG)
130
&generate_po_template;
133
elsif ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
137
## Report error if the language file supplied
138
## to the command line is non-existent
139
&print_error_not_existing("$lang.po") if ! -s "$lang.po";
143
print "Working, please wait..." if $VERBOSE;
145
&generate_po_template;
147
&update_po_file ($lang, $OUTPUT_FILE);
148
&print_status ($lang, $OUTPUT_FILE);
162
${PROGRAM} (${PACKAGE}) $VERSION
163
Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.
165
Copyright (C) 2000-2003 Free Software Foundation, Inc.
166
This is free software; see the source for copying conditions. There is NO
167
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
175
Usage: ${PROGRAM} [OPTION]... LANGCODE
176
Updates PO template files and merge them with the translations.
178
Mode of operation (only one is allowed):
179
-p, --pot generate the PO template only
180
-s, --headers generate the header files in POTFILES.in
181
-m, --maintain search for left out files from POTFILES.in
182
-r, --report display a status report for the module
183
-d, --dist merge LANGCODE.po with existing PO template
186
-g, --gettext-package=NAME override PO template name, useful with --pot
187
-o, --output-file=FILE write merged translation to FILE
188
-x, --verbose display lots of feedback
189
--help display this help and exit
190
--version output version information and exit
193
${PROGRAM} --pot just create a new PO template
194
${PROGRAM} xy create new PO template and merge xy.po with it
196
Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
197
or send email to <xml-i18n-tools\@gnome.org>.
202
sub determine_type ($)
207
# FIXME: Use $xml_extension, and maybe do all this even nicer
209
"(?:xml(\.in)*|ui|lang|oaf(?:\.in)+|server(?:\.in)+|sheet(?:\.in)+|".
210
"pong(?:\.in)+|etspec|schemas(?:\.in)+)";
212
"(?:desktop(?:\.in)+|theme(?:\.in)+|caves(?:\.in)+|directory(?:\.in)+|".
213
"soundlist(?:\.in)+)";
215
if ($type =~ /\[type: gettext\/([^\]].*)]/)
219
elsif ($type =~ /schemas(\.in)+$/)
221
$gettext_type="schemas";
223
elsif ($type =~ /$xml_regex$/)
227
elsif ($type =~ /glade2?(\.in)*$/)
229
$gettext_type="glade";
231
elsif ($type =~ /$ini_regex$/)
235
elsif ($type =~ /scm(\.in)*$/)
237
$gettext_type="scheme";
239
elsif ($type =~ /keys(\.in)+$/)
241
$gettext_type="keys";
248
return "gettext\/$gettext_type";
251
sub determine_code ($)
253
my $gettext_code="ASCII"; # All files are ASCII by default
254
my $filetype=`file $_ | cut -d ' ' -f 2`;
258
if ($filetype =~ /^(ISO|UTF)/)
260
chomp ($gettext_code = $filetype);
262
elsif ($filetype =~ /^XML/)
264
$gettext_code="UTF-8"; # We asume that .glade and other .xml files are UTF-8
268
return $gettext_code;
272
sub find_leftout_files
276
@buf_i18n_xml_unmarked,
279
@buf_potfiles_ignore,
281
@buf_allfiles_sorted,
285
## Search and find all translatable files
287
push @buf_i18n_plain, "$File::Find::name" if /\.(c|y|cc|cpp|c\+\+|h|gob)$/;
288
push @buf_i18n_xml, "$File::Find::name" if /\.($xml_extension)$/;
289
push @buf_i18n_ini, "$File::Find::name" if /\.($ini_extension)$/;
290
push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/;
294
open POTFILES, "<POTFILES.in" or die "$PROGRAM: there's no POTFILES.in!\n";
295
@buf_potfiles = grep !/^(#|\s*$)/, <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")
308
(-s $ignore) or next;
310
if ("$ignore" eq "POTFILES.ignore")
312
print "The usage of POTFILES.ignore is deprecated. Please consider moving the\n".
313
"content of this file to POTFILES.skip.\n";
316
print "Found $ignore: Ignoring files...\n" if $VERBOSE;
317
open FILE, "<$ignore" or die "ERROR: Failed to open $ignore!\n";
321
push @buf_potfiles_ignore, $_ unless /^(#|\s*$)/;
325
@buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
328
foreach my $file (@buf_i18n_plain)
336
# Handle continued multi-line comment.
339
next unless s-.*\*/--;
343
# Handle continued macro.
346
$in_macro = 0 unless /\\$/;
350
# Handle start of macro (or any preprocessor directive).
353
$in_macro = 1 if /^([^\\]|\\.)*\\$/;
357
# Handle comments and quoted text.
358
while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
369
elsif ($match eq "//")
375
if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
377
warn "mismatched quotes at line $. in $file\n";
385
## Remove the first 3 chars and add newline
386
push @buf_allfiles, unpack("x3 A*", $file) . "\n";
393
foreach my $file (@buf_i18n_xml)
399
# FIXME: share the pattern matching code with intltool-extract
400
if (/\s_(.*)=\"/ || /<_[^>]+>/ || /translatable=\"yes\"/)
402
push @buf_allfiles, unpack("x3 A*", $file) . "\n";
409
foreach my $file (@buf_i18n_ini)
416
push @buf_allfiles, unpack("x3 A*", $file) . "\n";
423
foreach my $file (@buf_i18n_xml_unmarked)
425
push @buf_allfiles, unpack("x3 A*", $file) . "\n";
429
@buf_allfiles_sorted = sort (@buf_allfiles);
430
@buf_potfiles_sorted = sort (@buf_potfiles);
433
foreach (@buf_potfiles_sorted)
440
foreach (@buf_allfiles_sorted)
442
if (!exists($in2{$_}))
448
my @buf_potfiles_notexist;
450
foreach (@buf_potfiles_sorted)
452
chomp (my $dummy = $_);
453
if ("$dummy" ne "" and ! -f "../$dummy")
455
push @buf_potfiles_notexist, $_;
459
## Save file with information about the files missing
460
## if any, and give information about this procedure.
461
if (@result + @buf_potfiles_notexist > 0)
465
print "\n" if $VERBOSE;
467
open OUT, ">missing";
470
warn "\e[1mThe following files contain translations and are currently not in use. Please\e[0m\n".
471
"\e[1mconsider adding these to the POTFILES.in file, located in the po/ directory.\e[0m\n\n";
472
print STDERR @result, "\n";
473
warn "If some of these files are left out on purpose then please add them to\n".
474
"POTFILES.skip instead of POTFILES.in. A file \e[1m'missing'\e[0m containing this list\n".
475
"of left out files has been written in the current directory.\n";
477
if (@buf_potfiles_notexist)
480
open OUT, ">notexist";
481
print OUT @buf_potfiles_notexist;
483
warn "\n" if ($VERBOSE or @result);
484
warn "\e[1mThe following files do not exist anymore:\e[0m\n\n";
485
warn @buf_potfiles_notexist, "\n";
486
warn "Please remove them from POTFILES.in or POTFILES.skip. A file \e[1m'notexist'\e[0m\n".
487
"containing this list of absent files has been written in the current directory.\n";
491
## If there is nothing to complain about, notify the user
493
print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
497
sub print_error_invalid_option
499
## Handle invalid arguments
500
print STDERR "Try `${PROGRAM} --help' for more information.\n";
506
my $EXTRACT = `which intltool-extract 2>/dev/null`;
509
$EXTRACT = $ENV{"INTLTOOL_EXTRACT"} if $ENV{"INTLTOOL_EXTRACT"};
511
## Generate the .h header files, so we can allow glade and
512
## xml translation support
515
print STDERR "\n *** The intltool-extract script wasn't found!"
516
."\n *** Without it, intltool-update can not generate files.\n";
521
open (FILE, "<POTFILES.in") or die "$PROGRAM: POTFILES.in not found.\n";
526
next if /^\[\s*encoding/;
528
## Find xml files in POTFILES.in and generate the
529
## files with help from the extract script
531
my $gettext_type= &determine_type ($1);
533
if (/\.($xml_extension|$ini_extension)$/ || /^\[/)
537
my $filename = "../$_";
541
system ($EXTRACT, "--update",
542
"--type=$gettext_type", $filename);
546
system ($EXTRACT, "--update", "--type=$gettext_type",
547
"--quiet", $filename);
556
# Generate .pot file from POTFILES.in
558
sub generate_po_template
560
my $XGETTEXT = `which xgettext 2>/dev/null`;
563
$XGETTEXT = $ENV{"XGETTEXT"} if $ENV{"XGETTEXT"};
567
print STDERR " *** xgettext is not found on this system!\n".
568
" *** Without it, intltool-update can not extract strings.\n";
572
print "Building $MODULE.pot...\n" if $VERBOSE;
574
open INFILE, "<POTFILES.in";
575
unlink "POTFILES.in.temp";
576
open OUTFILE, ">POTFILES.in.temp";
578
my $gettext_support_nonascii = 0;
580
# checks for GNU gettext >= 0.12
581
# don't use argument list, since shell interpretation is desired here
582
if (system("$XGETTEXT --version --from-code=UTF-8 >&/dev/null") == 0)
584
$gettext_support_nonascii = 1;
588
# urge everybody to upgrade gettext
589
print STDERR "WARNING: This version of gettext does not support extracting non-ASCII\n".
590
" strings. That means you should install a version of gettext\n".
591
" that supports non-ASCII strings (such as GNU gettext >= 0.12),\n".
592
" or have to let non-ASCII strings untranslated.\n";
595
my $encoding = "ASCII";
596
my $forced_gettext_code;
598
my $encoding_problem_is_reported = 0;
602
next if (/^#/ or /^\s*$/);
608
if (/^\[\s*encoding:\s*(.*)\s*\]/)
610
$forced_gettext_code=$1;
612
elsif (/\.($xml_extension|$ini_extension)$/ || /^\[/)
615
print OUTFILE "$_.h\n";
616
push @temp_headers, "../$_.h";
617
$gettext_code = &determine_code ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
621
print OUTFILE "$_\n";
622
$gettext_code = &determine_code ("../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
625
next if (! $gettext_support_nonascii);
627
if (defined $forced_gettext_code)
629
$encoding=$forced_gettext_code;
631
elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
633
if ($encoding eq "ASCII")
635
$encoding=$gettext_code;
637
elsif ($gettext_code ne "ASCII")
639
# Only report once because the message is quite long
640
if (! $encoding_problem_is_reported)
642
print STDERR "WARNING: You should use the same file encoding for all your project files,\n".
643
" but $PROGRAM thinks that most of the source files are in\n".
644
" $encoding encoding, while \"$_\" is (likely) in\n".
645
" $gettext_code encoding. If you are sure that all translatable strings\n".
646
" are in same encoding (say UTF-8), please \e[1m*prepend*\e[0m the following\n".
647
" line to POTFILES.in:\n\n".
648
" [encoding: UTF-8]\n\n".
649
" and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .\n".
650
"(such warning message will only be reported once.)\n";
651
$encoding_problem_is_reported = 1;
660
unlink "$MODULE.pot";
661
my @xgettext_argument=("$XGETTEXT",
667
"--output\=$MODULE\.pot",
668
"--files-from\=\.\/POTFILES\.in\.temp");
669
push @xgettext_argument, "--from-code\=$encoding" if ($gettext_support_nonascii);
670
my $xgettext_command = join ' ', @xgettext_argument;
672
# intercept xgettext error message
673
my $xgettext_error_msg = `$xgettext_command 2>\&1`;
674
my $command_failed = $?;
676
unlink "POTFILES.in.temp";
678
print "Removing generated header (.h) files..." if $VERBOSE;
679
unlink foreach (@temp_headers);
680
print "done.\n" if $VERBOSE;
682
if (! $command_failed)
684
if (! -e "$MODULE.pot")
686
print "None of the files in POTFILES.in contain strings marked for translation.\n" if $VERBOSE;
690
print "Wrote $MODULE.pot\n" if $VERBOSE;
695
if ($xgettext_error_msg =~ /--from-code/)
697
# replace non-ASCII error message with a more useful one.
698
print STDERR "ERROR: xgettext failed to generate PO template file because there is non-ASCII\n".
699
" string marked for translation. Please make sure that all strings marked\n".
700
" for translation are in uniform encoding (say UTF-8), then \e[1m*prepend*\e[0m the\n".
701
" following line to POTFILES.in and rerun $PROGRAM:\n\n".
702
" [encoding: UTF-8]\n\n";
706
print STDERR "$xgettext_error_msg";
707
if (-e "$MODULE.pot")
710
print STDERR "ERROR: xgettext failed but still managed to generate PO template file.\n".
711
" Please consult error message above if there is any.\n";
715
print STDERR "ERROR: xgettext failed to generate PO template file. Please consult\n".
716
" error message above if there is any.\n";
725
-f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
727
my ($lang, $outfile) = @_;
729
print "Merging $lang.po with $MODULE.pot..." if $VERBOSE;
731
my $infile = "$lang.po";
732
$outfile = "$lang.po" if ($outfile eq "");
734
# I think msgmerge won't overwrite old file if merge is not successful
735
system ("msgmerge", "-o", $outfile, $infile, "$MODULE.pot");
738
sub print_error_not_existing
742
## Report error if supplied language file is non-existing
743
print STDERR "$PROGRAM: $file does not exist!\n";
744
print STDERR "Try '$PROGRAM --help' for more information.\n";
750
my @po_files = glob ("./*.po");
752
@languages = map (&po_file2lang, @po_files);
754
foreach my $lang (@languages)
756
$po_files_by_lang{$lang} = shift (@po_files);
762
s/^(.*\/)?(.+)\.po$/$2/;
768
my ($lang, $output_file) = @_;
770
$output_file = "$lang.po" if ($output_file eq "");
772
system ("msgfmt", "-o", "/dev/null", "--statistics", $output_file);
779
foreach my $lang (@languages)
782
&update_po_file ($lang, "");
785
print "\n\n * Current translation support in $MODULE \n\n";
787
foreach my $lang (@languages)
790
system ("msgfmt", "-o", "/dev/null", "--statistics", "$lang.po");
798
# always need to rewind file whenever it has been accessed
801
# cache each variable. varhash is global to we can add
802
# variables elsewhere.
811
if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
815
my $sub = $varhash{$2};
817
return substitute_var ("$untouched$sub$rest");
824
my $base_dirname = getcwd();
825
$base_dirname =~ s@.*/@@;
827
my ($conf_in, $src_dir);
829
if ($base_dirname =~ /^po(-.+)?$/)
831
if (-f "../configure.ac")
833
$conf_in = "../configure.ac";
835
elsif (-f "../configure.in")
837
$conf_in = "../configure.in";
844
open IN, "<Makefile" || die "can't open Makefile: $!";
848
if (/^top_srcdir[ \t]*=/)
851
$src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
854
$conf_in = "$src_dir" . "/configure.in" . "\n";
861
$conf_in || die "Cannot find top_srcdir in Makefile.";
864
open (CONF, "<$conf_in") || die "can't open $conf_in: $!";
868
print STDERR "$PROGRAM: Unable to proceed.\n" .
869
"Make sure to run this script inside the po directory.\n";
874
sub find_package_name
880
open (IN, "<&CONF") || die "can't open configure.in/configure.ac: $!";
882
local $/; # slurp mode
887
my $name = "untitled";
890
# priority for getting package name:
892
# 2. first argument of AC_INIT (with >= 2 arguments)
893
# 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)
895
# /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m
896
# the \s makes this not work, why?
897
if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
899
($name, $version) = ($1, $2);
900
$name =~ s/[\[\]\s]//g;
901
($varhash{"AC_PACKAGE_VERSION"} = $version) =~ s/[\[\]\s]//g;
904
if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)/m)
906
($name, $version) = ($1, $2);
907
$name=~ s/[\[\]\s]//g;
908
$varhash{"AC_PACKAGE_NAME"} = $name;
909
($varhash{"AC_PACKAGE_VERSION"} = $version) =~ s/[\[\]\s]//g;
912
# \s makes this not work, why?
913
$name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
915
# prepend '$' to auto* internal variables, usually they are
916
# used in configure.in/ac without the '$'
917
$name =~ s/AC_/\$AC_/g;
918
$name =~ s/\$\$/\$/g;
920
$name = substitute_var ($name);
922
return $name if $name;