Koha/misc/translator/xgettext-pref
Julian Maurice 3cfc2ec7bd Bug 25067: Move PO file manipulation code into gulp tasks
misc/translator/translate was doing three different things:
- extract translatable strings
- create or update PO files
- install translated templates

This patch separates responsibilities by moving the string extraction
code into several 'xgettext-like' scripts and adds gulp tasks to
automate string extraction and PO files update

This has several benefits:

- gulp runs tasks in parallel, so it's a lot faster (updating all PO
  files is at least 10 times faster with my 4-cores CPU)

- there is no need for $KOHA_CONF to be defined
  LangInstaller.pm relied on $KOHA_CONF to get the different paths
  needed. I'm not sure why, since string extraction and PO update should
  work on source files, not installed files

- string extraction code can be more easily tested

This patch also brings a couple of fixes and improvements:

- TT string extraction (strings wrapped in [% t(...) %]) was done with
  Template::Parser and PPI, which was extremely slow, and had some
  problems (see bug 24797).
  This is now done with Locale::XGettext::TT2 (new dependency) which is
  a lot faster, and fixes bug 24797

- Fix header in 4 PO files

For backward compatibility, 'create' and 'update' commands of
misc/translator/translate can still be used and will execute the
corresponding gulp task

Test plan:
1. Run `yarn install` and install Locale::XGettext::TT2
2. Run `gulp po:update`
3. Verify the contents of updated PO files
4. Run `cd misc/translator && ./translate install <lang>`
5. Verify that all (templates, sysprefs, xslt, installer files) is
   correctly translated
6. Run `gulp po:create --lang <lang>` and verify that it created all PO
   files for that language
7. Run `prove t/misc/translator`

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Need to install yarn & gulp, no errors

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-11-06 09:46:11 +01:00

151 lines
3.9 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
=head1 NAME
xgettext-pref - extract translatable strings from system preferences YAML files
=head1 SYNOPSIS
xgettext-pref [OPTION] [INPUTFILE]...
=head1 OPTIONS
=over
=item B<-f, --files-from=FILE>
get list of input files from FILE
=item B<-o, --output=FILE>
write output to the specified file
=item B<-h, --help>
display this help and exit
=back
=cut
use Modern::Perl;
use File::Basename;
use Getopt::Long;
use Locale::PO;
use Pod::Usage;
use YAML::Syck qw(LoadFile);
$YAML::Syck::ImplicitTyping = 1;
my $output = 'messages.pot';
my $files_from;
my $help;
GetOptions(
'output=s' => \$output,
'files-from=s' => \$files_from,
'help' => \$help,
) or pod2usage(-verbose => 1, -exitval => 2);
if ($help) {
pod2usage(-verbose => 1, -exitval => 0);
}
my @files = @ARGV;
if ($files_from) {
open(my $fh, '<', $files_from) or die "Cannot open $files_from: $!";
push @files, <$fh>;
chomp @files;
close $fh;
}
my $pot = {
'' => Locale::PO->new(
-msgid => '',
-msgstr => "Project-Id-Version: Koha\n"
. "PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
. "Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n"
. "Language-Team: Koha Translate List <koha-translate\@lists.koha-community.org>\n"
. "MIME-Version: 1.0\n"
. "Content-Type: text/plain; charset=UTF-8\n"
. "Content-Transfer-Encoding: 8bit\n"
),
};
for my $file (@files) {
my $pref = LoadFile($file);
while ( my ($tab, $tab_content) = each %$pref ) {
add_po(undef, basename($file));
if ( ref($tab_content) eq 'ARRAY' ) {
add_prefs( $file, $tab, $tab_content );
} else {
while ( my ($section, $sysprefs) = each %$tab_content ) {
my $context = "$tab > $section";
my $msgid = sprintf('%s %s', basename($file), $section);
add_po($tab, $msgid);
add_prefs( $file, $context, $sysprefs );
}
}
}
}
Locale::PO->save_file_fromhash($output, $pot);
sub add_prefs {
my ($file, $context, $prefs) = @_;
for my $pref (@$prefs) {
my $pref_name = '';
for my $element (@$pref) {
if ( ref($element) eq 'HASH' ) {
$pref_name = $element->{pref};
last;
}
}
for my $element (@$pref) {
if ( ref($element) eq 'HASH' ) {
while ( my ( $key, $value ) = each(%$element) ) {
next unless $key eq 'choices' or $key eq 'multiple';
next unless ref($value) eq 'HASH';
for my $ckey ( keys %$value ) {
my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $value->{$ckey});
add_po( "$context > $pref_name", $msgid );
}
}
}
elsif ($element) {
my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $element);
add_po( "$context > $pref_name", $msgid );
}
}
}
}
sub add_po {
my ($comment, $msgid ) = @_;
return unless $msgid;
$pot->{$msgid} = Locale::PO->new(
-comment => $comment,
-msgid => $msgid,
-msgstr => '',
);
}