Skip to content

Commit

Permalink
Fix the --translate-only option
Browse files Browse the repository at this point in the history
I added a lot of verbose messages to understand what's going on,
rewrote a bit of code I didn't understant but was not recently
modified, and the bug introduced last month was gone.

At the end, I'm not sure of what happened, but this
fixes #482

Thanks Alexander for the diagnostic and the reproducer!
  • Loading branch information
mquinson committed Mar 1, 2024
1 parent d0a6861 commit b70d42a
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 25 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ __ __/ _ \|___ |___ \
\ V /| |_| | / / / __/
\_/ \___(_)_/ |_____| (unreleased)

po4a main script:
* Fix the behavior of --translate-only (GitHub's #482) [Mt & Golubev Alexander].

Documentation:
* Fix typos (GitHub's #478) [Marco Ciampa & Helge Kreutzmann]

Expand Down
78 changes: 57 additions & 21 deletions po4a
Original file line number Diff line number Diff line change
Expand Up @@ -1319,11 +1319,12 @@ while (<CONFIG>) {
}
if ( scalar @{ $po4a_opts{"partial"} } ) {
foreach my $file ( @{ $po4a_opts{"partial"} } ) {
if ( grep ( m/(\S+):\Q$file\E\b/, @split_args ) ) {
$partial{'lang'}{$1} = 1;
$partial{'master'}{$main} = 1;
$partial{'files'}{$file} = 1;
last;
foreach my $arg (@split_args) {
if ( $arg =~ m/(\S+):\Q$file\E\b/ ) {
$partial{'lang'}{$1} = 1;
$partial{'master'}{$main} = 1;
$partial{'files'}{$file} = 1;
}
}
}
}
Expand All @@ -1344,7 +1345,7 @@ while (<CONFIG>) {
$document{$main}{'format'} = $aliases{$1}{"module"};
if ( defined $aliases{$1}{"options"} ) {

# print STDERR "Override the global options with the alias ones\n";
# print STDERR "Override the global options with the alias ones\n";
%options = %{ $aliases{$1}{"options"} }; # XXX not a merge, but overwrite
}
}
Expand Down Expand Up @@ -1440,16 +1441,18 @@ while (<CONFIG>) {
}
}
} elsif ( $cmd =~ m/po4a_alias: *(.*)/ ) {
my $name = $1;
my $name = $1;
shift @split_args;

if ( defined $aliases{$name} ) { # If that alias was previously defined
die "Alias $name redefined from ".$aliases{$name}{"module"}." to $main\n"
if ( $aliases{$name}{"module"} ne $main ); # Sanity checks
# Concat the new content to the existing alias
@split_args = map { parse_config_options( "$config_file:$nb", $_, \%{ $aliases{$name}{"options"} } ); } @split_args;
} else {
# Otherwise, define a new option set accordingly
if ( defined $aliases{$name} ) { # If that alias was previously defined
die "Alias $name redefined from " . $aliases{$name}{"module"} . " to $main\n"
if ( $aliases{$name}{"module"} ne $main );

# Concat the new content to the existing alias after a sanity check
@split_args =
map { parse_config_options( "$config_file:$nb", $_, \%{ $aliases{$name}{"options"} } ); } @split_args;

} else { # Otherwise, define a new option set accordingly
my %alias = ();
$alias{"module"} = $main;

Expand Down Expand Up @@ -1530,13 +1533,33 @@ if ( $pot_filename =~ m/\$master/ ) {

# Skip documents not specified, strings are read directly from POT file
foreach my $master ( keys %document ) {
next unless length $master;
delete $document{$master} unless exists $partial{'master'}{$master};
next unless length $master;
if ( exists $partial{'master'}{$master} ) {
print wrap_msg( gettext("Document %s kept for update (--translate-only)."), $master )
if ( $po4a_opts{"verbose"} );
} else {
print wrap_msg( gettext("Skip the update of %s as requested (--translate-only)."), $master )
if ( $po4a_opts{"verbose"} );
delete $document{$master};
}
}

# Do not read PO files if no file is processed for this language
foreach my $lang ( keys %po_filename ) {
delete $po_filename{$lang} unless exists $partial{'lang'}{$lang};
my $skipped_langs = "";
my $kept_langs = "";
foreach my $lang ( sort keys %po_filename ) {
if ( exists $partial{'lang'}{$lang} ) {
$kept_langs .= " $lang";
} else {
$skipped_langs .= " $lang";
delete $po_filename{$lang};
}
}
if ( $po4a_opts{"verbose"} ) {
$skipped_langs =~ s/^ //;
$kept_langs =~ s/^ //;
print wrap_msg( gettext("Languages skipped because of --translate-only: %s; kept languages: %s."),
$skipped_langs, $kept_langs );
}
}

Expand Down Expand Up @@ -1654,8 +1677,16 @@ if ($update_pot_file) {
print wrap_msg( gettext(" (%d entries)"), $potfile->count_entries() )
unless ( $po4a_opts{"quiet"} );
} else {
print wrap_msg( gettext("POT file %s already up to date."), $pot_filename )
if ( $po4a_opts{"verbose"} and not $po4a_opts{"no-update"} );
if ( $po4a_opts{"no-update"} ) {
print wrap_msg( gettext("NOT updating the POT file %s as requested (--no-update)."), $pot_filename )
if ( $po4a_opts{"verbose"} );
} elsif ( scalar @{ $po4a_opts{"partial"} } ) {
print wrap_msg( gettext("NOT updating the POT file %s because of --translate-only."), $pot_filename )
if ( $po4a_opts{"verbose"} );
} else {
print wrap_msg( gettext("POT file %s already up to date."), $pot_filename )
if ( $po4a_opts{"verbose"} and not $po4a_opts{"no-update"} );
}
}

my %split_po; # po_files: '$lang','$master' => '$path'
Expand Down Expand Up @@ -1930,7 +1961,12 @@ if ( not $po4a_opts{"no-translations"} ) {
next if ( $master eq '' );
next unless defined $document{$master}{$lang};
if ( scalar @{ $po4a_opts{"partial"} } ) {
next unless defined $partial{'files'}{ $document{$master}{$lang} };
unless ( defined $partial{'files'}{ $document{$master}{$lang} } ) {
print wrap_msg( gettext("Skip master file %s in language %s because of --translate-only."),
$master, $lang )
if ( $po4a_opts{"verbose"} );
next;
}
}

unless ( $po4a_opts{"force"} ) {
Expand Down
2 changes: 1 addition & 1 deletion t/Testhelper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ sub run_one_po4aconf {
pass("Change directory back to $cwd");

my $expected_outfile = $t->{'expected_outfile'} // "$path/_output";
unless ( $t->{'diff_outfile'} ) {
unless ( -e $expected_outfile ) {
$expected_outfile = "$path/$expected_outfile"
if ( not -e $expected_outfile ) && ( -e "$path/$expected_outfile" );
unless ( -e $expected_outfile ) {
Expand Down
1 change: 1 addition & 0 deletions t/add/modifiers/_output
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating modifiers.pot as requested (--no-update).
NOT updating the POT file modifiers.pot as requested (--no-update).
with-1 is 100% translated (2 strings).
without-2 is 100% translated (2 strings).
without-3 is 100% translated (2 strings).
Expand Down
22 changes: 19 additions & 3 deletions t/cfg-multi.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use Testhelper;

my @tests;
push @tests, {
'doc' => 'Multiple languages, no pot no po',
'po4a.conf' => 'cfg/multiple-nopotpo/po4a.conf',
'closed_path' => 'cfg/*/', # Do not use or modify the other tests
'doc' => 'Multiple languages, no pot no po',
'po4a.conf' => 'cfg/multiple-nopotpo/po4a.conf',
'closed_path' => 'cfg/*/', # Do not use or modify the other tests
'expected_files' => 'multiple.de.po multiple.es.po multiple.fr.po multiple.it.po multiple.pot',
},
{
Expand Down Expand Up @@ -48,6 +48,22 @@ push @tests, {
. 'multiple.man.de.1 multiple.man.es.1 multiple.man.fr.1 multiple.man.it.1 multiple.pot',

},
{
'doc' => 'Multiple languages, translation uptodate, translate only one language',
'po4a.conf' => 'cfg/multiple-uptodate/po4a.conf',
'options' => ' --translate-only multiple.man.de.1',
'closed_path' => 'cfg/*/',
'expected_outfile' => 'cfg/multiple-uptodate/_output-only-one',
'expected_files' => 'multiple.man.de.1',
},
{
'doc' => 'Multiple languages, translation uptodate, translate only two languages',
'po4a.conf' => 'cfg/multiple-uptodate/po4a.conf',
'options' => ' --translate-only multiple.man.de.1 --translate-only multiple.man.es.1',
'closed_path' => 'cfg/*/',
'expected_outfile' => 'cfg/multiple-uptodate/_output-only-two',
'expected_files' => 'multiple.man.de.1 multiple.man.es.1',
},
{
'doc' => 'Multiple languages, translation already fuzzy',
'po4a.conf' => 'cfg/multiple-fuzzy/po4a.conf',
Expand Down
1 change: 1 addition & 0 deletions t/cfg/args-alias-redef/_output
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating args.pot as requested (--no-update).
NOT updating the POT file args.pot as requested (--no-update).
man.de.1 is 20% translated (1 of 5 strings).
Discard man.es.1 (3 of 5 strings; only 60% translated; need 70%).
man.fr.1 is 80% translated (4 of 5 strings).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/args-alias/_output
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating args.pot as requested (--no-update).
NOT updating the POT file args.pot as requested (--no-update).
man.de.1 is 20% translated (1 of 5 strings).
Discard man.de.2 (1 of 5 strings; only 20% translated; need 80%).
Discard man.es.1 (3 of 5 strings; only 60% translated; need 70%).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/args-alias/_output-keep100
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating args.pot as requested (--no-update).
NOT updating the POT file args.pot as requested (--no-update).
Discard man.de.1 (1 of 5 strings; only 20% translated; need 100%).
Discard man.de.2 (1 of 5 strings; only 20% translated; need 100%).
Discard man.es.1 (3 of 5 strings; only 60% translated; need 100%).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/args-global/_output
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating args.pot as requested (--no-update).
NOT updating the POT file args.pot as requested (--no-update).
man.de.1 is 20% translated (1 of 5 strings).
man.es.1 is 60% translated (3 of 5 strings).
man.fr.1 is 80% translated (4 of 5 strings).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/args-global/_output-keep100
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating args.pot as requested (--no-update).
NOT updating the POT file args.pot as requested (--no-update).
Discard man.de.1 (1 of 5 strings; only 20% translated; need 100%).
Discard man.es.1 (3 of 5 strings; only 60% translated; need 100%).
Discard man.fr.1 (4 of 5 strings; only 80% translated; need 100%).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/args-master/_output
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating args.pot as requested (--no-update).
NOT updating the POT file args.pot as requested (--no-update).
Discard man.de.1 (1 of 5 strings; only 20% translated; need 22%).
Discard man.es.1 (3 of 5 strings; only 60% translated; need 66%).
man.fr.1 is 80% translated (4 of 5 strings).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/args-master/_output-keep100
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT creating args.pot as requested (--no-update).
NOT updating the POT file args.pot as requested (--no-update).
Discard man.de.1 (1 of 5 strings; only 20% translated; need 100%).
Discard man.es.1 (3 of 5 strings; only 60% translated; need 100%).
Discard man.fr.1 (4 of 5 strings; only 80% translated; need 100%).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/multiple-fuzzied-noup/_output
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT updating multiple.pot as requested (--no-update).
NOT updating the POT file multiple.pot as requested (--no-update).
Discard multiple.man.de.1 (3 of 4 strings; only 75% translated; need 80%).
Discard multiple.man.es.1 (3 of 4 strings; only 75% translated; need 80%).
Discard multiple.man.fr.1 (3 of 4 strings; only 75% translated; need 80%).
Expand Down
1 change: 1 addition & 0 deletions t/cfg/multiple-nopo/_output-noupdate
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOT updating multiple.pot as requested (--no-update).
NOT updating the POT file multiple.pot as requested (--no-update).
PO file multiple.de.po for language de is missing -- skipping.
PO file multiple.es.po for language es is missing -- skipping.
PO file multiple.fr.po for language fr is missing -- skipping.
Expand Down
4 changes: 4 additions & 0 deletions t/cfg/multiple-uptodate/_output-only-one
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Document multiple.man.1 kept for update (--translate-only).
Languages skipped because of --translate-only: es fr it; kept languages: de.
NOT updating the POT file multiple.pot because of --translate-only.
multiple.man.de.1 is 100% translated (4 strings).
5 changes: 5 additions & 0 deletions t/cfg/multiple-uptodate/_output-only-two
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Document multiple.man.1 kept for update (--translate-only).
Languages skipped because of --translate-only: fr it; kept languages: de es.
NOT updating the POT file multiple.pot because of --translate-only.
multiple.man.de.1 is 100% translated (4 strings).
multiple.man.es.1 is 100% translated (4 strings).
1 change: 1 addition & 0 deletions t/cfg/single-fuzzied-noup/_output
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NOT updating single-fuzzied-noup.pot as requested (--no-update).
NOT updating the POT file single-fuzzied-noup.pot as requested (--no-update).
Discard single-fuzzied-noup.man.fr.1 (3 of 4 strings; only 75% translated; need 80%).
1 change: 1 addition & 0 deletions t/cfg/single-nopo/_output-noupdate
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NOT updating single.pot as requested (--no-update).
NOT updating the POT file single.pot as requested (--no-update).
PO file single.fr.po for language fr is missing -- skipping.

0 comments on commit b70d42a

Please sign in to comment.