intltool-update.in
Upload User: shyika
Upload Date: 2017-11-25
Package Size: 1227k
Code Size: 28k
Category:

Video Capture

Development Platform:

Unix_Linux

  1. #!@INTLTOOL_PERL@ -w
  2. # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
  3. #
  4. #  The Intltool Message Updater
  5. #
  6. #  Copyright (C) 2000-2003 Free Software Foundation.
  7. #
  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.
  11. #
  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.
  16. #
  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.
  20. #
  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.
  25. #
  26. #  Authors: Kenneth Christiansen <kenneth@gnu.org>
  27. #           Maciej Stachowiak
  28. #           Darin Adler <darin@bentspoon.com>
  29. ## Release information
  30. my $PROGRAM = "intltool-update";
  31. my $VERSION = "0.35.5";
  32. my $PACKAGE = "intltool";
  33. ## Loaded modules
  34. use strict;
  35. use Getopt::Long;
  36. use Cwd;
  37. use File::Copy;
  38. use File::Find;
  39. ## Scalars used by the option stuff
  40. my $HELP_ARG     = 0;
  41. my $VERSION_ARG    = 0;
  42. my $DIST_ARG    = 0;
  43. my $POT_ARG    = 0;
  44. my $HEADERS_ARG    = 0;
  45. my $MAINTAIN_ARG   = 0;
  46. my $REPORT_ARG     = 0;
  47. my $VERBOSE    = 0;
  48. my $GETTEXT_PACKAGE = "";
  49. my $OUTPUT_FILE    = "";
  50. my @languages;
  51. my %varhash = ();
  52. my %po_files_by_lang = ();
  53. # Regular expressions to categorize file types.
  54. # FIXME: Please check if the following is correct
  55. my $xml_support =
  56. "xml(?:\.in)*|". # http://www.w3.org/XML/ (Note: .in is not required)
  57. "ui|". # Bonobo specific - User Interface desc. files
  58. "lang|". # ?
  59. "glade2?(?:\.in)*|". # Glade specific - User Interface desc. files (Note: .in is not required)
  60. "scm(?:\.in)*|". # ? (Note: .in is not required)
  61. "oaf(?:\.in)+|". # DEPRECATED: Replaces by Bonobo .server files 
  62. "etspec|". # ?
  63. "server(?:\.in)+|". # Bonobo specific
  64. "sheet(?:\.in)+|". # ?
  65. "schemas(?:\.in)+|". # GConf specific
  66. "pong(?:\.in)+|". # DEPRECATED: PONG is not used [by GNOME] any longer.
  67. "kbd(?:\.in)+"; # GOK specific. 
  68. my $ini_support =
  69. "icon(?:\.in)+|". # http://www.freedesktop.org/Standards/icon-theme-spec
  70. "desktop(?:\.in)+|". # http://www.freedesktop.org/Standards/menu-spec
  71. "caves(?:\.in)+|". # GNOME Games specific
  72. "directory(?:\.in)+|". # http://www.freedesktop.org/Standards/menu-spec
  73. "soundlist(?:\.in)+|". # GNOME specific
  74. "keys(?:\.in)+|". # GNOME Mime database specific
  75. "theme(?:\.in)+|". # http://www.freedesktop.org/Standards/icon-theme-spec
  76. "service(?:\.in)+";    # DBus specific
  77. my $buildin_gettext_support = 
  78. "c|y|cs|cc|cpp|c\+\+|h|hh|gob|py";
  79. ## Always flush buffer when printing
  80. $| = 1;
  81. ## Sometimes the source tree will be rooted somewhere else.
  82. my $SRCDIR = $ENV{"srcdir"} || ".";
  83. my $POTFILES_in;
  84. $POTFILES_in = "<$SRCDIR/POTFILES.in";
  85. my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
  86. ## Handle options
  87. GetOptions 
  88. (
  89.  "help"         => $HELP_ARG,
  90.  "version"         => $VERSION_ARG,
  91.  "dist|d"        => $DIST_ARG,
  92.  "pot|p"        => $POT_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 &Console_WriteError_InvalidOption;
  100. &Console_Write_IntltoolHelp if $HELP_ARG;
  101. &Console_Write_IntltoolVersion if $VERSION_ARG;
  102. my $arg_count = ($DIST_ARG > 0)
  103.     + ($POT_ARG > 0)
  104.     + ($HEADERS_ARG > 0)
  105.     + ($MAINTAIN_ARG > 0)
  106.     + ($REPORT_ARG > 0);
  107. &Console_Write_IntltoolHelp if $arg_count > 1;
  108. my $PKGNAME = FindPackageName ();
  109. # --version and --help don't require a module name
  110. my $MODULE = $GETTEXT_PACKAGE || $PKGNAME || "unknown";
  111. if ($POT_ARG)
  112. {
  113.     &GenerateHeaders;
  114.     &GeneratePOTemplate;
  115. }
  116. elsif ($HEADERS_ARG)
  117. {
  118.     &GenerateHeaders;
  119. }
  120. elsif ($MAINTAIN_ARG)
  121. {
  122.     &FindLeftoutFiles;
  123. }
  124. elsif ($REPORT_ARG)
  125. {
  126.     &GenerateHeaders;
  127.     &GeneratePOTemplate;
  128.     &Console_Write_CoverageReport;
  129. }
  130. elsif ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
  131. {
  132.     my $lang = $ARGV[0];
  133.     ## Report error if the language file supplied
  134.     ## to the command line is non-existent
  135.     &Console_WriteError_NotExisting("$SRCDIR/$lang.po")
  136.         if ! -s "$SRCDIR/$lang.po";
  137.     if (!$DIST_ARG)
  138.     {
  139. print "Working, please wait..." if $VERBOSE;
  140. &GenerateHeaders;
  141. &GeneratePOTemplate;
  142.     }
  143.     &POFile_Update ($lang, $OUTPUT_FILE);
  144.     &Console_Write_TranslationStatus ($lang, $OUTPUT_FILE);
  145. else 
  146. {
  147.     &Console_Write_IntltoolHelp;
  148. }
  149. exit;
  150. #########
  151. sub Console_Write_IntltoolVersion
  152. {
  153.     print <<_EOF_;
  154. ${PROGRAM} (${PACKAGE}) $VERSION
  155. Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.
  156. Copyright (C) 2000-2003 Free Software Foundation, Inc.
  157. This is free software; see the source for copying conditions.  There is NO
  158. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  159. _EOF_
  160.     exit;
  161. }
  162. sub Console_Write_IntltoolHelp
  163. {
  164.     print <<_EOF_;
  165. Usage: ${PROGRAM} [OPTION]... LANGCODE
  166. Updates PO template files and merge them with the translations.
  167. Mode of operation (only one is allowed):
  168.   -p, --pot                   generate the PO template only
  169.   -s, --headers               generate the header files in POTFILES.in
  170.   -m, --maintain              search for left out files from POTFILES.in
  171.   -r, --report                display a status report for the module
  172.   -d, --dist                  merge LANGCODE.po with existing PO template
  173. Extra options:
  174.   -g, --gettext-package=NAME  override PO template name, useful with --pot
  175.   -o, --output-file=FILE      write merged translation to FILE
  176.   -x, --verbose               display lots of feedback
  177.       --help                  display this help and exit
  178.       --version               output version information and exit
  179. Examples of use:
  180. ${PROGRAM} --pot    just create a new PO template
  181. ${PROGRAM} xy       create new PO template and merge xy.po with it
  182. Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
  183. or send email to <xml-i18n-tools@gnome.org>.
  184. _EOF_
  185.     exit;
  186. }
  187. sub echo_n
  188. {
  189.     my $str = shift;
  190.     my $ret = `echo "$str"`;
  191.     $ret =~ s/n$//; # do we need the "s" flag?
  192.     return $ret;
  193. }
  194. sub POFile_DetermineType ($) 
  195. {
  196.    my $type = $_;
  197.    my $gettext_type;
  198.    my $xml_regex     = "(?:" . $xml_support . ")";
  199.    my $ini_regex     = "(?:" . $ini_support . ")";
  200.    my $buildin_regex = "(?:" . $buildin_gettext_support . ")";
  201.    if ($type =~ /[type: gettext/([^]].*)]/) 
  202.    {
  203. $gettext_type=$1;
  204.    }
  205.    elsif ($type =~ /schemas(.in)+$/) 
  206.    {
  207. $gettext_type="schemas";
  208.    }
  209.    elsif ($type =~ /glade2?(.in)*$/) 
  210.    {
  211.        $gettext_type="glade";
  212.    }
  213.    elsif ($type =~ /scm(.in)*$/) 
  214.    {
  215.        $gettext_type="scheme";
  216.    }
  217.    elsif ($type =~ /keys(.in)+$/) 
  218.    {
  219.        $gettext_type="keys";
  220.    }
  221.    # bucket types
  222.    elsif ($type =~ /$xml_regex$/) 
  223.    {
  224.        $gettext_type="xml";
  225.    }
  226.    elsif ($type =~ /$ini_regex$/) 
  227.    { 
  228.        $gettext_type="ini";
  229.    }
  230.    elsif ($type =~ /$buildin_regex$/) 
  231.    {
  232.        $gettext_type="buildin";
  233.    }
  234.    else
  235.    { 
  236.        $gettext_type="unknown"; 
  237.    }
  238.    return "gettext/$gettext_type";
  239. }
  240. sub TextFile_DetermineEncoding ($) 
  241. {
  242.     my $gettext_code="ASCII"; # All files are ASCII by default
  243.     my $filetype=`file $_ | cut -d ' ' -f 2`;
  244.     if ($? eq "0")
  245.     {
  246. if ($filetype =~ /^(ISO|UTF)/)
  247. {
  248.     chomp ($gettext_code = $filetype);
  249. }
  250. elsif ($filetype =~ /^XML/)
  251. {
  252.     $gettext_code="UTF-8"; # We asume that .glade and other .xml files are UTF-8
  253. }
  254.     }
  255.     return $gettext_code;
  256. }
  257. sub isNotValidMissing
  258. {
  259.     my ($file) = @_;
  260.     return if $file =~ /^{arch}/.*$/;
  261.     return if $file =~ /^$varhash{"PACKAGE"}-$varhash{"VERSION"}/.*$/;
  262. }
  263. sub FindLeftoutFiles
  264. {
  265.     my (@buf_i18n_plain,
  266. @buf_i18n_xml,
  267. @buf_i18n_xml_unmarked,
  268. @buf_i18n_ini,
  269. @buf_potfiles,
  270. @buf_potfiles_ignore,
  271. @buf_allfiles,
  272. @buf_allfiles_sorted,
  273. @buf_potfiles_sorted,
  274.         @buf_potfiles_ignore_sorted
  275.     );
  276.     ## Search and find all translatable files
  277.     find sub { 
  278. push @buf_i18n_plain,        "$File::Find::name" if /.($buildin_gettext_support)$/;
  279. push @buf_i18n_xml,          "$File::Find::name" if /.($xml_support)$/;
  280. push @buf_i18n_ini,          "$File::Find::name" if /.($ini_support)$/;
  281. push @buf_i18n_xml_unmarked, "$File::Find::name" if /.(schemas(.in)+)$/;
  282. }, "..";
  283.     open POTFILES, $POTFILES_in or die "$PROGRAM:  there's no POTFILES.in!n";
  284.     @buf_potfiles = grep !/^(#|s*$)/, <POTFILES>;
  285.     close POTFILES;
  286.     foreach (@buf_potfiles) {
  287. s/^[.*]s*//;
  288.     }
  289.     print "Searching for missing translatable files...n" if $VERBOSE;
  290.     ## Check if we should ignore some found files, when
  291.     ## comparing with POTFILES.in
  292.     foreach my $ignore ("POTFILES.skip", "POTFILES.ignore")
  293.     {
  294. (-s "$SRCDIR/$ignore") or next;
  295. if ("$ignore" eq "POTFILES.ignore")
  296. {
  297.     print "The usage of POTFILES.ignore is deprecated. Please consider moving then".
  298.   "content of this file to POTFILES.skip.n";
  299. }
  300. print "Found $ignore: Ignoring files...n" if $VERBOSE;
  301. open FILE, "<$SRCDIR/$ignore" or die "ERROR: Failed to open $SRCDIR/$ignore!n";
  302.     
  303. while (<FILE>)
  304. {
  305.     push @buf_potfiles_ignore, $_ unless /^(#|s*$)/;
  306. }
  307. close FILE;
  308. @buf_potfiles_ignore_sorted = sort (@buf_potfiles_ignore);
  309.     }
  310.     foreach my $file (@buf_i18n_plain)
  311.     {
  312. my $in_comment = 0;
  313. my $in_macro = 0;
  314. open FILE, "<$file";
  315. while (<FILE>)
  316. {
  317.     # Handle continued multi-line comment.
  318.     if ($in_comment)
  319.     {
  320. next unless s-.**/--;
  321. $in_comment = 0;
  322.     }
  323.     # Handle continued macro.
  324.     if ($in_macro)
  325.     {
  326. $in_macro = 0 unless /\$/;
  327. next;
  328.     }
  329.     # Handle start of macro (or any preprocessor directive).
  330.     if (/^s*#/)
  331.     {
  332. $in_macro = 1 if /^([^\]|\.)*\$/;
  333. next;
  334.     }
  335.     # Handle comments and quoted text.
  336.     while (m-(/*|//|'|")-) # ' and " keep emacs perl mode happy
  337.     {
  338. my $match = $1;
  339. if ($match eq "/*")
  340. {
  341.     if (!s-/*.*?*/--)
  342.     {
  343. s-/*.*--;
  344. $in_comment = 1;
  345.     }
  346. }
  347. elsif ($match eq "//")
  348. {
  349.     s-//.*--;
  350. }
  351. else # ' or "
  352. {
  353.     if (!s-$match([^\]|\.)*?$match-QUOTEDTEXT-)
  354.     {
  355. warn "mismatched quotes at line $. in $filen";
  356. s-$match.*--;
  357.     }
  358. }
  359.     }     
  360.     if (/w.GetString *(QUOTEDTEXT/)
  361.     {
  362.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  363.                     ## Remove the first 3 chars and add newline
  364.                     push @buf_allfiles, unpack("x3 A*", $file) . "n";
  365.                 }
  366. last;
  367.     }
  368.             ## N_ Q_ and _ are the three macros defined in gi8n.h
  369.     if (/[NQ]?_ *(QUOTEDTEXT/)
  370.     {
  371.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  372.                     ## Remove the first 3 chars and add newline
  373.                     push @buf_allfiles, unpack("x3 A*", $file) . "n";
  374.                 }
  375. last;
  376.     }
  377. }
  378. close FILE;
  379.     }
  380.     foreach my $file (@buf_i18n_xml) 
  381.     {
  382. open FILE, "<$file";
  383. while (<FILE>) 
  384. {
  385.     # FIXME: share the pattern matching code with intltool-extract
  386.     if (/s_[-A-Za-z0-9._:]+s*=s*"([^"]+)"/ || /<_[^>]+>/ || /translatable="yes"/)
  387.     {
  388.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  389.                     push @buf_allfiles, unpack("x3 A*", $file) . "n";
  390.                 }
  391. last;
  392.     }
  393. }
  394. close FILE;
  395.     }
  396.     foreach my $file (@buf_i18n_ini)
  397.     {
  398. open FILE, "<$file";
  399. while (<FILE>) 
  400. {
  401.     if (/_(.*)=/)
  402.     {
  403.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  404.                     push @buf_allfiles, unpack("x3 A*", $file) . "n";
  405.                 }
  406. last;
  407.     }
  408. }
  409. close FILE;
  410.     }
  411.     foreach my $file (@buf_i18n_xml_unmarked)
  412.     {
  413.         if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  414.             push @buf_allfiles, unpack("x3 A*", $file) . "n";
  415.         }
  416.     }
  417.     @buf_allfiles_sorted = sort (@buf_allfiles);
  418.     @buf_potfiles_sorted = sort (@buf_potfiles);
  419.     my %in2;
  420.     foreach (@buf_potfiles_sorted) 
  421.     {
  422. $in2{$_} = 1;
  423.     }
  424.     foreach (@buf_potfiles_ignore_sorted) 
  425.     {
  426. $in2{$_} = 1;
  427.     }
  428.     my @result;
  429.     foreach (@buf_allfiles_sorted)
  430.     {
  431. if (!exists($in2{$_}))
  432. {
  433.     push @result, $_
  434. }
  435.     }
  436.     my @buf_potfiles_notexist;
  437.     foreach (@buf_potfiles_sorted)
  438.     {
  439. chomp (my $dummy = $_);
  440. if ("$dummy" ne "" and !(-f "$SRCDIR/../$dummy" or -f "../$dummy"))
  441. {
  442.     push @buf_potfiles_notexist, $_;
  443. }
  444.     }
  445.     ## Save file with information about the files missing
  446.     ## if any, and give information about this procedure.
  447.     if (@result + @buf_potfiles_notexist > 0)
  448.     {
  449. if (@result) 
  450. {
  451.     print "n" if $VERBOSE;
  452.     unlink "missing";
  453.     open OUT, ">missing";
  454.     print OUT @result;
  455.     close OUT;
  456.     warn "e[1mThe following files contain translations and are currently not in use. Pleasee[0mn".
  457.          "e[1mconsider adding these to the POTFILES.in file, located in the po/ directory.e[0mnn";
  458.     print STDERR @result, "n";
  459.     warn "If some of these files are left out on purpose then please add them ton".
  460.  "POTFILES.skip instead of POTFILES.in. A file e[1m'missing'e[0m containing this listn".
  461.  "of left out files has been written in the current directory.n";
  462. }
  463. if (@buf_potfiles_notexist)
  464. {
  465.     unlink "notexist";
  466.     open OUT, ">notexist";
  467.     print OUT @buf_potfiles_notexist;
  468.     close OUT;
  469.     warn "n" if ($VERBOSE or @result);
  470.     warn "e[1mThe following files do not exist anymore:e[0mnn";
  471.     warn @buf_potfiles_notexist, "n";
  472.     warn "Please remove them from POTFILES.in. A file e[1m'notexist'e[0mn".
  473.  "containing this list of absent files has been written in the current directory.n";
  474. }
  475.     }
  476.     ## If there is nothing to complain about, notify the user
  477.     else {
  478. print "nAll files containing translations are present in POTFILES.in.n" if $VERBOSE;
  479.     }
  480. }
  481. sub Console_WriteError_InvalidOption
  482. {
  483.     ## Handle invalid arguments
  484.     print STDERR "Try `${PROGRAM} --help' for more information.n";
  485.     exit 1;
  486. }
  487. sub isIntltoolExtractInPath
  488. {
  489.     my ($file) = @_;
  490.     # If either a file exists, or when run it returns 0 exit status
  491.     return 1 if ((-x $file) or (system("$file >/dev/null") == 0));
  492.     return 0;
  493. }
  494. sub GenerateHeaders
  495. {
  496.     my $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} || "intltool-extract";
  497.     ## Generate the .h header files, so we can allow glade and
  498.     ## xml translation support
  499.     if (! isIntltoolExtractInPath("$EXTRACT"))
  500.     {
  501. print STDERR "n *** The intltool-extract script wasn't found!"
  502.      ."n *** Without it, intltool-update can not generate files.n";
  503. exit;
  504.     }
  505.     else
  506.     {
  507. open (FILE, $POTFILES_in) or die "$PROGRAM: POTFILES.in not found.n";
  508. while (<FILE>) 
  509. {
  510.    chomp;
  511.    next if /^[s*encoding/;
  512.    ## Find xml files in POTFILES.in and generate the
  513.    ## files with help from the extract script
  514.    my $gettext_type= &POFile_DetermineType ($1);
  515.    if (/.($xml_support|$ini_support)$/ || /^[/)
  516.    {
  517.        s/^[[^[].*]s*//;
  518.        my $filename = "../$_";
  519.        if ($VERBOSE)
  520.        {
  521.    system ($EXTRACT, "--update", "--srcdir=$SRCDIR",
  522.    "--type=$gettext_type", $filename);
  523.        } 
  524.        else 
  525.        {
  526.      system ($EXTRACT, "--update", "--type=$gettext_type", 
  527.    "--srcdir=$SRCDIR", "--quiet", $filename);
  528.        }
  529.    }
  530.        }
  531.        close FILE;
  532.    }
  533. }
  534. #
  535. # Generate .pot file from POTFILES.in
  536. #
  537. sub GeneratePOTemplate
  538. {
  539.     my $XGETTEXT = $ENV{"XGETTEXT"} || "@INTLTOOL_XGETTEXT@";
  540.     my $XGETTEXT_ARGS = $ENV{"XGETTEXT_ARGS"} || '';
  541.     chomp $XGETTEXT;
  542.     if (! -x $XGETTEXT)
  543.     {
  544. print STDERR " *** xgettext is not found on this system!n".
  545.      " *** Without it, intltool-update can not extract strings.n";
  546. exit;
  547.     }
  548.     print "Building $MODULE.pot...n" if $VERBOSE;
  549.     open INFILE, $POTFILES_in;
  550.     unlink "POTFILES.in.temp";
  551.     open OUTFILE, ">POTFILES.in.temp" or die("Cannot open POTFILES.in.temp for writing");
  552.     my $gettext_support_nonascii = 0;
  553.     # checks for GNU gettext >= 0.12
  554.     my $dummy = `$XGETTEXT --version --from-code=UTF-8 >$devnull 2>$devnull`;
  555.     if ($? == 0)
  556.     {
  557. $gettext_support_nonascii = 1;
  558.     }
  559.     else
  560.     {
  561. # urge everybody to upgrade gettext
  562. print STDERR "WARNING: This version of gettext does not support extracting non-ASCIIn".
  563.      "         strings. That means you should install a version of gettextn".
  564.      "         that supports non-ASCII strings (such as GNU gettext >= 0.12),n".
  565.      "         or have to let non-ASCII strings untranslated. (If there is any)n";
  566.     }
  567.     my $encoding = "ASCII";
  568.     my $forced_gettext_code;
  569.     my @temp_headers;
  570.     my $encoding_problem_is_reported = 0;
  571.     while (<INFILE>) 
  572.     {
  573. next if (/^#/ or /^s*$/);
  574. chomp;
  575. my $gettext_code;
  576. if (/^[s*encoding:s*(.*)s*]/)
  577. {
  578.     $forced_gettext_code=$1;
  579. }
  580. elsif (/.($xml_support|$ini_support)$/ || /^[/)
  581. {
  582.     s/^[.*]s*//;
  583.             print OUTFILE "../$_.hn";
  584.     push @temp_headers, "../$_.h";
  585.     $gettext_code = &TextFile_DetermineEncoding ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
  586. else 
  587. {
  588.             print OUTFILE "$SRCDIR/../$_n";
  589.     $gettext_code = &TextFile_DetermineEncoding ("$SRCDIR/../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
  590. }
  591. next if (! $gettext_support_nonascii);
  592. if (defined $forced_gettext_code)
  593. {
  594.     $encoding=$forced_gettext_code;
  595. }
  596. elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
  597. {
  598.     if ($encoding eq "ASCII")
  599.     {
  600. $encoding=$gettext_code;
  601.     }
  602.     elsif ($gettext_code ne "ASCII")
  603.     {
  604. # Only report once because the message is quite long
  605. if (! $encoding_problem_is_reported)
  606. {
  607.     print STDERR "WARNING: You should use the same file encoding for all your project files,n".
  608.  "         but $PROGRAM thinks that most of the source files are inn".
  609.  "         $encoding encoding, while "$_" is (likely) inn".
  610.          "         $gettext_code encoding. If you are sure that all translatable stringsn".
  611.  "         are in same encoding (say UTF-8), please e[1m*prepend*e[0m the followingn".
  612.  "         line to POTFILES.in:nn".
  613.  "                 [encoding: UTF-8]nn".
  614.  "         and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .n".
  615.  "(such warning message will only be reported once.)n";
  616.     $encoding_problem_is_reported = 1;
  617. }
  618.     }
  619. }
  620.     }
  621.     close OUTFILE;
  622.     close INFILE;
  623.     unlink "$MODULE.pot";
  624.     my @xgettext_argument=("$XGETTEXT",
  625.    "--add-comments",
  626.    "--directory=.",
  627.    "--output=$MODULE.pot",
  628.    "--files-from=./POTFILES.in.temp");
  629.     my $XGETTEXT_KEYWORDS = &FindPOTKeywords;
  630.     push @xgettext_argument, $XGETTEXT_KEYWORDS;
  631.     my $MSGID_BUGS_ADDRESS = &FindMakevarsBugAddress;
  632.     push @xgettext_argument, "--msgid-bugs-address=$MSGID_BUGS_ADDRESS" if $MSGID_BUGS_ADDRESS;
  633.     push @xgettext_argument, "--from-code=$encoding" if ($gettext_support_nonascii);
  634.     push @xgettext_argument, $XGETTEXT_ARGS if $XGETTEXT_ARGS;
  635.     my $xgettext_command = join ' ', @xgettext_argument;
  636.     # intercept xgettext error message
  637.     print "Running $xgettext_commandn" if $VERBOSE;
  638.     my $xgettext_error_msg = `$xgettext_command 2>&1`;
  639.     my $command_failed = $?;
  640.     unlink "POTFILES.in.temp";
  641.     print "Removing generated header (.h) files..." if $VERBOSE;
  642.     unlink foreach (@temp_headers);
  643.     print "done.n" if $VERBOSE;
  644.     if (! $command_failed)
  645.     {
  646. if (! -e "$MODULE.pot")
  647. {
  648.     print "None of the files in POTFILES.in contain strings marked for translation.n" if $VERBOSE;
  649. }
  650. else
  651. {
  652.     print "Wrote $MODULE.potn" if $VERBOSE;
  653. }
  654.     }
  655.     else
  656.     {
  657. if ($xgettext_error_msg =~ /--from-code/)
  658. {
  659.     # replace non-ASCII error message with a more useful one.
  660.     print STDERR "ERROR: xgettext failed to generate PO template file because there is non-ASCIIn".
  661.  "       string marked for translation. Please make sure that all strings markedn".
  662.  "       for translation are in uniform encoding (say UTF-8), then e[1m*prepend*e[0m then".
  663.  "       following line to POTFILES.in and rerun $PROGRAM:nn".
  664.  "           [encoding: UTF-8]nn";
  665. }
  666. else
  667. {
  668.     print STDERR "$xgettext_error_msg";
  669.     if (-e "$MODULE.pot")
  670.     {
  671. # is this possible?
  672. print STDERR "ERROR: xgettext failed but still managed to generate PO template file.n".
  673.      "       Please consult error message above if there is any.n";
  674.     }
  675.     else
  676.     {
  677. print STDERR "ERROR: xgettext failed to generate PO template file. Please consultn".
  678.      "       error message above if there is any.n";
  679.     }
  680. }
  681. exit (1);
  682.     }
  683. }
  684. sub POFile_Update
  685. {
  686.     -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.n";
  687.     my $MSGMERGE = $ENV{"MSGMERGE"} || "@INTLTOOL_MSGMERGE@";
  688.     my ($lang, $outfile) = @_;
  689.     print "Merging $SRCDIR/$lang.po with $MODULE.pot..." if $VERBOSE;
  690.     my $infile = "$SRCDIR/$lang.po";
  691.     $outfile = "$SRCDIR/$lang.po" if ($outfile eq "");
  692.     # I think msgmerge won't overwrite old file if merge is not successful
  693.     system ("$MSGMERGE", "-o", $outfile, $infile, "$MODULE.pot");
  694. }
  695. sub Console_WriteError_NotExisting
  696. {
  697.     my ($file) = @_;
  698.     ## Report error if supplied language file is non-existing
  699.     print STDERR "$PROGRAM: $file does not exist!n";
  700.     print STDERR "Try '$PROGRAM --help' for more information.n";
  701.     exit;
  702. }
  703. sub GatherPOFiles
  704. {
  705.     my @po_files = glob ("./*.po");
  706.     @languages = map (&POFile_GetLanguage, @po_files);
  707.     foreach my $lang (@languages) 
  708.     {
  709. $po_files_by_lang{$lang} = shift (@po_files);
  710.     }
  711. }
  712. sub POFile_GetLanguage ($)
  713. {
  714.     s/^(.*/)?(.+).po$/$2/;
  715.     return $_;
  716. }
  717. sub Console_Write_TranslationStatus
  718. {
  719.     my ($lang, $output_file) = @_;
  720.     my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
  721.     $output_file = "$SRCDIR/$lang.po" if ($output_file eq "");
  722.     system ("$MSGFMT", "-o", "$devnull", "--verbose", $output_file);
  723. }
  724. sub Console_Write_CoverageReport
  725. {
  726.     my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
  727.     &GatherPOFiles;
  728.     foreach my $lang (@languages) 
  729.     {
  730. print "$lang: ";
  731. &POFile_Update ($lang, "");
  732.     }
  733.     print "nn * Current translation support in $MODULE nn";
  734.     foreach my $lang (@languages)
  735.     {
  736. print "$lang: ";
  737. system ("$MSGFMT", "-o", "$devnull", "--verbose", "$SRCDIR/$lang.po");
  738.     }
  739. }
  740. sub SubstituteVariable
  741. {
  742.     my ($str) = @_;
  743.     
  744.     # always need to rewind file whenever it has been accessed
  745.     seek (CONF, 0, 0);
  746.     # cache each variable. varhash is global to we can add
  747.     # variables elsewhere.
  748.     while (<CONF>)
  749.     {
  750. if (/^(w+)=(.*)$/)
  751. {
  752.     ($varhash{$1} = $2) =~  s/^["'](.*)["']$/$1/;
  753. }
  754.     }
  755.     
  756.     if ($str =~ /^(.*)${?([A-Z_]+)}?(.*)$/)
  757.     {
  758. my $rest = $3;
  759. my $untouched = $1;
  760. my $sub = "";
  761.         # Ignore recursive definitions of variables
  762.         $sub = $varhash{$2} if defined $varhash{$2} and $varhash{$2} !~ /${?$2}?/;
  763. return SubstituteVariable ("$untouched$sub$rest");
  764.     }
  765.     
  766.     # We're using Perl backticks ` and "echo -n" here in order to 
  767.     # expand any shell escapes (such as backticks themselves) in every variable
  768.     return echo_n ($str);
  769. }
  770. sub CONF_Handle_Open
  771. {
  772.     my $base_dirname = getcwd();
  773.     $base_dirname =~ s@.*/@@;
  774.     my ($conf_in, $src_dir);
  775.     if ($base_dirname =~ /^po(-.+)?$/) 
  776.     {
  777. if (-f "Makevars") 
  778. {
  779.     my $makefile_source;
  780.     local (*IN);
  781.     open (IN, "<Makevars") || die "can't open Makevars: $!";
  782.     while (<IN>) 
  783.     {
  784. if (/^top_builddir[ t]*=/) 
  785. {
  786.     $src_dir = $_;
  787.     $src_dir =~ s/^top_builddir[ t]*=[ t]*([^ tnr]*)/$1/;
  788.     chomp $src_dir;
  789.                     if (-f "$src_dir" . "/configure.ac") {
  790.                         $conf_in = "$src_dir" . "/configure.ac" . "n";
  791.                     } else {
  792.                         $conf_in = "$src_dir" . "/configure.in" . "n";
  793.                     }
  794.     last;
  795. }
  796.     }
  797.     close IN;
  798.     $conf_in || die "Cannot find top_builddir in Makevars.";
  799. }
  800. elsif (-f "../configure.ac") 
  801. {
  802.     $conf_in = "../configure.ac";
  803. elsif (-f "../configure.in") 
  804. {
  805.     $conf_in = "../configure.in";
  806. else 
  807. {
  808.     my $makefile_source;
  809.     local (*IN);
  810.     open (IN, "<Makefile") || return;
  811.     while (<IN>) 
  812.     {
  813. if (/^top_srcdir[ t]*=/) 
  814. {
  815.     $src_dir = $_;     
  816.     $src_dir =~ s/^top_srcdir[ t]*=[ t]*([^ tnr]*)/$1/;
  817.     chomp $src_dir;
  818.     $conf_in = "$src_dir" . "/configure.in" . "n";
  819.     last;
  820. }
  821.     }
  822.     close IN;
  823.     $conf_in || die "Cannot find top_srcdir in Makefile.";
  824. }
  825. open (CONF, "<$conf_in");
  826.     }
  827.     else
  828.     {
  829. print STDERR "$PROGRAM: Unable to proceed.n" .
  830.      "Make sure to run this script inside the po directory.n";
  831. exit;
  832.     }
  833. }
  834. sub FindPackageName
  835. {
  836.     my $version;
  837.     my $domain = &FindMakevarsDomain;
  838.     my $name = $domain || "untitled";
  839.     &CONF_Handle_Open;
  840.     my $conf_source; {
  841. local (*IN);
  842. open (IN, "<&CONF") || return $name;
  843. seek (IN, 0, 0);
  844. local $/; # slurp mode
  845. $conf_source = <IN>;
  846. close IN;
  847.     }
  848.     # priority for getting package name:
  849.     # 1. GETTEXT_PACKAGE
  850.     # 2. first argument of AC_INIT (with >= 2 arguments)
  851.     # 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)
  852.     # /^AM_INIT_AUTOMAKE([s[]*([^,)s]]+)/m 
  853.     # the s makes this not work, why?
  854.     if ($conf_source =~ /^AM_INIT_AUTOMAKE(([^,)]+),([^,)]+)/m)
  855.     {
  856. ($name, $version) = ($1, $2);
  857. $name    =~ s/[[]s]//g;
  858. $version =~ s/[[]s]//g;
  859. $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /${?AC_PACKAGE_NAME}?/);
  860. $varhash{"PACKAGE"} = $name if (not $name =~ /${?PACKAGE}?/);
  861. $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /${?AC_PACKAGE_VERSION}?/);
  862. $varhash{"VERSION"} = $version if (not $name =~ /${?VERSION}?/);
  863.     }
  864.     
  865.     if ($conf_source =~ /^AC_INIT(([^,)]+),([^,)]+)/m) 
  866.     {
  867. ($name, $version) = ($1, $2);
  868. $name    =~ s/[[]s]//g;
  869. $version =~ s/[[]s]//g;
  870. $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /${?AC_PACKAGE_NAME}?/);
  871. $varhash{"PACKAGE"} = $name if (not $name =~ /${?PACKAGE}?/);
  872. $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /${?AC_PACKAGE_VERSION}?/);
  873. $varhash{"VERSION"} = $version if (not $name =~ /${?VERSION}?/);
  874.     }
  875.     # s makes this not work, why?
  876.     $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=[?([^n]]+)/m;
  877.     
  878.     # m4 macros AC_PACKAGE_NAME, AC_PACKAGE_VERSION etc. have same value
  879.     # as corresponding $PACKAGE_NAME, $PACKAGE_VERSION etc. shell variables.
  880.     $name =~ s/bAC_PACKAGE_/$PACKAGE_/g;
  881.     $name = $domain if $domain;
  882.     $name = SubstituteVariable ($name);
  883.     $name =~ s/^["'](.*)["']$/$1/;
  884.     return $name if $name;
  885. }
  886. sub FindPOTKeywords
  887. {
  888.     my $keywords = "--keyword=_ --keyword=N_ --keyword=U_ --keyword=Q_";
  889.     my $varname = "XGETTEXT_OPTIONS";
  890.     my $make_source; {
  891. local (*IN);
  892. open (IN, "<Makevars") || (open(IN, "<Makefile.in.in") && ($varname = "XGETTEXT_KEYWORDS")) || return $keywords;
  893. seek (IN, 0, 0);
  894. local $/; # slurp mode
  895. $make_source = <IN>;
  896. close IN;
  897.     }
  898.     $keywords = $1 if $make_source =~ /^$varname[ ]*=[?([^n]]+)/m;
  899.     
  900.     return $keywords;
  901. }
  902. sub FindMakevarsDomain
  903. {
  904.     my $domain = "";
  905.     my $makevars_source; { 
  906. local (*IN);
  907. open (IN, "<Makevars") || return $domain;
  908. seek (IN, 0, 0);
  909. local $/; # slurp mode
  910. $makevars_source = <IN>;
  911. close IN;
  912.     }
  913.     $domain = $1 if $makevars_source =~ /^DOMAIN[ ]*=[?([^n]$]+)/m;
  914.     $domain =~ s/^s+//;
  915.     $domain =~ s/s+$//;
  916.     
  917.     return $domain;
  918. }
  919. sub FindMakevarsBugAddress
  920. {
  921.     my $address = "";
  922.     my $makevars_source; { 
  923. local (*IN);
  924. open (IN, "<Makevars") || return undef;
  925. seek (IN, 0, 0);
  926. local $/; # slurp mode
  927. $makevars_source = <IN>;
  928. close IN;
  929.     }
  930.     $address = $1 if $makevars_source =~ /^MSGID_BUGS_ADDRESS[ ]*=[?([^n]$]+)/m;
  931.     $address =~ s/^s+//;
  932.     $address =~ s/s+$//;
  933.     
  934.     return $address;
  935. }