From bd81f0418c14af82cdd9f4858c32b18d76a90b49 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 28 Nov 2018 00:51:45 +0100 Subject: [PATCH] Bug 21895: Fix translation for package install The string extraction process was not taking into account the fact that standard/package install have a completely different directory structure than the dev install This patch tries to keep the exact same behaviour for dev installs, while making it work for standard install by using opachtdocs, intrahtdocs, opacdir and intranetdir from $KOHA_CONF Test plan: 1. Follow test plan in https://gitlab.com/koha-community/Koha/commit/d708255c7a4d981c7c7bdd0644a75202ec43b297 2. Do a standard install and repeat step 1 on this new install 3. If you know how to build the Debian package, build it, install it and verify that koha-translate works as expected 4. prove t/LangInstaller.t Signed-off-by: Mirko Tietgen Signed-off-by: Martin Renvoize Signed-off-by: Nick Clemens --- misc/translator/LangInstaller.pm | 75 ++++++++++++++++++++++++-------- t/LangInstaller.t | 13 +++--- 2 files changed, 63 insertions(+), 25 deletions(-) diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm index 5e0137b45c..333a148eba 100644 --- a/misc/translator/LangInstaller.pm +++ b/misc/translator/LangInstaller.pm @@ -29,6 +29,7 @@ use File::Basename; use File::Find; use File::Path qw( make_path ); use File::Slurp; +use File::Spec; use File::Temp qw( tempdir ); use Template::Parser; use PPI; @@ -518,15 +519,16 @@ sub update_messages { } sub extract_messages_from_templates { - my ($self, $tempdir, @files) = @_; + my ($self, $tempdir, $type, @files) = @_; - my $intranetdir = $self->{context}->config('intranetdir'); + my $htdocs = $type eq 'intranet' ? 'intrahtdocs' : 'opachtdocs'; + my $dir = $self->{context}->config($htdocs); my @keywords = qw(t tx tn txn tnx tp tpx tnp tnpx); my $parser = Template::Parser->new(); foreach my $file (@files) { say "Extract messages from $file" if $self->{verbose}; - my $template = read_file("$intranetdir/$file"); + my $template = read_file(File::Spec->catfile($dir, $file)); # No need to process a file that doesn't use the i18n.inc file. next unless $template =~ /i18n\.inc/; @@ -537,8 +539,12 @@ sub extract_messages_from_templates { next; } - make_path(dirname("$tempdir/$file")); - open my $fh, '>', "$tempdir/$file"; + my $destfile = $type eq 'intranet' ? + File::Spec->catfile($tempdir, 'koha-tmpl', 'intranet-tmpl', $file) : + File::Spec->catfile($tempdir, 'koha-tmpl', 'opac-tmpl', $file); + + make_path(dirname($destfile)); + open my $fh, '>', $destfile; my @blocks = ($data->{BLOCK}, values %{ $data->{DEFBLOCKS} }); foreach my $block (@blocks) { @@ -605,35 +611,66 @@ sub extract_messages { say "Extract messages into POT file" if $self->{verbose}; my $intranetdir = $self->{context}->config('intranetdir'); + my $opacdir = $self->{context}->config('opacdir'); + + # Find common ancestor directory + my @intranetdirs = File::Spec->splitdir($intranetdir); + my @opacdirs = File::Spec->splitdir($opacdir); + my @basedirs; + while (@intranetdirs and @opacdirs) { + my ($dir1, $dir2) = (shift @intranetdirs, shift @opacdirs); + last if $dir1 ne $dir2; + push @basedirs, $dir1; + } + my $basedir = File::Spec->catdir(@basedirs); + my @files_to_scan; my @directories_to_scan = ('.'); - my @blacklist = qw(blib koha-tmpl skel tmp t); + my @blacklist = map { File::Spec->catdir(@intranetdirs, $_) } qw(blib koha-tmpl skel tmp t); while (@directories_to_scan) { my $dir = shift @directories_to_scan; - opendir DIR, "$intranetdir/$dir" or die "Unable to open $dir: $!"; + opendir DIR, File::Spec->catdir($basedir, $dir) or die "Unable to open $dir: $!"; foreach my $entry (readdir DIR) { next if $entry =~ /^\./; - my $relentry = "$dir/$entry"; - $relentry =~ s|^\./||; - if (-d "$intranetdir/$relentry" and not grep /^$relentry$/, @blacklist) { - push @directories_to_scan, "$relentry"; - } elsif (-f "$intranetdir/$relentry" and $relentry =~ /(pl|pm)$/) { - push @files_to_scan, "$relentry"; + my $relentry = File::Spec->catfile($dir, $entry); + my $abspath = File::Spec->catfile($basedir, $relentry); + if (-d $abspath and not grep /^$relentry$/, @blacklist) { + push @directories_to_scan, $relentry; + } elsif (-f $abspath and $relentry =~ /\.(pl|pm)$/) { + push @files_to_scan, $relentry; } } } - my @tt_files; + my $intrahtdocs = $self->{context}->config('intrahtdocs'); + my $opachtdocs = $self->{context}->config('opachtdocs'); + + my @intranet_tt_files; + find(sub { + if ($File::Find::dir =~ m|/en/| && $_ =~ m/\.(tt|inc)$/) { + my $filename = $File::Find::name; + $filename =~ s|^$intrahtdocs/||; + push @intranet_tt_files, $filename; + } + }, $intrahtdocs); + + my @opac_tt_files; find(sub { if ($File::Find::dir =~ m|/en/| && $_ =~ m/\.(tt|inc)$/) { my $filename = $File::Find::name; - $filename =~ s|^$intranetdir/||; - push @tt_files, $filename; + $filename =~ s|^$opachtdocs/||; + push @opac_tt_files, $filename; } - }, "$intranetdir/koha-tmpl"); + }, $opachtdocs); my $tempdir = tempdir('Koha-translate-XXXX', TMPDIR => 1, CLEANUP => 1); - $self->extract_messages_from_templates($tempdir, @tt_files); + $self->extract_messages_from_templates($tempdir, 'intranet', @intranet_tt_files); + $self->extract_messages_from_templates($tempdir, 'opac', @opac_tt_files); + + @intranet_tt_files = map { File::Spec->catfile('koha-tmpl', 'intranet-tmpl', $_) } @intranet_tt_files; + @opac_tt_files = map { File::Spec->catfile('koha-tmpl', 'opac-tmpl', $_) } @opac_tt_files; + my @tt_files = grep { -e File::Spec->catfile($tempdir, $_) } @intranet_tt_files, @opac_tt_files; + push @files_to_scan, @tt_files; my $xgettext_cmd = "$self->{xgettext} --force-po -L Perl --from-code=UTF-8 " @@ -641,7 +678,7 @@ sub extract_messages { . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 " . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ -kN__n:1,2 " . "-kN__p:1c,2 -kN__np:1c,2,3 " - . "-o $Bin/$self->{domain}.pot -D $tempdir -D $intranetdir"; + . "-o $Bin/$self->{domain}.pot -D $tempdir -D $basedir"; $xgettext_cmd .= " $_" foreach (@files_to_scan); if (system($xgettext_cmd) != 0) { diff --git a/t/LangInstaller.t b/t/LangInstaller.t index 4075791c1a..6e122b368e 100644 --- a/t/LangInstaller.t +++ b/t/LangInstaller.t @@ -15,15 +15,16 @@ use_ok('LangInstaller'); my $installer = LangInstaller->new(); my $tempdir = tempdir(CLEANUP => 0); -t::lib::Mocks::mock_config('intranetdir', "$Bin/LangInstaller/templates"); +t::lib::Mocks::mock_config('intrahtdocs', "$Bin/LangInstaller/templates"); my @files = ('simple.tt'); -$installer->extract_messages_from_templates($tempdir, @files); +$installer->extract_messages_from_templates($tempdir, 'intranet', @files); -ok(-e "$tempdir/simple.tt", 'it has created a temporary file simple.tt'); +my $tempfile = "$tempdir/koha-tmpl/intranet-tmpl/simple.tt"; +ok(-e $tempfile, 'it has created a temporary file simple.tt'); SKIP: { - skip "simple.tt does not exist", 37 unless -e "$tempdir/simple.tt"; + skip "simple.tt does not exist", 37 unless -e $tempfile; - my $output = read_file("$tempdir/simple.tt"); + my $output = read_file($tempfile); my $expected_output = <<'EOF'; __('hello'); __x('hello {name}'); @@ -46,7 +47,7 @@ EOF . "--package-name=Koha --package-version='' " . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 " . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 " - . "-o $tempdir/Koha.pot -D $tempdir simple.tt"; + . "-o $tempdir/Koha.pot -D $tempdir koha-tmpl/intranet-tmpl/simple.tt"; system($xgettext_cmd); my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot"); -- 2.39.5