Changeset 10544 for trunk/Locale-Msgfmt
- Timestamp:
- 02/06/10 05:44:05 (2 years ago)
- Location:
- trunk/Locale-Msgfmt
- Files:
-
- 3 added
- 3 deleted
- 11 edited
- 1 moved
-
. (modified) (1 prop)
-
Build.PL (deleted)
-
Changes (modified) (1 diff)
-
Makefile.PL (added)
-
bin (deleted)
-
lib/Locale/Msgfmt.pm (modified) (8 diffs)
-
lib/Locale/Msgfmt/Utils.pm (modified) (3 diffs)
-
lib/Locale/Msgfmt/mo.pm (modified) (4 diffs)
-
lib/Locale/Msgfmt/po.pm (modified) (3 diffs)
-
lib/Module/Install/Msgfmt.pm (modified) (3 diffs)
-
private (moved) (moved from trunk/Locale-Msgfmt/dev)
-
script (added)
-
script/msgfmt.pl (added)
-
t/00-load.t (modified) (1 diff)
-
t/interface.t (modified) (3 diffs)
-
t/msgfmt.t (modified) (3 diffs)
-
t/pod.t (deleted)
-
t/version.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Locale-Msgfmt
- Property svn:ignore
-
old new 1 Build 1 inc 2 2 blib 3 _build 3 pm_to_blib 4 Makefile 4 5 META.yml 5 6 MANIFEST 6 Makefile.PL
-
- Property svn:ignore
-
trunk/Locale-Msgfmt/Changes
r5967 r10544 1 1 Revision history for Locale-Msgfmt 2 3 0.15 Feb 06, 2010 4 - General code cleaning 5 - Changes to Module::Install::Msgfmt to make it compatible with 6 Module::Install::Share. 7 - Moved script from bin/ to script/ 8 - Moved private tools from dev to private 9 - Picking 5.008005 as our minimum Perl to ensure solid Unicode support 10 - Converted the Build.PL to a Module::Install Makefile.PL 2 11 3 12 0.14 Jul 09, 2009 -
trunk/Locale-Msgfmt/lib/Locale/Msgfmt.pm
r5967 r10544 1 1 package Locale::Msgfmt; 2 2 3 use Locale::Msgfmt::mo; 4 use Locale::Msgfmt::po; 5 use Locale::Msgfmt::Utils; 6 use File::Path; 7 use File::Spec; 8 3 use 5.008005; 9 4 use strict; 10 5 use warnings; 6 use Exporter (); 7 use File::Spec (); 8 use Locale::Msgfmt::mo (); 9 use Locale::Msgfmt::po (); 10 use Locale::Msgfmt::Utils (); 11 11 12 use base 'Exporter'; 13 14 our @EXPORT = qw/msgfmt/; 15 16 our $VERSION = '0.14'; 12 our $VERSION = '0.15'; 13 our @ISA = 'Exporter'; 14 our @EXPORT = qw/msgfmt/; 17 15 18 16 sub do_msgfmt_for_module_install { … … 20 18 my $sharepath = shift; 21 19 my $fullpath = File::Spec->catfile( $lib, $sharepath, 'locale' ); 22 if ( !-d $fullpath ) {23 die ("$fullpath isn't a directory");20 unless ( -d $fullpath ) { 21 die "$fullpath isn't a directory"; 24 22 } 25 23 msgfmt( { in => $fullpath, verbose => 1, remove => 1 } ); … … 28 26 sub msgfmt { 29 27 my $hash = shift; 30 if ( !defined($hash) ) {31 die ("error: must give input");28 unless ( defined($hash) ) { 29 die "error: must give input"; 32 30 } 33 if ( !( ref($hash) eq "HASH" )) {31 unless ( ref($hash) eq "HASH" ) { 34 32 $hash = { in => $hash }; 35 33 } 36 if ( !defined( $hash->{in} ) or !length( $hash->{in} )) {37 die ("error: must give an input file");34 unless ( defined $hash->{in} and length $hash->{in} ) { 35 die "error: must give an input file"; 38 36 } 39 if ( !-e $hash->{in} ) {40 die ("error: input does not exist");37 unless ( -e $hash->{in} ) { 38 die "error: input does not exist"; 41 39 } 42 if ( !defined( $hash->{verbose} )) {40 unless ( defined $hash->{verbose} ) { 43 41 $hash->{verbose} = 1; 44 42 } … … 52 50 sub _msgfmt { 53 51 my $hash = shift; 54 if ( !defined( $hash->{in} )) {55 die ("error: must give an input file");52 unless ( defined $hash->{in} ) { 53 die "error: must give an input file"; 56 54 } 57 if ( !-f $hash->{in} ) {58 die ("error: input file does not exist");55 unless ( -f $hash->{in} ) { 56 die "error: input file does not exist"; 59 57 } 60 if ( !defined( $hash->{out} ) ) { 61 if ( $hash->{in} =~ /\.po$/ ) { 62 $hash->{out} = $hash->{in}; 63 $hash->{out} =~ s/po$/mo/; 64 } else { 65 die("error: must give an output file"); 58 unless ( defined $hash->{out} ) { 59 unless ( $hash->{in} =~ /\.po$/ ) { 60 die "error: must give an output file"; 61 } 62 $hash->{out} = $hash->{in}; 63 $hash->{out} =~ s/po$/mo/; 64 } 65 unless ( $hash->{force} ) { 66 my $min = Locale::Msgfmt::Utils::mtime( $hash->{in} ); 67 my $mout = Locale::Msgfmt::Utils::mtime( $hash->{out} ); 68 if ( -f $hash->{out} and $mout > $min ) { 69 return; 66 70 } 67 71 } 68 unless ( $hash->{force} ) { 69 return 70 if ( -f $hash->{out} 71 && Locale::Msgfmt::Utils::mtime( $hash->{out} ) > Locale::Msgfmt::Utils::mtime( $hash->{in} ) ); 72 } 73 my $mo = Locale::Msgfmt::mo->new(); 74 $mo->initialize(); 72 my $mo = Locale::Msgfmt::mo->new; 73 $mo->initialize; 75 74 my $po = Locale::Msgfmt::po->new( { fuzzy => $hash->{fuzzy} } ); 76 75 $po->parse( $hash->{in}, $mo ); 77 $mo->prepare ();78 unlink( $hash->{out} ) if ( -f $hash->{out} );76 $mo->prepare; 77 unlink( $hash->{out} ) if -f $hash->{out}; 79 78 $mo->out( $hash->{out} ); 80 print $hash->{in} . " -> " . $hash->{out} . "\n" if ( $hash->{verbose} );81 unlink( $hash->{in} ) if ( $hash->{remove} );79 print $hash->{in} . " -> " . $hash->{out} . "\n" if $hash->{verbose}; 80 unlink( $hash->{in} ) if $hash->{remove}; 82 81 } 83 82 84 83 sub _msgfmt_dir { 85 84 my $hash = shift; 86 if ( !-d $hash->{in} ) {85 unless ( -d $hash->{in} ) { 87 86 die("error: input directory does not exist"); 88 87 } 89 if ( !defined( $hash->{out} )) {88 unless ( defined $hash->{out} ) { 90 89 $hash->{out} = $hash->{in}; 91 90 } 92 if ( !-d $hash->{out} ) {91 unless ( -d $hash->{out} ) { 93 92 File::Path::mkpath( $hash->{out} ); 94 93 } 95 print $hash->{in} . " -> " . $hash->{out} . "\n" if ( $hash->{verbose} ); 96 opendir my $D, $hash->{in} or die "Could not open ($hash->{in}) $!"; 97 my @list = readdir $D; 98 closedir $D; 94 95 print "$hash->{in} -> $hash->{out}\n" if $hash->{verbose}; 96 97 local *DIRECTORY; 98 opendir( DIRECTORY, $hash->{in} ) or die "Could not open ($hash->{in}) $!"; 99 my @list = readdir DIRECTORY; 100 closedir DIRECTORY; 101 99 102 my @removelist = (); 100 103 if ( $hash->{remove} ) { … … 102 105 } 103 106 @list = grep /\.po$/, @list; 107 104 108 my %files; 105 foreach ( @list) {106 my $in = File::Spec->catfile( $hash->{in}, $_ );109 foreach ( @list ) { 110 my $in = File::Spec->catfile( $hash->{in}, $_ ); 107 111 my $out = File::Spec->catfile( $hash->{out}, substr( $_, 0, -3 ) . ".mo" ); 108 112 $files{$in} = $out; 109 113 } 110 114 foreach ( keys %files ) { 111 my %newhash = ( %{$hash} ); 112 $newhash{in} = $_; 113 $newhash{out} = $files{$_}; 114 _msgfmt( \%newhash ); 115 _msgfmt( { %$hash, in => $_, out => $files{$_} } ); 115 116 } 116 foreach ( @removelist) {117 foreach ( @removelist ) { 117 118 my $f = File::Spec->catfile( $hash->{in}, $_ ); 118 print "-$f\n" if ( $hash->{verbose} );119 print "-$f\n" if $hash->{verbose}; 119 120 unlink($f); 120 121 } … … 122 123 123 124 1; 125 126 =pod 124 127 125 128 =head1 NAME … … 133 136 examples: 134 137 135 use Locale::Msgfmt;138 use Locale::Msgfmt; 136 139 137 # compile po/fr.po into po/fr.mo 138 msgfmt({in => "po/fr.po", out => "po/fr.mo"}); 139 # compile po/fr.po into po/fr.mo and include fuzzy translations 140 msgfmt({in => "po/fr.po", out => "po/fr.mo", fuzzy => 1}); 141 # compile all the .po files in the po directory, and write the .mo 142 # files to the po directory 143 msgfmt("po/"); 144 # compile all the .po files in the po directory, and write the .mo 145 # files to the po directory, and include fuzzy translations 146 msgfmt({in => "po/", fuzzy => 1}); 147 # compile all the .po files in the po directory, and write the .mo 148 # files to the output directory, creating the output directory if 149 # it doesn't already exist 150 msgfmt({in => "po/", out => "output/"}); 151 # compile all the .po files in the po directory, and write the .mo 152 # files to the output directory, and include fuzzy translations 153 msgfmt({in => "po/", out => "output/", fuzzy => 1}); 154 # compile po/fr.po into po/fr.mo 155 msgfmt("po/fr.po"); 156 # compile po/fr.po into po/fr.mo and include fuzzy translations 157 msgfmt({in => "po/fr.po", fuzzy => 1}); 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}); 158 168 159 169 =head1 COPYRIGHT & LICENSE … … 164 174 under the same terms as Perl itself. 165 175 166 167 176 =cut -
trunk/Locale-Msgfmt/lib/Locale/Msgfmt/Utils.pm
r5968 r10544 1 1 package Locale::Msgfmt::Utils; 2 2 3 use 5.008005; 3 4 use strict; 4 5 use warnings; 5 6 6 our $VERSION = '0.1 4';7 our $VERSION = '0.15'; 7 8 8 9 sub character { … … 58 59 } 59 60 61 1; 62 63 __END__ 64 65 =pod 66 60 67 =head1 NAME 61 68 62 Locale::Msgfmt::Utils - functions used internally by Locale::Msgfmt69 Locale::Msgfmt::Utils - Functions used internally by Locale::Msgfmt 63 70 64 71 =head1 SYNOPSIS … … 71 78 72 79 =cut 73 74 1; -
trunk/Locale-Msgfmt/lib/Locale/Msgfmt/mo.pm
r5968 r10544 1 1 package Locale::Msgfmt::mo; 2 2 3 use 5.008005; 3 4 use strict; 4 5 use warnings; 6 use Locale::Msgfmt::Utils (); 5 7 6 our $VERSION = '0.14'; 7 8 use Locale::Msgfmt::Utils; 8 our $VERSION = '0.15'; 9 9 10 10 sub new { … … 20 20 21 21 sub add_string { 22 my ( $self, $string, $translation ) = @_; 23 $self->{strings}->{$string} = $translation; 22 $_[0]->{strings}->{$_[1]} = $_[2]; 24 23 } 25 24 26 25 sub prepare { 27 26 my $self = shift; 28 $self->{count} = scalar keys %{ $self->{strings} }; 29 $self->{free_mem} = 28 + $self->{count} * 16; 30 @{ $self->{sorted} } = sort keys %{ $self->{strings} }; 31 @{ $self->{translations} } = (); 32 foreach ( @{ $self->{sorted} } ) { 33 push @{ $self->{translations} }, $self->{strings}->{$_}; 34 } 27 $self->{count} = scalar keys %{ $self->{strings} }; 28 $self->{free_mem} = 28 + $self->{count} * 16; 29 $self->{sorted} = [ sort keys %{ $self->{strings} } ]; 30 $self->{translations} = [ 31 map { $self->{strings}->{$_} } @{ $self->{sorted} } 32 ]; 35 33 } 36 34 … … 69 67 } 70 68 69 1; 70 71 __END__ 72 73 =pod 74 71 75 =head1 NAME 72 76 … … 82 86 83 87 =cut 84 85 1; -
trunk/Locale-Msgfmt/lib/Locale/Msgfmt/po.pm
r5968 r10544 1 1 package Locale::Msgfmt::po; 2 2 3 use Locale::Msgfmt::Utils; 4 3 use 5.008005; 5 4 use strict; 6 5 use warnings; 6 use Locale::Msgfmt::Utils (); 7 7 8 our $VERSION = '0.1 4';8 our $VERSION = '0.15'; 9 9 10 10 sub new { … … 104 104 } 105 105 106 1; 107 108 __END__ 109 110 =pod 111 106 112 =head1 NAME 107 113 … … 117 123 118 124 =cut 119 120 1; -
trunk/Locale-Msgfmt/lib/Module/Install/Msgfmt.pm
r5968 r10544 1 1 package Module::Install::Msgfmt; 2 2 3 use 5.008005; 3 4 use strict; 4 use File::Spec; 5 use warnings; 6 use File::Spec (); 5 7 use Module::Install::Base (); 6 use Module::Install::Share;7 8 8 our $VERSION = '0.1 4';9 our $VERSION = '0.15'; 9 10 our @ISA = 'Module::Install::Base'; 10 11 … … 15 16 my $prefix = $self->_top->{prefix}; 16 17 my $name = $self->_top->{name}; 17 my $dir = @_ ? pop : 'share';18 my $dir = @_ ? pop : 'share'; 18 19 my $type = @_ ? shift : 'dist'; 19 20 my $module = @_ ? shift : ''; 20 $self->build_requires( 'Locale::Msgfmt' => '0.1 4' );21 install_share(@orig);21 $self->build_requires( 'Locale::Msgfmt' => '0.15' ); 22 $self->install_share(@orig); 22 23 my $distname = ""; 23 24 24 if ( $type eq 'dist' ) { 25 25 $distname = $self->name; … … 35 35 END_MAKEFILE 36 36 } 37 38 1; -
trunk/Locale-Msgfmt/t/00-load.t
r5854 r10544 1 #! perl -T1 #!/usr/bin/perl 2 2 3 use strict; 4 BEGIN { 5 $| = 1; 6 $^W = 1; 7 } 3 8 use Test::More tests => 1; 4 9 5 BEGIN { 6 use_ok('Locale::Msgfmt'); 7 } 8 9 diag("Testing Locale::Msgfmt $Locale::Msgfmt::VERSION, Perl $], $^X"); 10 use_ok('Locale::Msgfmt'); -
trunk/Locale-Msgfmt/t/interface.t
r5880 r10544 1 #! perl1 #!/usr/bin/perl 2 2 3 use strict; 4 BEGIN { 5 $| = 1; 6 $^W = 1; 7 } 3 8 use Test::More tests => 8; 4 5 use Locale::Msgfmt; 9 use File::Spec; 6 10 use File::Temp; 7 11 use File::Copy; 8 use File::Spec;9 12 use File::Path; 13 use Locale::Msgfmt; 10 14 11 15 sub slurp { 12 16 my $file = shift; 13 open my $F, '<', $file or die "Could not open ($file) $!"; 14 binmode $F; 15 my $s = ""; 16 while (<$F>) { $s .= $_; } 17 close $F; 18 return $s; 17 local *FILE; 18 open FILE, '<', $file or die "Could not open ($file) $!"; 19 binmode FILE; 20 my $string = ""; 21 while ( <FILE> ) { 22 $string .= $_; 23 } 24 close FILE; 25 return $string; 19 26 } 20 27 21 $dir = File::Temp::tempdir( CLEANUP => 0 );28 my $dir = File::Temp::tempdir( CLEANUP => 0 ); 22 29 copy( File::Spec->catfile( "t", "samples", "basic.po" ), File::Spec->catfile( $dir, "basic.po" ) ); 23 30 msgfmt( File::Spec->catfile( $dir, "basic.po" ) ); 24 31 ok( -f File::Spec->catfile( $dir, "basic.mo" ) ); 25 32 unlink File::Spec->catfile( $dir, "basic.mo" ); 33 26 34 msgfmt( { in => File::Spec->catfile( $dir, "basic.po" ) } ); 27 35 ok( -f File::Spec->catfile( $dir, "basic.mo" ) ); 28 36 unlink File::Spec->catfile( $dir, "basic.mo" ); 37 29 38 msgfmt( { in => File::Spec->catfile( $dir, "basic.po" ), out => File::Spec->catfile( $dir, "mo" ) } ); 30 39 ok( -f File::Spec->catfile( $dir, "mo" ) ); 31 40 unlink( File::Spec->catfile( $dir, "mo" ) ); 41 32 42 mkdir( File::Spec->catdir( $dir, "a" ) ); 33 43 mkdir( File::Spec->catdir( $dir, "b" ) ); … … 36 46 ok( -f File::Spec->catfile( $dir, "a", "basic.mo" ) ); 37 47 unlink File::Spec->catfile( $dir, "a", "basic.mo" ); 48 38 49 msgfmt( { in => File::Spec->catdir( $dir, "a" ) } ); 39 50 ok( -f File::Spec->catfile( $dir, "a", "basic.mo" ) ); 40 51 unlink File::Spec->catfile( $dir, "a", "basic.mo" ); 52 41 53 msgfmt( { in => File::Spec->catdir( $dir, "a" ), out => File::Spec->catdir( $dir, "b" ), } ); 42 54 ok( -f File::Spec->catfile( $dir, "b", "basic.mo" ) ); 43 55 unlink( File::Spec->catfile( $dir, "b", "basic.mo" ) ); 56 44 57 move( File::Spec->catfile( $dir, "a", "basic.po" ), File::Spec->catfile( $dir, "basic.po" ) ); 45 58 msgfmt( { in => File::Spec->catfile( $dir, "basic.po" ), fuzzy => 1, out => File::Spec->catfile( $dir, "fuzzy" ) } ); … … 48 61 unlink( File::Spec->catfile( $dir, "not_fuzzy" ) ); 49 62 unlink( File::Spec->catfile( $dir, "fuzzy" ) ); 63 50 64 move( File::Spec->catfile( $dir, "basic.po" ), File::Spec->catfile( $dir, "a", "basic.po" ) ); 51 65 msgfmt( { in => File::Spec->catfile( $dir, "a" ), fuzzy => 1, out => File::Spec->catfile( $dir, "b" ) } ); 52 66 msgfmt( { in => File::Spec->catfile( $dir, "a" ), out => File::Spec->catfile( $dir, "c" ) } ); 53 ok( !( slurp( File::Spec->catfile( $dir, "b", "basic.mo" ) ) eq slurp( File::Spec->catfile( $dir, "c", "basic.mo" ) ) ) 54 ); 67 ok( !( slurp( File::Spec->catfile( $dir, "b", "basic.mo" ) ) eq slurp( File::Spec->catfile( $dir, "c", "basic.mo" ) ) ) ); 55 68 unlink( File::Spec->catfile( $dir, "c", "basic.mo" ) ); 56 69 unlink( File::Spec->catfile( $dir, "b", "basic.mo" ) ); 57 -
trunk/Locale-Msgfmt/t/msgfmt.t
r5956 r10544 1 #! perl1 #!/usr/bin/perl 2 2 3 use strict; 4 BEGIN { 5 $| = 1; 6 $^W = 1; 7 } 3 8 use Test::More tests => 5; 4 9 use File::Spec; 10 use File::Temp; 5 11 use Locale::Msgfmt; 6 use File::Temp;7 use File::Spec;8 12 9 13 SKIP: { 10 skip "Test needs Locale::Maketext::Gettext", 5 if ( !eval("use Locale::Maketext::Gettext; 1;") ); 14 unless ( eval("use Locale::Maketext::Gettext; 1;") ) { 15 skip( "Test needs Locale::Maketext::Gettext", 5 ); 16 } 11 17 12 18 sub my_read_mo { 13 my %h = read_mo(shift); 14 return \%h; 19 return +{ read_mo(shift) }; 15 20 } 16 21 … … 20 25 my $in = shift; 21 26 utime( undef, undef, $in ); 22 my $fuzzy = 0; 23 if (shift) { 24 $fuzzy = 1; 25 } 27 my $fuzzy = $_[0] ? 1 : 0; 26 28 msgfmt( { in => $in, out => $filename, fuzzy => $fuzzy } ); 27 29 return $filename; … … 50 52 do_one_test("ngettext"); 51 53 } 52 -
trunk/Locale-Msgfmt/t/version.t
r5962 r10544 1 #! perl1 #!/usr/bin/perl 2 2 3 use strict; 4 BEGIN { 5 $| = 1; 6 $^W = 1; 7 } 3 8 use Test::More tests => 2; 4 use Locale::Msgfmt;5 use File::Spec;9 use File::Spec (); 10 use Locale::Msgfmt (); 6 11 7 12 sub slurp { 8 open F, File::Spec->catfile(@_); 9 my @str = <F>; 10 my $str = join "", @str; 11 close F; 12 return wantarray ? @str : $str; 13 my $file = File::Spec->catfile(@_); 14 local *FILE; 15 open( FILE, '<', $file ) or die "open($file): $!"; 16 my @str = <FILE>; 17 my $str = join "", @str; 18 close FILE; 19 return wantarray ? @str : $str; 13 20 } 14 21 15 my @all_bin = slurp(" bin", "msgfmt.pl");16 my @all_pm = slurp("lib", "Locale", "Msgfmt.pm");22 my @all_bin = slurp("script", "msgfmt.pl"); 23 my @all_pm = slurp("lib", "Locale", "Msgfmt.pm"); 17 24 my ($pm, $bin); 18 foreach( @all_bin) {19 $_ =~ /^use Locale::Msgfmt (.*);$/;20 $bin = $1 if($1);25 foreach( @all_bin ) { 26 $_ =~ /^use Locale::Msgfmt (.*);$/; 27 $bin = $1 if($1); 21 28 } 22 foreach( @all_pm) {23 $_ =~ /^our \$VERSION = '(.*)';$/;24 $pm = $1 if($1);29 foreach( @all_pm ) { 30 $_ =~ /^our \$VERSION = '(.*)';$/; 31 $pm = $1 if($1); 25 32 } 26 is( $pm, $Locale::Msgfmt::VERSION);27 is( $bin, $pm);33 is( $pm, $Locale::Msgfmt::VERSION ); 34 is( $bin, $pm );
Note: See TracChangeset
for help on using the changeset viewer.
