File Coverage

blib/lib/Locale/Msgfmt.pm
Criterion Covered Total %
statement 77 95 81.0
branch 27 46 58.7
condition 3 6 50.0
subroutine 11 12 91.6
pod 0 2 0.0
total 118 161 73.2


line stmt bran cond sub pod time code
1             package Locale::Msgfmt;
2              
3 4     4   182495 use 5.008005;
  4         18  
  4         189  
4 4     4   23 use strict;
  4         8  
  4         132  
5 4     4   830 use warnings;
  4         12  
  4         1092  
6 4     4   23 use Exporter ();
  4         9  
  4         953  
7 4     4   20 use File::Spec ();
  4         9  
  4         104  
8 4     4   4665 use Locale::Msgfmt::mo ();
  4         8  
  4         81  
9 4     4   2003 use Locale::Msgfmt::po ();
  4         9  
  4         81  
10 4     4   22 use Locale::Msgfmt::Utils ();
  4         9  
  4         4372  
11              
12             our $VERSION = '0.15';
13             our @ISA = 'Exporter';
14             our @EXPORT = qw/msgfmt/;
15              
16             sub do_msgfmt_for_module_install {
17 0     0 0 0 my $lib = shift;
18 0         0 my $sharepath = shift;
19 0         0 my $fullpath = File::Spec->catfile( $lib, $sharepath, 'locale' );
20 0 0       0 unless ( -d $fullpath ) {
21 0         0 die "$fullpath isn't a directory";
22             }
23 0         0 msgfmt( { in => $fullpath, verbose => 1, remove => 1 } );
24             }
25              
26             sub msgfmt {
27 15     15 0 63779 my $hash = shift;
28 15 50       48 unless ( defined($hash) ) {
29 0         0 die "error: must give input";
30             }
31 15 100       51 unless ( ref($hash) eq "HASH" ) {
32 2         7 $hash = { in => $hash };
33             }
34 15 50 33     106 unless ( defined $hash->{in} and length $hash->{in} ) {
35 0         0 die "error: must give an input file";
36             }
37 15 50       255 unless ( -e $hash->{in} ) {
38 0         0 die "error: input does not exist";
39             }
40 15 50       44 unless ( defined $hash->{verbose} ) {
41 15         28 $hash->{verbose} = 1;
42             }
43 15 100       236 if ( -d $hash->{in} ) {
44 5         12 return _msgfmt_dir($hash);
45             } else {
46 10         30 return _msgfmt($hash);
47             }
48             }
49              
50             sub _msgfmt {
51 15     15   25 my $hash = shift;
52 15 50       38 unless ( defined $hash->{in} ) {
53 0         0 die "error: must give an input file";
54             }
55 15 50       265 unless ( -f $hash->{in} ) {
56 0         0 die "error: input file does not exist";
57             }
58 15 100       35 unless ( defined $hash->{out} ) {
59 2 50       10 unless ( $hash->{in} =~ /\.po$/ ) {
60 0         0 die "error: must give an output file";
61             }
62 2         5 $hash->{out} = $hash->{in};
63 2         9 $hash->{out} =~ s/po$/mo/;
64             }
65 15 50       34 unless ( $hash->{force} ) {
66 15         56 my $min = Locale::Msgfmt::Utils::mtime( $hash->{in} );
67 15         61 my $mout = Locale::Msgfmt::Utils::mtime( $hash->{out} );
68 15 50 66     263 if ( -f $hash->{out} and $mout > $min ) {
69 0         0 return;
70             }
71             }
72 15         101 my $mo = Locale::Msgfmt::mo->new;
73 15         43 $mo->initialize;
74 15         112 my $po = Locale::Msgfmt::po->new( { fuzzy => $hash->{fuzzy} } );
75 15         53 $po->parse( $hash->{in}, $mo );
76 15         47 $mo->prepare;
77 15 100       735 unlink( $hash->{out} ) if -f $hash->{out};
78 15         54 $mo->out( $hash->{out} );
79 15 50       836 print $hash->{in} . " -> " . $hash->{out} . "\n" if $hash->{verbose};
80 15 50       350 unlink( $hash->{in} ) if $hash->{remove};
81             }
82              
83             sub _msgfmt_dir {
84 5     5   8 my $hash = shift;
85 5 50       82 unless ( -d $hash->{in} ) {
86 0         0 die("error: input directory does not exist");
87             }
88 5 100       12 unless ( defined $hash->{out} ) {
89 2         4 $hash->{out} = $hash->{in};
90             }
91 5 100       85 unless ( -d $hash->{out} ) {
92 1         198 File::Path::mkpath( $hash->{out} );
93             }
94              
95 5 50       113 print "$hash->{in} -> $hash->{out}\n" if $hash->{verbose};
96              
97 5         12 local *DIRECTORY;
98 5 50       152 opendir( DIRECTORY, $hash->{in} ) or die "Could not open ($hash->{in}) $!";
99 5         101 my @list = readdir DIRECTORY;
100 5         53 closedir DIRECTORY;
101              
102 5         8 my @removelist = ();
103 5 50       13 if ( $hash->{remove} ) {
104 0         0 @removelist = grep /\.pot$/, @list;
105             }
106 5         32 @list = grep /\.po$/, @list;
107              
108 5         6 my %files;
109 5         9 foreach ( @list ) {
110 5         55 my $in = File::Spec->catfile( $hash->{in}, $_ );
111 5         46 my $out = File::Spec->catfile( $hash->{out}, substr( $_, 0, -3 ) . ".mo" );
112 5         20 $files{$in} = $out;
113             }
114 5         15 foreach ( keys %files ) {
115 5         27 _msgfmt( { %$hash, in => $_, out => $files{$_} } );
116             }
117 5         28 foreach ( @removelist ) {
118 0           my $f = File::Spec->catfile( $hash->{in}, $_ );
119 0 0         print "-$f\n" if $hash->{verbose};
120 0           unlink($f);
121             }
122             }
123              
124             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