From 1860f4b0b52e1e94d4289a0847b098d69fe2e72b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 28 Nov 2012 11:04:35 +0100 Subject: [PATCH] Bug 9161: Translate only specific files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous version of tmpl_process3.pl says in the pod: -i, --input=SOURCE Get or update strings from SOURCE file. SOURCE is a directory if -r is also specified. But is was not possible to specify one or more files. This patch allows us to give one or more specific file with the -f parameter. for eg.: perl tmpl_process3.pl install -i /home/koha/koha-tmpl/opac-tmpl/prog/en/modules/ -o /home/koha/koha-tmpl/opac-tmpl/prog/fr-FR/modules/ -s /home/koha/misc/translator/po/fr-FR-i-opac-t-prog-v-3006000.po -r -f opac-account.tt -f opac-main.tt You can specify: -f advsearch.tt => translate all files with a filename containing 'advsearch.tt' or -f search => will translate acqui/histsearch.tt, acqui/z3950_search.tt, etc. Bug 9161: Followup: Add a -f param for the translate script Now you can directly call the translate script (misc/translator/translate) with the -f parameter eg.: ./translate install|create|update -f search.tt -f main.tt Signed-off-by: Frédéric Demians I've squashed the 3 patches, and reported doc into 'translate' script. It works as advertised. Side note: It would be great to extend this functionnality in order to be able to apply the translation to XSL files stored outside Koha directories hierarchy. Useful to translate site-specific XSLs defined with XSLTResultsDisplay, and other sysprefs. Signed-off-by: Chris Cormack --- misc/translator/LangInstaller.pm | 39 ++++++++++++------ misc/translator/tmpl_process3.pl | 87 +++++++++++++++++++--------------------- misc/translator/translate | 12 ++++-- 3 files changed, 78 insertions(+), 60 deletions(-) diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm index e5371e6..835f75f 100644 --- a/misc/translator/LangInstaller.pm +++ b/misc/translator/LangInstaller.pm @@ -321,7 +321,7 @@ sub install_prefs { sub install_tmpl { - my $self = shift; + my ($self, $files) = @_; say "Install templates" if $self->{verbose}; for my $trans ( @{$self->{interface}} ) { print @@ -336,13 +336,18 @@ sub install_tmpl { "$self->{process} install " . "-i $trans->{dir}/en/ " . "-o $trans->{dir}/$self->{lang} ". - "-s $self->{path_po}/$self->{lang}$trans->{suffix} -r" + "-s $self->{path_po}/$self->{lang}$trans->{suffix} -r " . + ( + $files + ? '-f ' . join ' -f ', @$files + : '' + ) } } sub update_tmpl { - my $self = shift; + my ($self, $files) = @_; say "Update templates" if $self->{verbose}; for my $trans ( @{$self->{interface}} ) { @@ -356,7 +361,12 @@ sub update_tmpl { system "$self->{process} update " . "-i $trans->{dir}/en/ " . - "-s $self->{path_po}/$self->{lang}$trans->{suffix} -r" + "-s $self->{path_po}/$self->{lang}$trans->{suffix} -r " . + ( + $files + ? '-f ' . join ' -f ', @$files + : '' + ) } } @@ -374,7 +384,7 @@ sub create_prefs { sub create_tmpl { - my $self = shift; + my ($self, $files) = @_; say "Create templates\n" if $self->{verbose}; for my $trans ( @{$self->{interface}} ) { @@ -386,15 +396,20 @@ sub create_tmpl { system "$self->{process} create " . "-i $trans->{dir}/en/ " . - "-s $self->{path_po}/$self->{lang}$trans->{suffix} -r" + "-s $self->{path_po}/$self->{lang}$trans->{suffix} -r " . + ( + $files + ? '-f ' . join ' -f ', @$files + : '' + ) } } sub install { - my $self = shift; + my ($self, $files) = @_; return unless $self->{lang}; - $self->install_tmpl() unless $self->{pref_only}; + $self->install_tmpl($files) unless $self->{pref_only}; $self->install_prefs(); } @@ -409,20 +424,20 @@ sub get_all_langs { sub update { - my $self = shift; + my ($self, $files) = @_; my @langs = $self->{lang} ? ($self->{lang}) : $self->get_all_langs(); for my $lang ( @langs ) { $self->set_lang( $lang ); - $self->update_tmpl() unless $self->{pref_only}; + $self->update_tmpl($files) unless $self->{pref_only}; $self->update_prefs(); } } sub create { - my $self = shift; + my ($self, $files) = @_; return unless $self->{lang}; - $self->create_tmpl() unless $self->{pref_only}; + $self->create_tmpl($files) unless $self->{pref_only}; $self->create_prefs(); } diff --git a/misc/translator/tmpl_process3.pl b/misc/translator/tmpl_process3.pl index 993e2a8..5d3b1af 100755 --- a/misc/translator/tmpl_process3.pl +++ b/misc/translator/tmpl_process3.pl @@ -13,6 +13,7 @@ using gettext-compatible translation files use strict; #use warnings; FIXME - Bug 2505 +use File::Basename; use Getopt::Long; use Locale::PO; use File::Temp qw( :POSIX ); @@ -21,7 +22,7 @@ use VerboseWarnings qw( :warn :die ); ############################################################################### -use vars qw( @in_files $in_dir $str_file $out_dir $quiet ); +use vars qw( $in_dir @filenames $str_file $out_dir $quiet ); use vars qw( @excludes $exclude_regex ); use vars qw( $recursive_p ); use vars qw( $pedantic_p ); @@ -131,25 +132,28 @@ sub text_replace (**) { } } -sub listfiles ($$$) { - my($dir, $type, $action) = @_; +sub listfiles { + my($dir, $type, $action, $filenames) = @_; my @it = (); if (opendir(DIR, $dir)) { - my @dirent = readdir DIR; # because DIR is shared when recursing - closedir DIR; - for my $dirent (@dirent) { - my $path = "$dir/$dirent"; - if ($dirent =~ /^\./ || $dirent eq 'CVS' || $dirent eq 'RCS' - || (defined $exclude_regex && $dirent =~ /^(?:$exclude_regex)$/)) { - ; - } elsif (-f $path) { - push @it, $path if (!defined $type || $dirent =~ /\.(?:$type)$/) || $action eq 'install'; - } elsif (-d $path && $recursive_p) { - push @it, listfiles($path, $type, $action); + my @dirent = readdir DIR; # because DIR is shared when recursing + closedir DIR; + for my $dirent (@dirent) { + my $path = "$dir/$dirent"; + if ($dirent =~ /^\./ || $dirent eq 'CVS' || $dirent eq 'RCS' + || (defined $exclude_regex && $dirent =~ /^(?:$exclude_regex)$/)) { + ; + } elsif (-f $path) { + my $basename = basename $path; + push @it, $path + if ( not @$filenames or ( grep { $basename =~ /$_/ } @$filenames ) ) + and (!defined $type || $dirent =~ /\.(?:$type)$/) || $action eq 'install'; + } elsif (-d $path && $recursive_p) { + push @it, listfiles($path, $type, $action, $filenames); + } } - } } else { - warn_normal "$dir: $!", undef; + warn_normal "$dir: $!", undef; } return @it; } @@ -181,12 +185,13 @@ Usage: $0 create [OPTION] or: $0 --help Create or update PO files from templates, or install translated templates. - -i, --input=SOURCE Get or update strings from SOURCE file. - SOURCE is a directory if -r is also specified. + -i, --input=SOURCE Get or update strings from SOURCE directory. -o, --outputdir=DIRECTORY Install translation(s) to specified DIRECTORY --pedantic-warnings Issue warnings even for detected problems which are likely to be harmless -r, --recursive SOURCE in the -i option is a directory + -f, --filename=FILE FILE is a specific filaneme. + If given, only these files will be processed. -s, --str-file=FILE Specify FILE as the translation (po) file for input (install) or output (create, update) -x, --exclude=REGEXP Exclude files matching the given REGEXP @@ -212,7 +217,8 @@ sub usage_error (;$) { ############################################################################### GetOptions( - 'input|i=s' => \@in_files, + 'input|i=s' => \$in_dir, + 'filename|f=s' => \@filenames, 'outputdir|o=s' => \$out_dir, 'recursive|r' => \$recursive_p, 'str-file|s=s' => \$str_file, @@ -233,36 +239,29 @@ $SIG{__WARN__} = sub { my $action = shift or usage_error('You must specify an ACTION.'); usage_error('You must at least specify input and string list filenames.') - if !@in_files || !defined $str_file; + if !$in_dir || !defined $str_file; # Type match defaults to *.tt plus *.inc if not specified $type = "tt|inc|xsl|xml" if !defined($type); -# Check the inputs for being files or directories -for my $input (@in_files) { - usage_error("$input: Input must be a file or directory.\n" - . "(Symbolic links are not supported at the moment)") - unless -d $input || -f $input;; -} +# Check the inputs for being directories +usage_error("$in_dir: Input must be a directory.\n" + . "(Symbolic links are not supported at the moment)") + unless -d $in_dir; # Generates the global exclude regular expression $exclude_regex = '(?:'.join('|', @excludes).')' if @excludes; +my @in_files; # Generate the list of input files if a directory is specified -if (-d $in_files[0]) { - die "If you specify a directory as input, you must specify only it.\n" - if @in_files > 1; - - # input is a directory, generates list of files to process - $in_dir = $in_files[0]; - $in_dir =~ s/\/$//; # strips the trailing / if any - @in_files = listfiles($in_dir, $type, $action); -} else { - for my $input (@in_files) { +# input is a directory, generates list of files to process +$in_dir =~ s/\/$//; # strips the trailing / if any + +for my $fn ( @filenames ) { die "You cannot specify input files and directories at the same time.\n" - unless -f $input; - } + if -d $fn; } +@in_files = listfiles($in_dir, $type, $action, \@filenames); # restores the string list from file $href = Locale::PO->load_file_ashash($str_file); @@ -405,23 +404,21 @@ if ($action eq 'create') { for my $input (@in_files) { die "Assertion failed" unless substr($input, 0, length($in_dir) + 1) eq "$in_dir/"; -# print "$input / $type\n"; + + my $target = $out_dir . substr($input, length($in_dir)); + my $targetdir = $` if $target =~ /[^\/]+$/s; + if (!defined $type || $input =~ /\.(?:$type)$/) { my $h = TmplTokenizer->new( $input ); $h->set_allow_cformat( 1 ); VerboseWarnings::set_input_file_name $input; - - my $target = $out_dir . substr($input, length($in_dir)); - my $targetdir = $` if $target =~ /[^\/]+$/s; mkdir_recursive($targetdir) unless -d $targetdir; print STDERR "Creating $target...\n" unless $quiet; - open( OUTPUT, ">$target" ) || die "$target: $!\n"; + open( OUTPUT, ">$target" ) || die "$target: $!\n"; text_replace( $h, *OUTPUT ); close OUTPUT; } else { # just copying the file - my $target = $out_dir . substr($input, length($in_dir)); - my $targetdir = $` if $target =~ /[^\/]+$/s; mkdir_recursive($targetdir) unless -d $targetdir; system("cp -f $input $target"); print STDERR "Copying $input...\n" unless $quiet; diff --git a/misc/translator/translate b/misc/translator/translate index de14401..8c13207 100755 --- a/misc/translator/translate +++ b/misc/translator/translate @@ -30,9 +30,11 @@ use Pod::Usage; my $verbose = 0; my $pref = 0; my $all = 0; +my @files; GetOptions( 'v|verbose' => \$verbose, 'p' => \$pref, + 'f:s' => \@files, 'a|all' => \$all, ); @@ -57,11 +59,11 @@ if ( $cmd =~ /create|install|update/ ) { usage() if $cmd eq 'create'; for my $lang ( @{$installer->{langs}} ) { $installer->set_lang( $lang ); - $installer->$cmd(); + $installer->$cmd(\@files); } } else { - $installer->$cmd(); + $installer->$cmd(\@files); } } else { @@ -79,6 +81,7 @@ translate - Handle templates and preferences translation translate create fr-FR translate update fr-FR translate install fr-FR + translate install fr-FR -f search -f memberentry translate -p install fr-FR translate install @@ -127,13 +130,16 @@ Update .po files in F directory, named F-*.po. Without F, all available languages are updated. With -p option, only preferences .po file is updated. -=item translate [-p] install F +=item translate [-p|-f] install F Use .po files to translate the english version of templates and preferences files and copy those files in the appropriate directory. Without F, all available languages are installed. With -p option, only preferences .po file is updated. +With -f parameter (repeatable) you can specify specific files to translate. For +example, -f search will translate all templates containing 'search'. + =back =cut -- 1.8.1.2