Skip to content

Commit

Permalink
Item14152: Merge commit '4eef383f3c031dce6d2ccba1af7c5657784fc036' in…
Browse files Browse the repository at this point in the history
…to Item14152

* commit '4eef383f3c031dce6d2ccba1af7c5657784fc036':
  Item13897: Revert xgettext to the non-OO version in master
  • Loading branch information
vrurg committed Sep 6, 2016
2 parents 3f9dcc1 + 4eef383 commit 55bb1bd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 103 deletions.
60 changes: 22 additions & 38 deletions core/lib/Foswiki/I18N/Extract.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
Support translatable strings extraction from Foswiki topics and templates.
Depends on Locale::Maketext::Extract (part of CPAN::Locale::Maketext::Lexicon).
SMELL: This is purely an offline extractor that is run by the tools/xgettext
utility. There is no reason that it needs to be part of the Foswiki class
hierarchy. it does not use any other Foswiki classes.
=cut

package Foswiki::I18N::Extract;
use v5.14;

use Assert;
use Try::Tiny;

use Foswiki::Class qw(app);
extends qw(Foswiki::Object);
use strict;
use warnings;

our $initError;

Expand All @@ -35,46 +35,30 @@ BEGIN {
}
}

has extractor => (
is => 'rw',
lazy => 1,
default => sub {
return new Locale::Maketext::Extract;
},
handles => [qw(extract_file compile write_po)],
);

##########################################################

=begin TML
---++ ClassMethod new ( app => $app ) -> $extract
---++ ClassMethod new ( $session ) -> $extract
Constructor. Creates a fresh new Extract object. A $app object, instance of
Constructor. Creates a fresh new Extract object. A $session object, instance of
the Foswiki class, is optional: if it's available, it'll be used for printing
warnings.
=cut

around BUILDARGS => sub {
my $orig = shift;

try {
Foswiki::load_package('Locale::Maketext::Extract');
}
catch {
$initError = ref($_) ? $_->stringify : $_;
};

return $orig->(@_);
};

sub BUILD {
my $this = shift;
sub new {
my $class = shift;
my $session = shift;

if ( defined $initError ) {
$this->app->logger->log( 'warning', $initError );
print STDERR $initError;
return;
}

my $self = new Locale::Maketext::Extract;
$self->{session} = $session;
return bless( $self, $class );
}

=begin TML
Expand All @@ -85,18 +69,18 @@ Extract the strings from =$text=,m using =$file= as the name of the current
file being read (for comments in PO file, for example). Overrides the base
class method but calls it so the base behavior is preserved.
As in base class, extracted strings are just stored in the =$this='s internal
As in base class, extracted strings are just stored in the =$self='s internal
table for further use (e.g. creating/updating a PO file). Nothing is returned.
=cut

sub extract {
my $this = shift;
my $self = shift;
my $file = shift;
local $_ = shift;

# do existing extraction
$this->extractor->extract( $file, $_ ) unless ( $file =~ m/\.(txt|tmpl)$/ );
$self->SUPER::extract( $file, $_ ) unless ( $file =~ m/\.(txt|tmpl)$/ );

my $line;
my $doublequoted = '"(\\\"|[^"])*"';
Expand All @@ -109,7 +93,7 @@ sub extract {
while (m/%MAKETEXT\{\s*(string=)?($doublequoted)/gm) {
my $str = substr( $2, 1, -1 );
$str =~ s/\\"/"/g;
$this->add_entry( $str, [ $file, $line, '' ] );
$self->add_entry( $str, [ $file, $line, '' ] );
}
$line++;
}
Expand Down Expand Up @@ -140,7 +124,7 @@ sub extract {
$str =~ s/\\"/"/g;

# collect the string:
$this->add_entry( $str, [ $file, $line, '' ] );
$self->add_entry( $str, [ $file, $line, '' ] );
}
$line++;
}
Expand Down
104 changes: 39 additions & 65 deletions core/tools/xgettext
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,28 @@
# Script to extract strings from Foswiki sources and templates

use strict;
use v5.14;

# force the use from the root
-d 'core'
or die(
"You may only run this utility from the root directory of Foswiki SVN checkout!"
);
-d 'core' or die("You may only run this utility from the root directory of Foswiki SVN checkout!");

use File::Spec;
use Try::Tiny;

use Foswiki::App ();
use Foswiki::Exception ();

$ENV{PATH};
my $cwd = `pwd`;
my $cwd = `pwd` ;
chomp($cwd);
unshift( @INC, File::Spec->catfile( $cwd, 'core/lib' ) );
unshift(@INC, File::Spec->catfile($cwd, 'core/lib'));
eval "use Foswiki::I18N::Extract";
die($@) if $@;
eval "use Locale::Maketext::Lexicon";
die($@) if $@;

die('You need at least version 0.61 of Locale::Maketext::Lexicon to run this utility') unless $Locale::Maketext::Lexicon::VERSION >= 0.61;

die(
'The msgmerge command is required, it must be in your path. Try installing GNU gettext tools.'
) unless `which msgmerge`;
die('The msgmerge command is required, it must be in your path. Try installing GNU gettext tools.') unless `which msgmerge`;


# config
my $localesDir = "core/locale";
my $potfile = "${localesDir}/Foswiki.pot";
my $potfile = "${localesDir}/Foswiki.pot";
my $newpotfile = $potfile . '.new';

# extract strings from the source code and append to the new potfile
Expand All @@ -46,66 +36,55 @@ sub read_MANIFEST {
my ( $manifest, $prefix ) = @_;

my @files;
open( R, '<', $manifest ) || die "Could not open $manifest: $!";
open(R, '<', $manifest) || die "Could not open $manifest: $!";
foreach my $source (<R>) {
chomp($source);

if ( $source =~ m#^!include ../((\w+)/.*)$# ) {
my $included = $2;
if ($source =~ m#^!include ../((\w+)/.*)$#) {
my $included = $2;
my $included_manifest = $1 . '/MANIFEST';
print "I: including: $included, from $included_manifest \n";

push( @files, read_MANIFEST( $included_manifest, "$included/" ) );
push(@files, read_MANIFEST($included_manifest, "$included/"));
}

my @fields = split( /\s/, $source );
my @fields = split(/\s/, $source);
$source = $fields[0];
if (
$source =~ m/^bin\/[a-z]/
|| ( $source =~ m/^lib\/.*\.pm$/
&& ( !( $source =~ m/^lib\/CPAN\/.*\.pm$/ ) ) )
|| $source =~ m/^templates\/.*\.tmpl$/
|| $source =~ m/^data\/.*\.txt$/
)
{
if( $source =~ m/^bin\/[a-z]/ ||
($source =~ m/^lib\/.*\.pm$/ && (! ($source =~ m/^lib\/CPAN\/.*\.pm$/))) ||
$source =~ m/^templates\/.*\.tmpl$/ ||
$source =~ m/^data\/.*\.txt$/) {
my $file = $prefix . $source;
if ( -e $file ) {
push( @files, $prefix . $source );
}
else {
if (-e $file) {
push(@files, $prefix . $source);
} else {
print "W: $file is listed in MANIFEST and does not exist!\n";
}
}
}
close(R);

return @files;
return @files;
}

sub extract {
my ( $target, @sources ) = @_;
my ($target, @sources) = @_;
print "I: Extracting strings into $target.\n";

local %ENV = %ENV;
my $app = Foswiki::App->new( env => \%ENV );
my $extractor = $app->create('Foswiki::I18N::Extract');
die(
"Could not create an extractor. Is Locale::Maketext installed? (see above messages)"
) unless $extractor;
my $extractor = new Foswiki::I18N::Extract;
die("Could not create an extractor. Is Locale::Maketext installed? (see above messages)") unless $extractor;

my $progress = undef;
eval "use Term::ProgressBar;";
if ($@) {
print
"W: (You could have a nice progress bar if you had Term::ProgressBar installed.)\n";
if ( $@ ) {
print "W: (You could have a nice progress bar if you had Term::ProgressBar installed.)\n";
} else {
$progress = Term::ProgressBar->new (scalar(@sources));
}
else {
$progress = Term::ProgressBar->new( scalar(@sources) );
}


my $counter = 0;
foreach my $file (@sources) {
$extractor->extract_file($file);
$extractor->extract_file($file) ;
$counter++;
$progress->update($counter) if $progress;
}
Expand All @@ -114,42 +93,37 @@ sub extract {
}

sub merge {
my ( $old, $new ) = @_;
my ($old, $new) = @_;
print("I: Merging new strings in $new into $old.\n");
system("msgmerge --update $old $new");
}


# read the top-level MANIFEST, and any other included by it
my @sources = read_MANIFEST( 'core/lib/MANIFEST', '' );
my @sources = read_MANIFEST('core/lib/MANIFEST', '');

# handle --list: only list files to be scanned
if ( $ARGV[0] eq '--list' ) {
if ($ARGV[0] eq '--list') {
foreach my $file (@sources) {
print $file, "\n";
print $file, "\n";
}
exit 0;
}


# extract the strings to the template POT file.
try {
extract( $newpotfile, @sources );
}
catch {
say STDERR Foswiki::Exception::errorStr($_);
exit 1;
};
extract($newpotfile, @sources);

# merge with old POT file
if ( -e $potfile ) {
merge( $potfile, $newpotfile );
}
else {
if (-e $potfile) {
merge($potfile, $newpotfile);
} else {
system("cp $newpotfile $potfile");
}

# merge with existing translations:
my @translations = <${localesDir}/*.po>;
merge( $_, $potfile ) for @translations;
merge($_, $potfile) for @translations;

# cleanup
system("rm -rf $newpotfile");

0 comments on commit 55bb1bd

Please sign in to comment.