View | Details | Raw Unified | Return to bug 21895
Collapse All | Expand All

(-)a/misc/translator/LangInstaller.pm (-19 / +56 lines)
Lines 29-34 use File::Basename; Link Here
29
use File::Find;
29
use File::Find;
30
use File::Path qw( make_path );
30
use File::Path qw( make_path );
31
use File::Slurp;
31
use File::Slurp;
32
use File::Spec;
32
use File::Temp qw( tempdir );
33
use File::Temp qw( tempdir );
33
use Template::Parser;
34
use Template::Parser;
34
use PPI;
35
use PPI;
Lines 518-532 sub update_messages { Link Here
518
}
519
}
519
520
520
sub extract_messages_from_templates {
521
sub extract_messages_from_templates {
521
    my ($self, $tempdir, @files) = @_;
522
    my ($self, $tempdir, $type, @files) = @_;
522
523
523
    my $intranetdir = $self->{context}->config('intranetdir');
524
    my $htdocs = $type eq 'intranet' ? 'intrahtdocs' : 'opachtdocs';
525
    my $dir = $self->{context}->config($htdocs);
524
    my @keywords = qw(t tx tn txn tnx tp tpx tnp tnpx);
526
    my @keywords = qw(t tx tn txn tnx tp tpx tnp tnpx);
525
    my $parser = Template::Parser->new();
527
    my $parser = Template::Parser->new();
526
528
527
    foreach my $file (@files) {
529
    foreach my $file (@files) {
528
        say "Extract messages from $file" if $self->{verbose};
530
        say "Extract messages from $file" if $self->{verbose};
529
        my $template = read_file("$intranetdir/$file");
531
        my $template = read_file(File::Spec->catfile($dir, $file));
530
532
531
        # No need to process a file that doesn't use the i18n.inc file.
533
        # No need to process a file that doesn't use the i18n.inc file.
532
        next unless $template =~ /i18n\.inc/;
534
        next unless $template =~ /i18n\.inc/;
Lines 537-544 sub extract_messages_from_templates { Link Here
537
            next;
539
            next;
538
        }
540
        }
539
541
540
        make_path(dirname("$tempdir/$file"));
542
        my $destfile = $type eq 'intranet' ?
541
        open my $fh, '>', "$tempdir/$file";
543
            File::Spec->catfile($tempdir, 'koha-tmpl', 'intranet-tmpl', $file) :
544
            File::Spec->catfile($tempdir, 'koha-tmpl', 'opac-tmpl', $file);
545
546
        make_path(dirname($destfile));
547
        open my $fh, '>', $destfile;
542
548
543
        my @blocks = ($data->{BLOCK}, values %{ $data->{DEFBLOCKS} });
549
        my @blocks = ($data->{BLOCK}, values %{ $data->{DEFBLOCKS} });
544
        foreach my $block (@blocks) {
550
        foreach my $block (@blocks) {
Lines 605-639 sub extract_messages { Link Here
605
    say "Extract messages into POT file" if $self->{verbose};
611
    say "Extract messages into POT file" if $self->{verbose};
606
612
607
    my $intranetdir = $self->{context}->config('intranetdir');
613
    my $intranetdir = $self->{context}->config('intranetdir');
614
    my $opacdir = $self->{context}->config('opacdir');
615
616
    # Find common ancestor directory
617
    my @intranetdirs = File::Spec->splitdir($intranetdir);
618
    my @opacdirs = File::Spec->splitdir($opacdir);
619
    my @basedirs;
620
    while (@intranetdirs and @opacdirs) {
621
        my ($dir1, $dir2) = (shift @intranetdirs, shift @opacdirs);
622
        last if $dir1 ne $dir2;
623
        push @basedirs, $dir1;
624
    }
625
    my $basedir = File::Spec->catdir(@basedirs);
626
608
    my @files_to_scan;
627
    my @files_to_scan;
609
    my @directories_to_scan = ('.');
628
    my @directories_to_scan = ('.');
610
    my @blacklist = qw(blib koha-tmpl skel tmp t);
629
    my @blacklist = map { File::Spec->catdir(@intranetdirs, $_) } qw(blib koha-tmpl skel tmp t);
611
    while (@directories_to_scan) {
630
    while (@directories_to_scan) {
612
        my $dir = shift @directories_to_scan;
631
        my $dir = shift @directories_to_scan;
613
        opendir DIR, "$intranetdir/$dir" or die "Unable to open $dir: $!";
632
        opendir DIR, File::Spec->catdir($basedir, $dir) or die "Unable to open $dir: $!";
614
        foreach my $entry (readdir DIR) {
633
        foreach my $entry (readdir DIR) {
615
            next if $entry =~ /^\./;
634
            next if $entry =~ /^\./;
616
            my $relentry = "$dir/$entry";
635
            my $relentry = File::Spec->catfile($dir, $entry);
617
            $relentry =~ s|^\./||;
636
            my $abspath = File::Spec->catfile($basedir, $relentry);
618
            if (-d "$intranetdir/$relentry" and not grep /^$relentry$/, @blacklist) {
637
            if (-d $abspath and not grep /^$relentry$/, @blacklist) {
619
                push @directories_to_scan, "$relentry";
638
                push @directories_to_scan, $relentry;
620
            } elsif (-f "$intranetdir/$relentry" and $relentry =~ /(pl|pm)$/) {
639
            } elsif (-f $abspath and $relentry =~ /\.(pl|pm)$/) {
621
                push @files_to_scan, "$relentry";
640
                push @files_to_scan, $relentry;
622
            }
641
            }
623
        }
642
        }
624
    }
643
    }
625
644
626
    my @tt_files;
645
    my $intrahtdocs = $self->{context}->config('intrahtdocs');
646
    my $opachtdocs = $self->{context}->config('opachtdocs');
647
648
    my @intranet_tt_files;
649
    find(sub {
650
        if ($File::Find::dir =~ m|/en/| && $_ =~ m/\.(tt|inc)$/) {
651
            my $filename = $File::Find::name;
652
            $filename =~ s|^$intrahtdocs/||;
653
            push @intranet_tt_files, $filename;
654
        }
655
    }, $intrahtdocs);
656
657
    my @opac_tt_files;
627
    find(sub {
658
    find(sub {
628
        if ($File::Find::dir =~ m|/en/| && $_ =~ m/\.(tt|inc)$/) {
659
        if ($File::Find::dir =~ m|/en/| && $_ =~ m/\.(tt|inc)$/) {
629
            my $filename = $File::Find::name;
660
            my $filename = $File::Find::name;
630
            $filename =~ s|^$intranetdir/||;
661
            $filename =~ s|^$opachtdocs/||;
631
            push @tt_files, $filename;
662
            push @opac_tt_files, $filename;
632
        }
663
        }
633
    }, "$intranetdir/koha-tmpl");
664
    }, $opachtdocs);
634
665
635
    my $tempdir = tempdir('Koha-translate-XXXX', TMPDIR => 1, CLEANUP => 1);
666
    my $tempdir = tempdir('Koha-translate-XXXX', TMPDIR => 1, CLEANUP => 1);
636
    $self->extract_messages_from_templates($tempdir, @tt_files);
667
    $self->extract_messages_from_templates($tempdir, 'intranet', @intranet_tt_files);
668
    $self->extract_messages_from_templates($tempdir, 'opac', @opac_tt_files);
669
670
    @intranet_tt_files = map { File::Spec->catfile('koha-tmpl', 'intranet-tmpl', $_) } @intranet_tt_files;
671
    @opac_tt_files = map { File::Spec->catfile('koha-tmpl', 'opac-tmpl', $_) } @opac_tt_files;
672
    my @tt_files = grep { -e File::Spec->catfile($tempdir, $_) } @intranet_tt_files, @opac_tt_files;
673
637
    push @files_to_scan, @tt_files;
674
    push @files_to_scan, @tt_files;
638
675
639
    my $xgettext_cmd = "$self->{xgettext} --force-po -L Perl --from-code=UTF-8 "
676
    my $xgettext_cmd = "$self->{xgettext} --force-po -L Perl --from-code=UTF-8 "
Lines 641-647 sub extract_messages { Link Here
641
        . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 "
678
        . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 "
642
        . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ -kN__n:1,2 "
679
        . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ -kN__n:1,2 "
643
        . "-kN__p:1c,2 -kN__np:1c,2,3 "
680
        . "-kN__p:1c,2 -kN__np:1c,2,3 "
644
        . "-o $Bin/$self->{domain}.pot -D $tempdir -D $intranetdir";
681
        . "-o $Bin/$self->{domain}.pot -D $tempdir -D $basedir";
645
    $xgettext_cmd .= " $_" foreach (@files_to_scan);
682
    $xgettext_cmd .= " $_" foreach (@files_to_scan);
646
683
647
    if (system($xgettext_cmd) != 0) {
684
    if (system($xgettext_cmd) != 0) {
(-)a/t/LangInstaller.t (-7 / +7 lines)
Lines 15-29 use_ok('LangInstaller'); Link Here
15
my $installer = LangInstaller->new();
15
my $installer = LangInstaller->new();
16
16
17
my $tempdir = tempdir(CLEANUP => 0);
17
my $tempdir = tempdir(CLEANUP => 0);
18
t::lib::Mocks::mock_config('intranetdir', "$Bin/LangInstaller/templates");
18
t::lib::Mocks::mock_config('intrahtdocs', "$Bin/LangInstaller/templates");
19
my @files = ('simple.tt');
19
my @files = ('simple.tt');
20
$installer->extract_messages_from_templates($tempdir, @files);
20
$installer->extract_messages_from_templates($tempdir, 'intranet', @files);
21
21
22
ok(-e "$tempdir/simple.tt", 'it has created a temporary file simple.tt');
22
my $tempfile = "$tempdir/koha-tmpl/intranet-tmpl/simple.tt";
23
ok(-e $tempfile, 'it has created a temporary file simple.tt');
23
SKIP: {
24
SKIP: {
24
    skip "simple.tt does not exist", 37 unless -e "$tempdir/simple.tt";
25
    skip "simple.tt does not exist", 37 unless -e $tempfile;
25
26
26
    my $output = read_file("$tempdir/simple.tt");
27
    my $output = read_file($tempfile);
27
    my $expected_output = <<'EOF';
28
    my $expected_output = <<'EOF';
28
__('hello');
29
__('hello');
29
__x('hello {name}');
30
__x('hello {name}');
Lines 46-52 EOF Link Here
46
        . "--package-name=Koha --package-version='' "
47
        . "--package-name=Koha --package-version='' "
47
        . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 "
48
        . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 "
48
        . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 "
49
        . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 "
49
        . "-o $tempdir/Koha.pot -D $tempdir simple.tt";
50
        . "-o $tempdir/Koha.pot -D $tempdir koha-tmpl/intranet-tmpl/simple.tt";
50
51
51
    system($xgettext_cmd);
52
    system($xgettext_cmd);
52
    my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot");
53
    my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot");
53
- 

Return to bug 21895