From e875e3980605f406bb62fbc5f2eacb9ff5c7c0de Mon Sep 17 00:00:00 2001 From: Bernardo Gonzalez Kriegel Date: Tue, 10 Dec 2019 21:02:41 -0300 Subject: [PATCH] Bug 24211: Compress/uncompress translation files This patch adds the ability to compress/uncompress translations files. On update/install the files are uncompressed first The only gain is to use less space. To test: 1) Apply the patch 2) Go to misc/translator 3) Try it $ ./translate compress fr-FR (check po/*.gz) $ ./translate uncompress fr-FR ( check normal files) 4) Try again with verbose mode $ ./translate compress fr-FR -v (list compressed files) $ ./translate uncompress fr-FR -v (list uncompressed files) 5) Try update compressed files $ ./translate compress fr-FR $ ./translate update fr-FR (result is uncompressed) 6) Try install compressed files $ ./translate compress fr-FR $ ./translate install fr-FR 7) Try compress all $ ./translate compress (add '-v' for verbose output) 8) Try uncompress all $ ./translate uncompress (add '-v' for verbose output) Signed-off-by: David Nind Signed-off-by: Bouzid Fergani Signed-off-by: Jonathan Druart Signed-off-by: Martin Renvoize --- misc/translator/LangInstaller.pm | 38 ++++++++++++++++++++++++++++++-- misc/translator/translate | 15 ++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm index 8a7c5abcfa..13367b5c54 100644 --- a/misc/translator/LangInstaller.pm +++ b/misc/translator/LangInstaller.pm @@ -82,12 +82,16 @@ sub new { $self->{xgettext} = `which xgettext`; $self->{sed} = `which sed`; $self->{po2json} = "$Bin/po2json"; + $self->{gzip} = `which gzip`; + $self->{gunzip} = `which gunzip`; chomp $self->{cp}; chomp $self->{msgmerge}; chomp $self->{msgfmt}; chomp $self->{msginit}; chomp $self->{xgettext}; chomp $self->{sed}; + chomp $self->{gzip}; + chomp $self->{gunzip}; unless ($self->{xgettext}) { die "Missing 'xgettext' executable. Have you installed the gettext package?\n"; @@ -766,9 +770,38 @@ sub remove_pot { unlink "$Bin/$self->{domain}-js.pot"; } +sub compress { + my ($self, $files) = @_; + my @langs = $self->{lang} ? ($self->{lang}) : $self->get_all_langs(); + for my $lang ( @langs ) { + $self->set_lang( $lang ); + opendir( my $dh, $self->{path_po} ); + my @files = grep { $_ =~ /^$self->{lang}.*po$/ } readdir $dh; + foreach my $file ( @files ) { + say "Compress file $file" if $self->{verbose}; + system "$self->{gzip} -9 $self->{path_po}/$file"; + } + } +} + +sub uncompress { + my ($self, $files) = @_; + my @langs = $self->{lang} ? ($self->{lang}) : $self->get_all_langs(); + for my $lang ( @langs ) { + opendir( my $dh, $self->{path_po} ); + $self->set_lang( $lang ); + my @files = grep { $_ =~ /^$self->{lang}.*po.gz$/ } readdir $dh; + foreach my $file ( @files ) { + say "Uncompress file $file" if $self->{verbose}; + system "$self->{gunzip} $self->{path_po}/$file"; + } + } +} + sub install { my ($self, $files) = @_; return unless $self->{lang}; + $self->uncompress(); $self->install_tmpl($files) unless $self->{pref_only}; $self->install_prefs(); $self->install_messages(); @@ -779,9 +812,9 @@ sub install { sub get_all_langs { my $self = shift; opendir( my $dh, $self->{path_po} ); - my @files = grep { $_ =~ /-pref.po$/ } + my @files = grep { $_ =~ /-pref.(po|po.gz)$/ } readdir $dh; - @files = map { $_ =~ s/-pref.po$//; $_ } @files; + @files = map { $_ =~ s/-pref.(po|po.gz)$//; $_ } @files; } @@ -790,6 +823,7 @@ sub update { my @langs = $self->{lang} ? ($self->{lang}) : $self->get_all_langs(); for my $lang ( @langs ) { $self->set_lang( $lang ); + $self->uncompress(); $self->update_tmpl($files) unless $self->{pref_only}; $self->update_prefs(); $self->update_messages(); diff --git a/misc/translator/translate b/misc/translator/translate index 8683afd7db..bab716de43 100755 --- a/misc/translator/translate +++ b/misc/translator/translate @@ -52,7 +52,7 @@ usage() if $#ARGV != 1 && $#ARGV != 0; my ($cmd, $lang) = @ARGV; $cmd = lc $cmd; -if ( $cmd =~ /create|install|update/ ) { +if ( $cmd =~ /^(create|install|update|compress|uncompress)$/ ) { my $installer = LangInstaller->new( $lang, $pref, $verbose ); if ( $cmd ne 'create' and $lang and not grep( {$_ eq $lang} @{ $installer->{langs} } ) ) { print "Unsupported language: $lang\n"; @@ -87,6 +87,8 @@ translate - Handle templates and preferences translation translate install fr-FR -f search -f memberentry translate -p install fr-FR translate install + translate compress [fr-FR] + translate uncompress [fr-FR] =head1 DESCRIPTION @@ -143,6 +145,17 @@ updated. With -f parameter (repeatable) you can specify specific files to translate. For example, -f search will translate all templates containing 'search'. +=item translate compress F + +Compress .po files in F directory, named F-*.po. Without F, files +from all available languages are compressed. + +=item translate uncompress F + +Uncompress .po.gz files in F directory, named F-*.po.gz. Without F, +files from all available languages are uncompressed. + + =back =cut -- 2.39.5