Locale/Msgfmt.pm

filter: Statementstmt 81.6% 80 / 98 Branchbran 58.7% 27 / 46 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 2 totaltotal 72.0% 121 / 168 CC 29 SCAR 22.8
CC
29
Cov
73%
_msgfmt_dir
13.3
_msgfmt
13.3
msgfmt
9.9
CRAP 9.8
« Module/Install/Msgfmt.pm ↑ Summary Locale/Msgfmt/po.pm »
1 package Locale::Msgfmt;
2  
3 4 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Locale::Msgfmt::mo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Locale::Msgfmt::po ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Locale::Msgfmt::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.15';
13 4 our @ISA = 'Exporter';
14 4 our @EXPORT = qw/msgfmt/;
15  
16 0 ▶sub do_msgfmt_for_module_install {
Subroutine do_msgfmt_for_module_install: not called CC=2 SCAR=17.9
Pod undocumented
17 0 my $lib = shift;
18 0 my $sharepath = shift;
19 0 my $fullpath = File::Spec->catfile( $lib, $sharepath, 'locale' );
20 0 ▶ unless ( -d $fullpath ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $fullpath) 0 0 0
21 0 die "$fullpath isn't a directory";
22 }
23 0 msgfmt( { in => $fullpath, verbose => 1, remove => 1 } );
24 }
25  
26 15 ▶sub msgfmt {
Subroutine msgfmt: called CC=8 SCAR=22.9
Pod undocumented
27 15 my $hash = shift;
28 15 ▶ unless ( defined($hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash) 0 15 15
29 0 die "error: must give input";
30 }
31 15 ▶ unless ( ref($hash) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash eq "HASH") 2 13 15
32 2 $hash = { in => $hash };
33 }
34 15 ▶ unless ( defined $hash->{in} and length $hash->{in} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"in"} and length $hash->{"in"}) 0 15 15
Condition33%1/3
defined $hash->{"in"} and length $hash->{"in"}
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $hash->{"in"} and length $hash->{"in"}
defined $hash->{"in"} length $hash->{"in"}
Truth table33%1/3
A and B
A= defined $hash->{"in"}B= length $hash->{"in"}
ABresult
0X0
100
111
35 0 die "error: must give an input file";
36 }
37 15 ▶ unless ( -e $hash->{in} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $hash->{"in"}) 0 15 15
38 0 die "error: input does not exist";
39 }
40 15 ▶ unless ( defined $hash->{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"verbose"}) 15 0 15
41 15 $hash->{verbose} = 1;
42 }
43 15 ▶ if ( -d $hash->{in} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $hash->{'in'}) { } 5 10 15
44 5 return _msgfmt_dir($hash);
45 } else {
46 10 return _msgfmt($hash);
47 }
48 }
49  
50 15 ▶sub _msgfmt {
Subroutine _msgfmt: called CC=11 SCAR=25.9
51 15 my $hash = shift;
52 15 ▶ unless ( defined $hash->{in} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"in"}) 0 15 15
53 0 die "error: must give an input file";
54 }
55 15 ▶ unless ( -f $hash->{in} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $hash->{"in"}) 0 15 15
56 0 die "error: input file does not exist";
57 }
58 15 ▶ unless ( defined $hash->{out} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"out"}) 2 13 15
59 2 ▶ unless ( $hash->{in} =~ /\.po$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash->{"in"} =~ /\.po$/) 0 2 2
60 0 die "error: must give an output file";
61 }
62 2 $hash->{out} = $hash->{in};
63 2 $hash->{out} =~ s/po$/mo/;
64 }
65 15 ▶ unless ( $hash->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash->{"force"}) 15 0 15
66 15 my $min = Locale::Msgfmt::Utils::mtime( $hash->{in} );
67 15 my $mout = Locale::Msgfmt::Utils::mtime( $hash->{out} );
68 15 ▶ if ( -f $hash->{out} and $mout > $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $hash->{"out"} and $mout > $min) 0 15 15
Condition66%2/3
-f $hash->{"out"} and $mout > $min
!ll&&!rl&&r
1050
MC/DC0%0/2
-f $hash->{"out"} and $mout > $min
-f $hash->{"out"} $mout > $min
Truth table66%2/3
A and B
A= -f $hash->{"out"}B= $mout > $min
ABresult
0X0
100
111
69 0 return;
70 }
71 }
72 15 my $mo = Locale::Msgfmt::mo->new;
73 15 $mo->initialize;
74 15 my $po = Locale::Msgfmt::po->new( { fuzzy => $hash->{fuzzy} } );
75 15 $po->parse( $hash->{in}, $mo );
76 15 $mo->prepare;
77 15 ▶ unlink( $hash->{out} ) if -f $hash->{out};
Branch: 1 branch
BranchTrueFalseTotal
if -f $hash->{"out"} 5 10 15
78 15 $mo->out( $hash->{out} );
79 15 ▶ print $hash->{in} . " -> " . $hash->{out} . "\n" if $hash->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"verbose"} 15 0 15
80 15 ▶ unlink( $hash->{in} ) if $hash->{remove};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"remove"} 0 15 15
81 }
82  
83 5 ▶sub _msgfmt_dir {
Subroutine _msgfmt_dir: called CC=11 SCAR=25.9
84 5 my $hash = shift;
85 5 ▶ unless ( -d $hash->{in} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $hash->{"in"}) 0 5 5
86 0 die("error: input directory does not exist");
87 }
88 5 ▶ unless ( defined $hash->{out} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"out"}) 2 3 5
89 2 $hash->{out} = $hash->{in};
90 }
91 5 ▶ unless ( -d $hash->{out} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $hash->{"out"}) 1 4 5
92 1 File::Path::mkpath( $hash->{out} );
93 }
94  
95 5 ▶ print "$hash->{in} -> $hash->{out}\n" if $hash->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"verbose"} 5 0 5
96  
97 5 local *DIRECTORY;
98 5 ▶ opendir( DIRECTORY, $hash->{in} ) or die "Could not open ($hash->{in}) $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIRECTORY, $hash->{"in"} or die qq[Could not open ($hash->{"in"}) $!] 5 0 5
99 5 my @list = readdir DIRECTORY;
100 5 closedir DIRECTORY;
101  
102 5 my @removelist = ();
103 5 ▶ if ( $hash->{remove} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"remove"}) 0 5 5
104 0 @removelist = grep /\.pot$/, @list;
105 }
106 5 @list = grep /\.po$/, @list;
107  
108 5 my %files;
109 5 foreach ( @list ) {
110 5 my $in = File::Spec->catfile( $hash->{in}, $_ );
111 5 my $out = File::Spec->catfile( $hash->{out}, substr( $_, 0, -3 ) . ".mo" );
112 5 $files{$in} = $out;
113 }
114 5 foreach ( keys %files ) {
115 5 _msgfmt( { %$hash, in => $_, out => $files{$_} } );
116 }
117 5 foreach ( @removelist ) {
118 0 my $f = File::Spec->catfile( $hash->{in}, $_ );
119 0 ▶ print "-$f\n" if $hash->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"verbose"} 0 0 0
120 0 unlink($f);
121 }
122 }
123  
124 4 1;
125  
126 =pod
127  
128 =head1 NAME
129  
130 Locale::Msgfmt - Compile .po files to .mo files
131  
132 =head1 SYNOPSIS
133  
134 This module does the same thing as msgfmt from GNU gettext-tools,
135 except this is pure Perl. The interface is best explained through
136 examples:
137  
138 use Locale::Msgfmt;
139  
140 # Compile po/fr.po into po/fr.mo
141 msgfmt({in => "po/fr.po", out => "po/fr.mo"});
142
143 # Compile po/fr.po into po/fr.mo and include fuzzy translations
144 msgfmt({in => "po/fr.po", out => "po/fr.mo", fuzzy => 1});
145
146 # Compile all the .po files in the po directory, and write the .mo
147 # files to the po directory
148 msgfmt("po/");
149
150 # Compile all the .po files in the po directory, and write the .mo
151 # files to the po directory, and include fuzzy translations
152 msgfmt({in => "po/", fuzzy => 1});
153
154 # Compile all the .po files in the po directory, and write the .mo
155 # files to the output directory, creating the output directory if
156 # it doesn't already exist
157 msgfmt({in => "po/", out => "output/"});
158
159 # Compile all the .po files in the po directory, and write the .mo
160 # files to the output directory, and include fuzzy translations
161 msgfmt({in => "po/", out => "output/", fuzzy => 1});
162
163 # Compile po/fr.po into po/fr.mo
164 msgfmt("po/fr.po");
165
166 # Compile po/fr.po into po/fr.mo and include fuzzy translations
167 msgfmt({in => "po/fr.po", fuzzy => 1});
168  
169 =head1 COPYRIGHT & LICENSE
170  
171 Copyright 2009 Ryan Niebur, all rights reserved.
172  
173 This program is free software; you can redistribute it and/or modify it
174 under the same terms as Perl itself.
175  
176 =cut
« Module/Install/Msgfmt.pm ↑ Summary Locale/Msgfmt/po.pm »