Skip to content

Commit

Permalink
Set forgiving encoding fallback when parsing Po file
Browse files Browse the repository at this point in the history
Before reading the expected encoding from the file itself, we first
try to read it as UTF-8 so we can at least get to read the line that
specifies the encoding.

For some reason, $PerlIO::encoding::fallback is set to FB_CROAK when
we reach this part of the code so the program would instead exit
at the first unknown character.

Unless we were requested a specific encoding, do not even print any
warning about it.
  • Loading branch information
aleasto committed Sep 2, 2024
1 parent 76a463e commit b00fa0b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/Locale/Po4a/Po.pm
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ sub read {
my $filename = shift
or croak wrap_mod( "po4a::po", dgettext( "po4a", "Please provide a non-null filename" ) );

my $charset = shift // 'UTF-8';
my $charset = shift;
my $did_request_charset = defined($charset);
$charset //= 'UTF-8';
$charset = 'UTF-8' if $charset eq "CHARSET";
warn "Read $filename with encoding: $charset" if $debug{'encoding'};

Expand All @@ -340,6 +342,12 @@ sub read {
unless ( $? == 0 );
}

# Allow reading with the wrong encoding.
# We will read the expected encoding from the file itself later.
use Encode qw(:fallback_all);
use PerlIO::encoding;
$PerlIO::encoding::fallback = $did_request_charset ? WARN_ON_ERR : FB_DEFAULT;

my $fh;
if ( $filename eq '-' ) {
$fh = *STDIN;
Expand Down

0 comments on commit b00fa0b

Please sign in to comment.