| 1 | package Data::Localize::Gettext::Parser; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | has encoding => ( | |||||||||||||||||||||||||||
| 5 | is => 'ro', | ||||||||||||||||||||||||||||
| 6 | required => 1, | ||||||||||||||||||||||||||||
| 7 | ); | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | has use_fuzzy => ( | |||||||||||||||||||||||||||
| 10 | is => 'ro', | ||||||||||||||||||||||||||||
| 11 | required => 1, | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | has keep_empty => ( | |||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 11 | ▶ | sub parse_file { | ||||||||||||||||||||||||||
|
Subroutine
parse_file: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 11 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 11 | my $enc = ':encoding(' . $self->encoding . ')'; | |||||||||||||||||||||||||||
| 23 | 11 | ▶ | open(my $fh, "<$enc", $file) or die "Could not open $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 11 | my @block = (); | |||||||||||||||||||||||||||
| 26 | 11 | my %lexicons; | |||||||||||||||||||||||||||
| 27 | 11 | ▶ | while ( defined( my $line = <$fh> ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 74 | $line =~ s/[\015\012]*\z//; # fix CRLF issues | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 74 | ▶ | if ( $line =~ /^\s*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 18 | ▶ | $self->_process_block(\@block, \%lexicons) if @block; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 18 | @block = (); | |||||||||||||||||||||||||||
| 33 | 18 | next; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 56 | push @block, $line; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 11 | ▶ | $self->_process_block(\@block, \%lexicons) if @block; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 11 | return \%lexicons; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 25 | ▶ | sub _process_block { | ||||||||||||||||||||||||||
|
Subroutine
_process_block: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 45 | 25 | my ($self, $block, $lexicons) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 25 | my $msgid = q{}; | |||||||||||||||||||||||||||
| 48 | 25 | my $msgstr = q{}; | |||||||||||||||||||||||||||
| 49 | 25 | my $value; | |||||||||||||||||||||||||||
| 50 | 25 | my $is_fuzzy = 0; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Note that we are ignoring the various types of comments allowed in a .po | ||||||||||||||||||||||||||||
| 53 | # file - see | ||||||||||||||||||||||||||||
| 54 | # http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files for | ||||||||||||||||||||||||||||
| 55 | # more details. | ||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||
| 57 | # We do not handle the msgstr[0]/msgstr[1] type of string. | ||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | # Finally, we don't handle msgctxt at all. | ||||||||||||||||||||||||||||
| 60 | 25 | for my $line (@{$block} ) { | |||||||||||||||||||||||||||
| 61 | 56 | ▶ | if ( $line =~ /^msgid\s+"(.*)"\s*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 62 | 25 | $value = \$msgid; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 25 | ${$value} .= $1; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | elsif ( $line =~ /^msgstr\s+"(.*)"\s*$/ ) { | ||||||||||||||||||||||||||||
| 67 | 25 | $value = \$msgstr; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 25 | ${$value} .= $1; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | elsif ( $line =~ /^"(.*)"\s*$/ ) { | ||||||||||||||||||||||||||||
| 72 | 4 | ${$value} .= $1; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | elsif ( $line =~ /#,\s+.*fuzzy.*$/ ) { | ||||||||||||||||||||||||||||
| 75 | 2 | $is_fuzzy = 1; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 25 | ▶ | return unless length $msgstr || $self->keep_empty(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $msgstr or $self->keep_empty
MC/DC100%2/2
length $msgstr or $self->keep_empty
length $msgstr
$self->keep_empty
Truth table100%3/3
A or B
A= length $msgstrB= $self->keep_empty
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 24 | ▶ | return if $is_fuzzy && ! $self->use_fuzzy(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_fuzzy and not $self->use_fuzzy
MC/DC100%2/2
$is_fuzzy and not $self->use_fuzzy
$is_fuzzy
not $self->use_fuzzy
Truth table100%3/3
A and not(B)
A= $is_fuzzyB= not $self->use_fuzzy
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 23 | ▶ | s/\\(n|\\|\")/ $1 eq 'n' ? "\n" : $1 eq '\\' ? "\\" : '"' /ge for $msgid, $msgstr; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 23 | $lexicons->{$msgid} = $msgstr; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 23 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||