File Coverage

blib/lib/Locale/Msgfmt/po.pm
Criterion Covered Total %
statement 73 79 92.4
branch 30 34 88.2
condition 8 11 72.7
subroutine 9 9 100.0
pod 0 5 0.0
total 120 138 86.9


line stmt bran cond sub pod time code
1             package Locale::Msgfmt::po;
2              
3 4     4   63 use 5.008005;
  4         12  
  4         160  
4 4     4   18 use strict;
  4         6  
  4         128  
5 4     4   25 use warnings;
  4         10  
  4         151  
6 4     4   23 use Locale::Msgfmt::Utils ();
  4         6  
  4         3901  
7              
8             our $VERSION = '0.15';
9              
10             sub new {
11 15     15 0 26 my $class = shift;
12 15   50     62 return bless shift || {}, $class;
13             }
14              
15             sub cleanup_string {
16 893     893 0 1220 my $str = shift;
17 893         1137 $str =~ s/\\n/\n/g;
18 893         969 $str =~ s/\\r/\r/g;
19 893         1213 $str =~ s/\\t/\t/g;
20 893         945 $str =~ s/\\"/"/g;
21 893         994 $str =~ s/\\\\/\\/g;
22 893         2874 return $str;
23             }
24              
25             sub add_string {
26 468     468 0 679 my $self = shift;
27 468         484 my $hash = shift;
28 468         454 my %h = %{$hash};
  468         1448  
29 468 100 66     2247 return if !( defined( $h{msgid} ) && defined( $h{msgstr} ) );
30 462 100 100     1110 return if ( $h{fuzzy} && !$self->{fuzzy} && length( $h{msgid} ) > 0 );
      66        
31 453         1086 my $msgstr = join Locale::Msgfmt::Utils::null(), @{ $h{msgstr} };
  453         1008  
32 453 100       1055 return if ( $msgstr eq "" );
33 445         423 my $context;
34             my $plural;
35              
36 445 100       670 if ( $h{msgctxt} ) {
37 2         6 $context = cleanup_string( $h{msgctxt} ) . Locale::Msgfmt::Utils::eot();
38             } else {
39 443         516 $context = "";
40             }
41 445 100       684 if ( $h{msgid_plural} ) {
42 1         4 $plural = Locale::Msgfmt::Utils::null() . cleanup_string( $h{msgid_plural} );
43             } else {
44 444         479 $plural = "";
45             }
46 445         926 $self->{mo}->add_string( $context . cleanup_string( $h{msgid} ) . $plural, cleanup_string($msgstr) );
47             }
48              
49             sub read_po {
50 15     15 0 19 my $self = shift;
51 15         18 my $pofile = shift;
52 15         23 my $mo = $self->{mo};
53 15 50       584 open my $F, '<', $pofile or die "Could not open ($pofile) $!";
54 15         28 my %h = ();
55 15         17 my $type;
56 15         271 while (<$F>) {
57 1962         2382 s/\r\n/\n/;
58 1962 100       9374 if (/^(msgid(?:|_plural)|msgctxt) +"(.*)" *$/) {
    100          
    100          
    100          
    50          
59 465         845 $type = $1;
60 465 50       900 if ( defined( $h{$type} ) ) {
61 0         0 $self->add_string( \%h );
62 0         0 %h = ();
63             }
64 465         2224 $h{$type} = $2;
65             } elsif (/^msgstr(?:\[(\d*)\])? +"(.*)" *$/) {
66 463         566 $type = "msgstr";
67 463 100       920 if ( !$h{$type} ) {
68 462         452 @{ $h{$type} } = ();
  462         1210  
69             }
70 463         532 push @{ $h{$type} }, $2;
  463         2286  
71             } elsif (/^"(.*)" *$/) {
72 60 100       95 if ( $type eq "msgstr" ) {
73 34         31 @{ $h{$type} }[ scalar( @{ $h{$type} } ) - 1 ] .= $1;
  34         180  
  34         58  
74             } else {
75 26         142 $h{$type} .= $1;
76             }
77             } elsif (/^ *$/) {
78 453         999 $self->add_string( \%h );
79 453         1251 %h = ();
80 453         1807 $type = undef;
81             } elsif (/^#/) {
82 521 100       1876 if (/^#, fuzzy/) {
    100          
83 12         40 $h{fuzzy} = 1;
84             } elsif (/^#:/) {
85 454 50       2050 if ( defined( $h{msgid} ) ) {
86 0         0 $self->add_string( \%h );
87 0         0 %h = ();
88 0         0 $type = undef;
89             }
90             }
91             } else {
92 0         0 die( "unknown line: " . $_ );
93             }
94             }
95 15         39 $self->add_string( \%h );
96 15         256 close $F;
97             }
98              
99             sub parse {
100 15     15 0 21 my $self = shift;
101 15         23 my ( $pofile, $mo ) = @_;
102 15         35 $self->{mo} = $mo;
103 15         38 $self->read_po($pofile);
104             }
105              
106             1;
107              
108             __END__