From c2e47023202ea17cee272b3e9900136787d5fcbc Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Sun, 24 Jun 2018 22:04:56 +0200 Subject: [PATCH] Bug 15395: Extract strings from TT BLOCKs too --- misc/translator/LangInstaller.pm | 76 ++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm index 141a410dd3..59754a748c 100644 --- a/misc/translator/LangInstaller.pm +++ b/misc/translator/LangInstaller.pm @@ -550,50 +550,60 @@ sub extract_messages_from_templates { say "Extract messages from $file" if $self->{verbose}; my $template = read_file("$intranetdir/$file"); my $data = $parser->parse($template); - my $document = PPI::Document->new(\$data->{BLOCK}); + unless ($data) { + warn "Error at $file : " . $parser->error(); + next; + } - # [% t('foo') %] is compiled to $output .= $stash->get(['t', ['foo']]); - # We try to find all nodes corresponding to keyword (here 't') - my $nodes = $document->find(sub { - my ($topnode, $element) = @_; + make_path(dirname("$tempdir/$file")); + open my $fh, '>', "$tempdir/$file"; - # Filter out non-valid keywords - return 0 unless ($element->isa('PPI::Token::Quote::Single')); - return 0 unless (grep {$element->content eq qq{'$_'}} @keywords); + my @blocks = ($data->{BLOCK}, values %{ $data->{DEFBLOCKS} }); + foreach my $block (@blocks) { + my $document = PPI::Document->new(\$block); - # keyword (e.g. 't') should be the first element of the arrayref - # passed to $stash->get() - return 0 if $element->sprevious_sibling; + # [% t('foo') %] is compiled to + # $output .= $stash->get(['t', ['foo']]); + # We try to find all nodes corresponding to keyword (here 't') + my $nodes = $document->find(sub { + my ($topnode, $element) = @_; - return 0 unless $element->snext_sibling - && $element->snext_sibling->snext_sibling - && $element->snext_sibling->snext_sibling->isa('PPI::Structure::Constructor'); + # Filter out non-valid keywords + return 0 unless ($element->isa('PPI::Token::Quote::Single')); + return 0 unless (grep {$element->content eq qq{'$_'}} @keywords); - # Check that it's indeed a call to $stash->get() - my $statement = $element->statement->parent->statement->parent->statement; - return 0 unless grep { $_->isa('PPI::Token::Symbol') && $_->content eq '$stash' } $statement->children; - return 0 unless grep { $_->isa('PPI::Token::Operator') && $_->content eq '->' } $statement->children; - return 0 unless grep { $_->isa('PPI::Token::Word') && $_->content eq 'get' } $statement->children; + # keyword (e.g. 't') should be the first element of the arrayref + # passed to $stash->get() + return 0 if $element->sprevious_sibling; - return 1; - }); + return 0 unless $element->snext_sibling + && $element->snext_sibling->snext_sibling + && $element->snext_sibling->snext_sibling->isa('PPI::Structure::Constructor'); - next unless $nodes; + # Check that it's indeed a call to $stash->get() + my $statement = $element->statement->parent->statement->parent->statement; + return 0 unless grep { $_->isa('PPI::Token::Symbol') && $_->content eq '$stash' } $statement->children; + return 0 unless grep { $_->isa('PPI::Token::Operator') && $_->content eq '->' } $statement->children; + return 0 unless grep { $_->isa('PPI::Token::Word') && $_->content eq 'get' } $statement->children; - # Write the Perl equivalent of calls to t* functions family, so xgettext - # can extract the strings correctly - make_path(dirname("$tempdir/$file")); - open my $fh, '>', "$tempdir/$file"; + return 1; + }); + + next unless $nodes; - foreach my $node (@$nodes) { - my @args = map { - $_->significant && !$_->isa('PPI::Token::Operator') ? $_->content : () - } $node->snext_sibling->snext_sibling->find_first('PPI::Statement')->children; + # Write the Perl equivalent of calls to t* functions family, so + # xgettext can extract the strings correctly + foreach my $node (@$nodes) { + my @args = map { + $_->significant && !$_->isa('PPI::Token::Operator') ? $_->content : () + } $node->snext_sibling->snext_sibling->find_first('PPI::Statement')->children; - my $keyword = $node->content; - $keyword =~ s/^'t(.*)'$/__$1/; + my $keyword = $node->content; + $keyword =~ s/^'t(.*)'$/__$1/; + + say $fh "$keyword(" . join(', ', @args) . ");"; + } - say $fh "$keyword(" . join(', ', @args) . ");"; } close $fh; -- 2.17.1