Lines 25-31
use C4::Context;
Link Here
|
25 |
use YAML::Syck qw( Dump LoadFile ); |
25 |
use YAML::Syck qw( Dump LoadFile ); |
26 |
use Locale::PO; |
26 |
use Locale::PO; |
27 |
use FindBin qw( $Bin ); |
27 |
use FindBin qw( $Bin ); |
|
|
28 |
use File::Basename; |
29 |
use File::Find; |
28 |
use File::Path qw( make_path ); |
30 |
use File::Path qw( make_path ); |
|
|
31 |
use File::Slurp; |
32 |
use File::Temp qw( tempdir ); |
33 |
use Template::Parser; |
34 |
use PPI; |
29 |
|
35 |
|
30 |
$YAML::Syck::ImplicitTyping = 1; |
36 |
$YAML::Syck::ImplicitTyping = 1; |
31 |
|
37 |
|
Lines 507-512
sub update_messages {
Link Here
|
507 |
system "$self->{msgmerge} -U $pofile $self->{domain}.pot"; |
513 |
system "$self->{msgmerge} -U $pofile $self->{domain}.pot"; |
508 |
} |
514 |
} |
509 |
|
515 |
|
|
|
516 |
sub extract_messages_from_templates { |
517 |
my ($self, $tempdir, @files) = @_; |
518 |
|
519 |
my $intranetdir = $self->{context}->config('intranetdir'); |
520 |
my @keywords = qw(t tx tn txn tnx tp tpx tnp tnpx); |
521 |
my $parser = Template::Parser->new(); |
522 |
|
523 |
foreach my $file (@files) { |
524 |
my $template = read_file("$intranetdir/$file"); |
525 |
my $data = $parser->parse($template); |
526 |
my $document = PPI::Document->new(\$data->{BLOCK}); |
527 |
|
528 |
# [% t('foo') %] is compiled to $output .= $stash->get(['t', ['foo']]); |
529 |
# We try to find all nodes corresponding to keyword (here 't') |
530 |
my $nodes = $document->find(sub { |
531 |
my ($topnode, $element) = @_; |
532 |
|
533 |
# Filter out non-valid keywords |
534 |
return 0 unless ($element->isa('PPI::Token::Quote::Single')); |
535 |
return 0 unless (grep {$element->content eq qq{'$_'}} @keywords); |
536 |
|
537 |
# keyword (e.g. 't') should be the first element of the arrayref |
538 |
# passed to $stash->get() |
539 |
return 0 if $element->sprevious_sibling; |
540 |
|
541 |
return 0 unless $element->snext_sibling |
542 |
&& $element->snext_sibling->snext_sibling |
543 |
&& $element->snext_sibling->snext_sibling->isa('PPI::Structure::Constructor'); |
544 |
|
545 |
# Check that it's indeed a call to $stash->get() |
546 |
my $statement = $element->statement->parent->statement->parent->statement; |
547 |
return 0 unless grep { $_->isa('PPI::Token::Symbol') && $_->content eq '$stash' } $statement->children; |
548 |
return 0 unless grep { $_->isa('PPI::Token::Operator') && $_->content eq '->' } $statement->children; |
549 |
return 0 unless grep { $_->isa('PPI::Token::Word') && $_->content eq 'get' } $statement->children; |
550 |
|
551 |
return 1; |
552 |
}); |
553 |
|
554 |
next unless $nodes; |
555 |
|
556 |
# Write the Perl equivalent of calls to t* functions family, so xgettext |
557 |
# can extract the strings correctly |
558 |
make_path(dirname("$tempdir/$file")); |
559 |
open my $fh, '>', "$tempdir/$file"; |
560 |
|
561 |
foreach my $node (@$nodes) { |
562 |
my @args = map { |
563 |
$_->significant && !$_->isa('PPI::Token::Operator') ? $_->content : () |
564 |
} $node->snext_sibling->snext_sibling->find_first('PPI::Statement')->children; |
565 |
|
566 |
my $keyword = $node->content; |
567 |
$keyword =~ s/^'t(.*)'$/__$1/; |
568 |
|
569 |
say $fh "$keyword(" . join(', ', @args) . ");"; |
570 |
} |
571 |
|
572 |
close $fh; |
573 |
} |
574 |
|
575 |
return $tempdir; |
576 |
} |
577 |
|
510 |
sub extract_messages { |
578 |
sub extract_messages { |
511 |
my $self = shift; |
579 |
my $self = shift; |
512 |
|
580 |
|
Lines 529-539
sub extract_messages {
Link Here
|
529 |
} |
597 |
} |
530 |
} |
598 |
} |
531 |
|
599 |
|
|
|
600 |
my @tt_files; |
601 |
find(sub { |
602 |
if ($File::Find::dir =~ m|/en/| && $_ =~ m/\.(tt|inc)$/) { |
603 |
my $filename = $File::Find::name; |
604 |
$filename =~ s|^$intranetdir/||; |
605 |
push @tt_files, $filename; |
606 |
} |
607 |
}, "$intranetdir/koha-tmpl"); |
608 |
|
609 |
my $tempdir = tempdir('Koha-translate-XXXX', TMPDIR => 1, CLEANUP => 1); |
610 |
$self->extract_messages_from_templates($tempdir, @tt_files); |
611 |
push @files_to_scan, @tt_files; |
612 |
|
532 |
my $xgettext_cmd = "$self->{xgettext} -L Perl --from-code=UTF-8 " |
613 |
my $xgettext_cmd = "$self->{xgettext} -L Perl --from-code=UTF-8 " |
533 |
. "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 " |
614 |
. "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 " |
534 |
. "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ -kN__n:1,2 " |
615 |
. "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ -kN__n:1,2 " |
535 |
. "-kN__p:1c,2 -kN__np:1c,2,3 " |
616 |
. "-kN__p:1c,2 -kN__np:1c,2,3 " |
536 |
. "-o $Bin/$self->{domain}.pot -D $intranetdir"; |
617 |
. "-o $Bin/$self->{domain}.pot -D $tempdir -D $intranetdir"; |
537 |
$xgettext_cmd .= " $_" foreach (@files_to_scan); |
618 |
$xgettext_cmd .= " $_" foreach (@files_to_scan); |
538 |
|
619 |
|
539 |
if (system($xgettext_cmd) != 0) { |
620 |
if (system($xgettext_cmd) != 0) { |
540 |
- |
|
|