2 # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4 -*-
5 # The Intltool Message Updater
7 # Copyright (C) 2000-2003 Free Software Foundation.
9 # Intltool is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # version 2 published by the Free Software Foundation.
13 # Intltool is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 # As a special exception to the GNU General Public License, if you
23 # distribute this file as part of a program that contains a
24 # configuration script generated by Autoconf, you may include it under
25 # the same distribution terms that you use for the rest of that program.
27 # Authors: Kenneth Christiansen <kenneth@gnu.org>
29 # Darin Adler <darin@bentspoon.com>
31 ## Release information
32 my $PROGRAM = "intltool-update";
33 my $VERSION = "@VERSION@";
34 my $PACKAGE = "@PACKAGE@";
43 ## Scalars used by the option stuff
52 my $GETTEXT_PACKAGE = "";
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)*|". # http://www.w3.org/XML/ (Note: .in is not required)
64 "ui|". # Bonobo specific - User Interface desc. files
66 "glade2?(?:\\.in)*|". # Glade specific - User Interface desc. files (Note: .in is not required)
67 "oaf(?:\\.in)+|". # DEPRECATED: Replaces by Bonobo .server files
69 "server(?:\\.in)+|". # Bonobo specific
70 "sheet(?:\\.in)+|". # ?
71 "schemas(?:\\.in)+|". # GConf specific
72 "gschema.xml|". # GLib schema (ie: GSettings) specific
73 "pong(?:\\.in)+|". # DEPRECATED: PONG is not used [by GNOME] any longer.
74 "kbd(?:\\.in)+|". # GOK specific.
75 "policy(?:\\.in)+"; # PolicyKit files
78 "icon(?:\\.in)+|". # http://www.freedesktop.org/Standards/icon-theme-spec
79 "desktop(?:\\.in)+|". # http://www.freedesktop.org/Standards/menu-spec
80 "caves(?:\\.in)+|". # GNOME Games specific
81 "directory(?:\\.in)+|". # http://www.freedesktop.org/Standards/menu-spec
82 "soundlist(?:\\.in)+|". # GNOME specific
83 "keys(?:\\.in)+|". # GNOME Mime database specific
84 "theme(?:\\.in)+|". # http://www.freedesktop.org/Standards/icon-theme-spec
85 "service(?:\\.in)+"; # DBus specific
88 "tlk(?:\\.in)+"; # Bioware Aurora Talk Table Format
90 my $buildin_gettext_support =
91 "c|y|cs|cc|cpp|c\\+\\+|h|hh|gob|py|scm(?:\\.in)*";
93 ## Always flush buffer when printing
96 ## Sometimes the source tree will be rooted somewhere else.
97 my $SRCDIR = $ENV{"srcdir"} || ".";
100 $POTFILES_in = "<$SRCDIR/POTFILES.in";
102 my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
107 "help" => \$HELP_ARG,
108 "version" => \$VERSION_ARG,
109 "dist|d" => \$DIST_ARG,
110 "pot|p" => \$POT_ARG,
111 "headers|s" => \$HEADERS_ARG,
112 "maintain|m" => \$MAINTAIN_ARG,
113 "report|r" => \$REPORT_ARG,
114 "verbose|x" => \$VERBOSE,
115 "gettext-package|g=s" => \$GETTEXT_PACKAGE,
116 "output-file|o=s" => \$OUTPUT_FILE,
117 ) or &Console_WriteError_InvalidOption;
119 &Console_Write_IntltoolHelp if $HELP_ARG;
120 &Console_Write_IntltoolVersion if $VERSION_ARG;
122 my $arg_count = ($DIST_ARG > 0)
125 + ($MAINTAIN_ARG > 0)
128 &Console_Write_IntltoolHelp if $arg_count > 1;
130 my $MODULE = $GETTEXT_PACKAGE || FindPackageName() || "unknown";
141 elsif ($MAINTAIN_ARG)
149 &Console_Write_CoverageReport;
151 elsif ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
155 ## Report error if the language file supplied
156 ## to the command line is non-existent
157 &Console_WriteError_NotExisting("$SRCDIR/$lang.po")
158 if ! -s "$SRCDIR/$lang.po";
162 print "Working, please wait..." if $VERBOSE;
166 &POFile_Update ($lang, $OUTPUT_FILE);
167 &Console_Write_TranslationStatus ($lang, $OUTPUT_FILE);
171 &Console_Write_IntltoolHelp;
178 sub Console_Write_IntltoolVersion
181 ${PROGRAM} (${PACKAGE}) $VERSION
182 Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.
184 Copyright (C) 2000-2003 Free Software Foundation, Inc.
185 This is free software; see the source for copying conditions. There is NO
186 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
191 sub Console_Write_IntltoolHelp
194 Usage: ${PROGRAM} [OPTION]... LANGCODE
195 Updates PO template files and merge them with the translations.
197 Mode of operation (only one is allowed):
198 -p, --pot generate the PO template only
199 -s, --headers generate the header files in POTFILES.in
200 -m, --maintain search for left out files from POTFILES.in
201 -r, --report display a status report for the module
202 -d, --dist merge LANGCODE.po with existing PO template
205 -g, --gettext-package=NAME override PO template name, useful with --pot
206 -o, --output-file=FILE write merged translation to FILE
207 -x, --verbose display lots of feedback
208 --help display this help and exit
209 --version output version information and exit
212 ${PROGRAM} --pot just create a new PO template
213 ${PROGRAM} xy create new PO template and merge xy.po with it
215 Report bugs to http://bugs.launchpad.net/intltool
223 my $ret = `echo "$str"`;
225 $ret =~ s/\n$//; # do we need the "s" flag?
230 sub POFile_DetermineType ($)
235 my $xml_regex = "(?:" . $xml_support . ")";
236 my $ini_regex = "(?:" . $ini_support . ")";
237 my $tlk_regex = "(?:" . $tlk_support . ")";
238 my $buildin_regex = "(?:" . $buildin_gettext_support . ")";
240 if ($type =~ /\[type: gettext\/([^\]].*)]/)
244 elsif ($type =~ /gschema.xml$/)
246 $gettext_type="gsettings";
248 elsif ($type =~ /schemas(\.in)+$/)
250 $gettext_type="schemas";
252 elsif ($type =~ /glade2?(\.in)*$/)
254 $gettext_type="glade";
256 elsif ($type =~ /scm(\.in)*$/)
258 $gettext_type="scheme";
260 elsif ($type =~ /keys(\.in)+$/)
262 $gettext_type="keys";
267 elsif ($type =~ /$xml_regex$/)
271 elsif ($type =~ /$ini_regex$/)
275 elsif ($type =~ /$tlk_regex$/)
279 elsif ($type =~ /$buildin_regex$/)
281 $gettext_type="buildin";
285 $gettext_type="unknown";
288 return "gettext\/$gettext_type";
291 sub TextFile_DetermineEncoding ($)
293 my $gettext_code="UTF-8"; # All files are UTF-8 by default
294 my $filetype=`file $_ | cut -d ' ' -f 2`;
298 if ($filetype =~ /^(ISO|UTF)/)
300 chomp ($gettext_code = $filetype);
302 elsif ($filetype =~ /^XML/)
304 $gettext_code="UTF-8"; # We asume that .glade and other .xml files are UTF-8
308 return $gettext_code;
311 sub isNotValidMissing
314 my $package_name = "";
316 $package_name = $varhash{"PACKAGE"} if (defined $varhash{"PACKAGE"});
317 $version = $varhash{"VERSION"} if (defined $varhash{"VERSION"});
319 return if $file =~ /^\{arch\}\/.*$/;
320 return if $file =~ /^$package_name-$version\/.*$/;
325 my ($file, @array) = @_;
328 foreach my $potfile (@array) {
329 delete $array[$i] if $potfile =~ m/$file/;
334 sub AddFileToListIfMissing
336 my ($file, $list) = @_;
339 if ($file =~ /^\.\.\//) {
340 $name_pattern = "x3 A*";
342 $name_pattern = "A*";
345 my $file_name = unpack($name_pattern, $file);
346 if (defined isNotValidMissing ($file_name)) {
347 ## Remove the first 3 chars if needed and add newline
348 push @$list, $file_name . "\n";
356 @buf_i18n_xml_unmarked,
359 @buf_potfiles_ignore,
361 @buf_allfiles_sorted,
362 @buf_potfiles_sorted,
363 @buf_potfiles_ignore_sorted
366 ## Search and find all translatable files
368 # Ignore hidden files
369 return if "$File::Find::name" =~ /\/\./;
370 push @buf_i18n_plain, "$File::Find::name" if /\.($buildin_gettext_support)$/;
371 push @buf_i18n_xml, "$File::Find::name" if /\.($xml_support)$/;
372 push @buf_i18n_ini, "$File::Find::name" if /\.($ini_support)$/;
373 push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/;
376 # Ignore hidden files
377 return if "$File::Find::name" =~ /\/\.[^.]/;
378 push @buf_i18n_plain, "$File::Find::name" if /\.($buildin_gettext_support)$/;
379 push @buf_i18n_xml, "$File::Find::name" if /\.($xml_support)$/;
380 push @buf_i18n_ini, "$File::Find::name" if /\.($ini_support)$/;
381 push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/;
382 }, "$SRCDIR/.." if "$SRCDIR" ne ".";
384 open POTFILES, $POTFILES_in or die "$PROGRAM: there's no POTFILES.in!\n";
385 @buf_potfiles = grep !/^(#|\s*$)/, <POTFILES>;
388 foreach (@buf_potfiles) {
392 print "Searching for missing translatable files...\n" if $VERBOSE;
394 ## Check if we should ignore some found files, when
395 ## comparing with POTFILES.in
396 foreach my $ignore ("POTFILES.skip", "POTFILES.ignore")
398 (-s "$SRCDIR/$ignore") or next;
400 if ("$ignore" eq "POTFILES.ignore")
402 print "The usage of POTFILES.ignore is deprecated. Please consider moving the\n".
403 "content of this file to POTFILES.skip.\n";
406 print "Found $ignore: Ignoring files...\n" if $VERBOSE;
407 open FILE, "<$SRCDIR/$ignore" or die "ERROR: Failed to open $SRCDIR/$ignore!\n";
412 next if (/^(#|\s*$)/);
414 my $skipdir = "../$_";
415 $skipdir = "$SRCDIR/../$_" if "$SRCDIR" ne ".";
423 push @dirignored, "$File::Find::name" if /\.($buildin_gettext_support)$/;
424 push @dirignored, "$File::Find::name" if /\.($xml_support)$/;
425 push @dirignored, "$File::Find::name" if /\.($ini_support)$/;
426 push @dirignored, "$File::Find::name" if /\.(schemas(\.in)+)$/;
428 foreach my $ignored (@dirignored)
430 $ignored =~ s/^$SRCDIR\///g;
431 $ignored =~ s/^..\///g;
432 $ignored =~ s/$/\n/g;
434 removeFromArray ($ignored, @buf_i18n_plain);
435 removeFromArray ($ignored, @buf_i18n_xml);
436 removeFromArray ($ignored, @buf_i18n_ini);
437 removeFromArray ($ignored, @buf_i18n_xml_unmarked);
438 push @buf_potfiles_ignore, $ignored;
442 removeFromArray ($_, @buf_i18n_plain);
443 removeFromArray ($_, @buf_i18n_xml);
444 removeFromArray ($_, @buf_i18n_ini);
445 removeFromArray ($_, @buf_i18n_xml_unmarked);
446 push @buf_potfiles_ignore, $_;
450 @buf_potfiles_ignore_sorted = sort (@buf_potfiles_ignore);
453 foreach my $file (@buf_i18n_plain)
458 my @multiline_quotes;
459 if ($file =~ /\.scm/) {
460 @multiline_quotes = ('"');
462 @multiline_quotes = ("'''", '"""');
468 if ($file =~ /\.scm/) {
469 # Strip single quotes from .scm files.
473 # Handle continued multi-line comment.
476 next unless s-.*\*/--;
480 # Handle continued multi-line string.
483 my $pattern = join '|', @multiline_quotes;
484 if (!s/.*$pattern//) {
491 # Handle continued macro.
494 $in_macro = 0 unless /\\$/;
498 # Handle start of macro (or any preprocessor directive).
501 $in_macro = 1 if /^([^\\]|\\.)*\\$/;
505 # Handle comments and quoted text.
506 while (m-(/\*|//|\'\'\'|\"\"\"|\'|\")-) # \' and \" keep emacs perl mode happy
517 elsif ($match eq "//")
521 elsif (grep($match, @multiline_quotes))
523 if (!s-$match(\\$match|[^$match])*$match-QUOTEDTEXT-g)
525 s-$match.*-QUOTEDTEXT-s;
531 s-$match(\\$match|[^$match])*$match-QUOTEDTEXT-g;
533 # Handle inline # comments.
534 s/^([^$match]+)\#.*/$1/;
538 warn "mismatched quotes at line $. in $file\n";
544 if (/\w\.GetString *\(QUOTEDTEXT/)
546 AddFileToListIfMissing($file, \@buf_allfiles);
550 ## C_ N_ NC_ Q_ and _ are the macros defined in gi8n.h
551 if (/(NC_|[NCQ]_|[^_]_|(^|$)[_]) *\(?QUOTEDTEXT/m)
553 AddFileToListIfMissing($file, \@buf_allfiles);
557 # Check for direct calls to the glib gettext wrappers
558 if (/g_d[np]?gettext[2]? *\(QUOTEDTEXT/)
560 AddFileToListIfMissing($file, \@buf_allfiles);
567 foreach my $file (@buf_i18n_xml)
573 # FIXME: share the pattern matching code with intltool-extract
574 if (/\s_[-A-Za-z0-9._:]+\s*=\s*\"([^"]+)\"/ || /<_[^>]+>/ || /translatable=\"yes\"/)
576 AddFileToListIfMissing($file, \@buf_allfiles);
583 foreach my $file (@buf_i18n_ini)
590 AddFileToListIfMissing($file, \@buf_allfiles);
597 foreach my $file (@buf_i18n_xml_unmarked)
599 AddFileToListIfMissing($file, \@buf_allfiles);
603 @buf_allfiles_sorted = sort (@buf_allfiles);
604 @buf_potfiles_sorted = sort (@buf_potfiles);
607 foreach (@buf_potfiles_sorted)
614 foreach (@buf_potfiles_ignore_sorted)
623 foreach (@buf_allfiles_sorted)
626 my $srcdir = $SRCDIR;
629 $dummy =~ s#^$srcdir/../##;
630 $dummy =~ s#^$srcdir/##;
631 $dummy =~ s#_build/##;
632 if (!exists($in2{$dummy}))
638 my @buf_potfiles_notexist;
640 foreach (@buf_potfiles_sorted)
642 chomp (my $dummy = $_);
643 if ("$dummy" ne "" and !(-f "$SRCDIR/../$dummy" or -f "../$dummy"))
645 push @buf_potfiles_notexist, $_;
649 ## Save file with information about the files missing
650 ## if any, and give information about this procedure.
651 if (@result + @buf_potfiles_notexist > 0)
655 print "\n" if $VERBOSE;
657 open OUT, ">missing";
660 warn "The following files contain translations and are currently not in use. Please\n".
661 "consider adding these to the POTFILES.in file, located in the po/ directory.\n\n";
662 print STDERR @result, "\n";
663 warn "If some of these files are left out on purpose then please add them to\n".
664 "POTFILES.skip instead of POTFILES.in. A file 'missing' containing this list\n".
665 "of left out files has been written in the current directory.\n";
666 warn "Please report to ". $varhash{"PACKAGE_BUGREPORT"} ."\n" if (defined $varhash{"PACKAGE_BUGREPORT"});
668 if (@buf_potfiles_notexist)
671 open OUT, ">notexist";
672 print OUT @buf_potfiles_notexist;
674 warn "\n" if ($VERBOSE or @result);
675 warn "The following files do not exist anymore:\n\n";
676 warn @buf_potfiles_notexist, "\n";
677 warn "Please remove them from POTFILES.in. A file 'notexist'\n".
678 "containing this list of absent files has been written in the current directory.\n";
679 warn "Please report to ". $varhash{"PACKAGE_BUGREPORT"} ."\n" if (defined $varhash{"PACKAGE_BUGREPORT"});
683 ## If there is nothing to complain about, notify the user
685 print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
689 sub Console_WriteError_InvalidOption
691 ## Handle invalid arguments
692 print STDERR "Try `${PROGRAM} --help' for more information.\n";
699 # If a file is executable (or exists on Windows),
700 # or when it returns 0 exit status.
702 ((-x $file) or ($^O eq 'MSWin32' and (-e $file))) or
703 (system("$file --version >$devnull") == 0));
710 # Check that we are using GNU gettext tools
711 if (isProgramInPath ($file))
713 my $version = `$file --version`;
714 return 1 if ($version =~ m/.*\(GNU .*\).*/);
721 my $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} || "intltool-extract";
723 ## Generate the .h header files, so we can allow glade and
724 ## xml translation support
725 if (! isProgramInPath ("$EXTRACT"))
727 print STDERR "\n *** The intltool-extract script wasn't found!"
728 ."\n *** Without it, intltool-update can not generate files.\n";
733 open (FILE, $POTFILES_in) or die "$PROGRAM: POTFILES.in not found.\n";
738 next if /^\[\s*encoding/;
740 ## Find xml files in POTFILES.in and generate the
741 ## files with help from the extract script
743 my $gettext_type= &POFile_DetermineType ($1);
745 if (/\.($xml_support|$ini_support|$tlk_support)$/ || /^\[/)
749 my @cmd = ($EXTRACT, "--update", "--type=$gettext_type",
752 unshift (@cmd, $^X) if ($^O eq 'MSWin32' && !($EXTRACT =~ /perl/));
754 push (@cmd, "--quiet") if (! $VERBOSE);
755 push (@cmd, "../$_");
765 # Generate .pot file from POTFILES.in
767 sub GeneratePOTemplate
769 my $XGETTEXT = $ENV{"XGETTEXT"} || "xgettext";
770 my $XGETTEXT_ARGS = $ENV{"XGETTEXT_ARGS"} || '';
773 if (! isGNUGettextTool ("$XGETTEXT"))
775 print STDERR " *** GNU xgettext is not found on this system!\n".
776 " *** Without it, intltool-update can not extract strings.\n";
780 print "Building $MODULE.pot...\n" if $VERBOSE;
782 open INFILE, $POTFILES_in;
783 unlink "POTFILES.in.temp";
784 open OUTFILE, ">POTFILES.in.temp" or die("Cannot open POTFILES.in.temp for writing");
786 my $gettext_support_nonascii = 0;
788 # checks for GNU gettext >= 0.12
789 my $dummy = `$XGETTEXT --version --from-code=UTF-8 >$devnull 2>$devnull`;
792 $gettext_support_nonascii = 1;
796 # require gnu gettext >= 0.12
797 die "$PROGRAM: GNU gettext >= 0.12 is required for intltool\n";
800 my $encoding = "UTF-8";
801 my $forced_gettext_code;
803 my $encoding_problem_is_reported = 0;
807 next if (/^#/ or /^\s*$/);
813 if (/^\[\s*encoding:\s*(.*)\s*\]/)
815 $forced_gettext_code=$1;
817 elsif (/\.($xml_support|$ini_support|$tlk_support)$/ || /^\[/)
820 print OUTFILE "../$_.h\n";
821 push @temp_headers, "../$_.h";
822 $gettext_code = &TextFile_DetermineEncoding ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
826 print OUTFILE "$SRCDIR/../$_\n";
827 $gettext_code = &TextFile_DetermineEncoding ("$SRCDIR/../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
830 next if (! $gettext_support_nonascii);
832 if (defined $forced_gettext_code)
834 $encoding=$forced_gettext_code;
836 elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
838 if ($encoding eq "ASCII")
840 $encoding=$gettext_code;
842 elsif ($gettext_code ne "ASCII")
844 # Only report once because the message is quite long
845 if (! $encoding_problem_is_reported)
847 print STDERR "WARNING: You should use the same file encoding for all your project files,\n".
848 " but $PROGRAM thinks that most of the source files are in\n".
849 " $encoding encoding, while \"$_\" is (likely) in\n".
850 " $gettext_code encoding. If you are sure that all translatable strings\n".
851 " are in same encoding (say UTF-8), please *prepend* the following\n".
852 " line to POTFILES.in:\n\n".
853 " [encoding: UTF-8]\n\n".
854 " and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .\n".
855 "(such warning message will only be reported once.)\n";
856 $encoding_problem_is_reported = 1;
865 unlink "$MODULE.pot";
866 my @xgettext_argument=("$XGETTEXT",
869 "--default-domain\=$MODULE",
870 "--flag\=g_strdup_printf:1:c-format",
871 "--flag\=g_string_printf:2:c-format",
872 "--flag\=g_string_append_printf:2:c-format",
873 "--flag\=g_error_new:3:c-format",
874 "--flag\=g_set_error:4:c-format",
875 "--flag\=g_markup_printf_escaped:1:c-format",
876 "--flag\=g_log:3:c-format",
877 "--flag\=g_print:1:c-format",
878 "--flag\=g_printerr:1:c-format",
879 "--flag\=g_printf:1:c-format",
880 "--flag\=g_fprintf:2:c-format",
881 "--flag\=g_sprintf:2:c-format",
882 "--flag\=g_snprintf:3:c-format",
883 "--flag\=g_scanner_error:2:c-format",
884 "--flag\=g_scanner_warn:2:c-format",
885 "--output\=$MODULE\.pot",
886 "--files-from\=\.\/POTFILES\.in\.temp");
887 my $XGETTEXT_KEYWORDS = &FindPOTKeywords;
888 push @xgettext_argument, $XGETTEXT_KEYWORDS;
889 my $MSGID_BUGS_ADDRESS = &FindMakevarsBugAddress;
890 push @xgettext_argument, "--msgid-bugs-address\=\"$MSGID_BUGS_ADDRESS\"" if $MSGID_BUGS_ADDRESS;
891 push @xgettext_argument, "--from-code\=$encoding" if ($gettext_support_nonascii);
892 push @xgettext_argument, $XGETTEXT_ARGS if $XGETTEXT_ARGS;
893 my $xgettext_command = join ' ', @xgettext_argument;
895 # intercept xgettext error message
896 print "Running $xgettext_command\n" if $VERBOSE;
897 my $xgettext_error_msg = `$xgettext_command 2>\&1`;
898 my $command_failed = $?;
900 unlink "POTFILES.in.temp";
902 print "Removing generated header (.h) files..." if $VERBOSE;
903 unlink foreach (@temp_headers);
904 print "done.\n" if $VERBOSE;
906 if (! $command_failed)
908 if (! -e "$MODULE.pot")
910 print "None of the files in POTFILES.in contain strings marked for translation.\n" if $VERBOSE;
914 print "Wrote $MODULE.pot\n" if $VERBOSE;
919 if ($xgettext_error_msg =~ /--from-code/)
921 my $errlocation = "unknown";
923 if ($xgettext_error_msg =~ /Non-ASCII string at (.*)\..*/)
927 print STDERR "ERROR: xgettext failed to generate PO tempalte file because the following \n".
928 " file contains strings marked for translation, not encoded in UTF-8. \n".
929 " Please ensure all strings marked for translation are UTF-8 encoded. \n\n".
934 print STDERR "$xgettext_error_msg";
935 if (-e "$MODULE.pot")
938 print STDERR "ERROR: xgettext failed but still managed to generate PO template file.\n".
939 " Please consult error message above if there is any.\n";
943 print STDERR "ERROR: xgettext failed to generate PO template file. Please consult\n".
944 " error message above if there is any.\n";
953 -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
955 my $MSGMERGE = $ENV{"MSGMERGE"} || "msgmerge";
956 my ($lang, $outfile) = @_;
958 if (! isGNUGettextTool ("$MSGMERGE"))
960 print STDERR " *** GNU msgmerge is not found on this system!\n".
961 " *** Without it, intltool-update can not extract strings.\n";
965 print "Merging $SRCDIR/$lang.po with $MODULE.pot..." if $VERBOSE;
967 my $infile = "$SRCDIR/$lang.po";
968 $outfile = "$SRCDIR/$lang.po" if ($outfile eq "");
970 # I think msgmerge won't overwrite old file if merge is not successful
971 system ("$MSGMERGE", "-o", $outfile, $infile, "$MODULE.pot");
974 sub Console_WriteError_NotExisting
978 ## Report error if supplied language file is non-existing
979 print STDERR "$PROGRAM: $file does not exist!\n";
980 print STDERR "Try '$PROGRAM --help' for more information.\n";
986 my @po_files = glob ("./*.po");
988 @languages = map (&POFile_GetLanguage, @po_files);
990 foreach my $lang (@languages)
992 $po_files_by_lang{$lang} = shift (@po_files);
996 sub POFile_GetLanguage ($)
998 s/^(.*\/)?(.+)\.po$/$2/;
1002 sub Console_Write_TranslationStatus
1004 my ($lang, $output_file) = @_;
1005 my $MSGFMT = $ENV{"MSGFMT"} || "msgfmt";
1007 if (! isGNUGettextTool ("$MSGFMT"))
1009 print STDERR " *** GNU msgfmt is not found on this system!\n".
1010 " *** Without it, intltool-update can not extract strings.\n";
1014 $output_file = "$SRCDIR/$lang.po" if ($output_file eq "");
1016 system ("$MSGFMT", "-o", "$devnull", "--verbose", $output_file);
1019 sub Console_Write_CoverageReport
1021 my $MSGFMT = $ENV{"MSGFMT"} || "msgfmt";
1023 if (! isGNUGettextTool ("$MSGFMT"))
1025 print STDERR " *** GNU msgfmt is not found on this system!\n".
1026 " *** Without it, intltool-update can not extract strings.\n";
1032 foreach my $lang (@languages)
1034 print STDERR "$lang: ";
1035 &POFile_Update ($lang, "");
1038 print STDERR "\n\n * Current translation support in $MODULE \n\n";
1040 foreach my $lang (@languages)
1042 print STDERR "$lang: ";
1043 system ("$MSGFMT", "-o", "$devnull", "--verbose", "$SRCDIR/$lang.po");
1047 sub SubstituteVariable
1051 # always need to rewind file whenever it has been accessed
1054 # cache each variable. varhash is global to we can add
1055 # variables elsewhere.
1060 ($varhash{$1} = $2) =~ s/^["'](.*)["']$/$1/;
1064 if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
1069 # Ignore recursive definitions of variables
1070 $sub = $varhash{$2} if defined $varhash{$2} and $varhash{$2} !~ /\${?$2}?/;
1072 return SubstituteVariable ("$untouched$sub$rest");
1075 # We're using Perl backticks ` and "echo -n" here in order to
1076 # expand any shell escapes (such as backticks themselves) in every variable
1077 return echo_n ($str);
1080 sub CONF_Handle_Open
1082 my $base_dirname = getcwd();
1083 $base_dirname =~ s@.*/@@;
1085 my ($conf_in, $src_dir);
1087 if ($base_dirname =~ /^po(-.+)?$/)
1091 my $makefile_source;
1094 open (IN, "<Makevars") || die "can't open Makevars: $!";
1098 if (/^top_builddir[ \t]*=/)
1101 $src_dir =~ s/^top_builddir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
1104 if (-f "$src_dir" . "/configure.ac") {
1105 $conf_in = "$src_dir" . "/configure.ac" . "\n";
1107 $conf_in = "$src_dir" . "/configure.in" . "\n";
1114 $conf_in || die "Cannot find top_builddir in Makevars.";
1116 elsif (-f "$SRCDIR/../configure.ac")
1118 $conf_in = "$SRCDIR/../configure.ac";
1120 elsif (-f "$SRCDIR/../configure.in")
1122 $conf_in = "$SRCDIR/../configure.in";
1126 my $makefile_source;
1129 open (IN, "<Makefile") || return;
1133 if (/^top_srcdir[ \t]*=/)
1136 $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
1139 $conf_in = "$src_dir" . "/configure.in" . "\n";
1146 $conf_in || die "Cannot find top_srcdir in Makefile.";
1149 open (CONF, "<$conf_in");
1153 print STDERR "$PROGRAM: Unable to proceed.\n" .
1154 "Make sure to run this script inside the po directory.\n";
1162 my $domain = &FindMakevarsDomain;
1163 my $name = $domain || "untitled";
1170 open (IN, "<&CONF") || return $name;
1172 local $/; # slurp mode
1173 $conf_source = <IN>;
1177 # priority for getting package name:
1178 # 1. GETTEXT_PACKAGE
1179 # 2. first argument of AC_INIT (with >= 2 arguments)
1180 # 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)
1182 # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m
1183 # the \s makes this not work, why?
1184 if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
1186 ($name, $version) = ($1, $2);
1187 $name =~ s/[\[\]\s]//g;
1188 $version =~ s/[\[\]\s]//g;
1190 $version =~ s/\(+$//g;
1192 $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
1193 $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
1194 $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
1195 $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
1198 if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)[,]?([^,\)]+)?/m)
1200 ($name, $version) = ($1, $2);
1201 $bugurl = $3 if (defined $3);
1204 # FIXME: We should do this in a more generic way that works for all vars
1205 if ($version =~ /m4_esyscmd\([\[]?([^\)\]]+)/)
1207 my $cwd = getcwd ();
1208 chdir ("$SRCDIR/..");
1214 $name =~ s/[\[\]\s]//g;
1215 $version =~ s/[\[\]\s]//g;
1216 $bugurl =~ s/[\[\]\s]//g if (defined $bugurl);
1218 $version =~ s/\(+$//g;
1219 $bugurl =~ s/\(+$//g if (defined $bugurl);
1221 $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
1222 $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
1223 $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
1224 $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
1225 $varhash{"PACKAGE_BUGREPORT"} = $bugurl if (defined $bugurl and not $bugurl =~ /\${?\w+}?/);
1228 # \s makes this not work, why?
1229 $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
1231 # m4 macros AC_PACKAGE_NAME, AC_PACKAGE_VERSION etc. have same value
1232 # as corresponding $PACKAGE_NAME, $PACKAGE_VERSION etc. shell variables.
1233 $name =~ s/\bAC_PACKAGE_/\$PACKAGE_/g;
1235 $name = $domain if $domain;
1237 $name = SubstituteVariable ($name);
1238 $name =~ s/^["'](.*)["']$/$1/;
1240 return $name if $name;
1247 my $keywords = "--keyword=_ --keyword=N_ --keyword=C_:1c,2 --keyword=NC_:1c,2 --keyword=Q_ --keyword=g_dgettext:2 --keyword=g_dngettext:2,3 --keyword=g_dpgettext:2 --keyword=g_dpgettext2=2c,3";
1248 my $varname = "XGETTEXT_OPTIONS";
1251 open (IN, "<Makevars") || (open(IN, "<Makefile.in.in") && ($varname = "XGETTEXT_KEYWORDS")) || return $keywords;
1253 local $/; # slurp mode
1254 $make_source = <IN>;
1258 # unwrap lines split with a trailing \
1259 $make_source =~ s/\\ $ \n/ /mxg;
1260 $keywords = $1 if $make_source =~ /^$varname[ ]*=\[?([^\n\]]+)/m;
1265 sub FindMakevarsDomain
1269 my $makevars_source; {
1271 open (IN, "<Makevars") || return $domain;
1273 local $/; # slurp mode
1274 $makevars_source = <IN>;
1278 $domain = $1 if $makevars_source =~ /^DOMAIN[ ]*=\[?([^\n\]\$]+)/m;
1279 $domain =~ s/^\s+//;
1280 $domain =~ s/\s+$//;
1285 sub FindMakevarsBugAddress
1289 my $makevars_source; {
1291 open (IN, "<Makevars") || return undef;
1293 local $/; # slurp mode
1294 $makevars_source = <IN>;
1298 $address = $1 if $makevars_source =~ /^MSGID_BUGS_ADDRESS[ ]*=\[?([^\n\]\$]+)/m;
1299 $address =~ s/^\s+//;
1300 $address =~ s/\s+$//;