Changeset 5854
- Timestamp:
- 07/05/09 21:49:57 (3 years ago)
- Location:
- trunk/Locale-Msgfmt
- Files:
-
- 8 edited
-
lib/Locale/Msgfmt.pm (modified) (1 diff)
-
lib/Locale/Msgfmt/Utils.pm (modified) (1 diff)
-
lib/Locale/Msgfmt/mo.pm (modified) (1 diff)
-
lib/Locale/Msgfmt/po.pm (modified) (1 diff)
-
lib/Module/Install/Msgfmt.pm (modified) (2 diffs)
-
t/00-load.t (modified) (1 diff)
-
t/interface.t (modified) (1 diff)
-
t/msgfmt.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Locale-Msgfmt/lib/Locale/Msgfmt.pm
r5853 r5854 13 13 our @EXPORT = qw/msgfmt/; 14 14 15 our $VERSION = '0.0 8';15 our $VERSION = '0.09'; 16 16 17 17 sub msgfmt { 18 my $hash = shift;19 if(!defined($hash)) {20 die("error: must give input");21 }22 if(!(ref($hash) eq "HASH")) {23 $hash = {in => $hash};24 }25 if(!defined($hash->{in}) or !length($hash->{in})) {26 die("error: must give an input file");27 }28 if(! -e $hash->{in}) {29 die("error: input does not exist");30 }31 if(-d $hash->{in}) {32 return _msgfmt_dir($hash);33 } else {34 return _msgfmt($hash);35 }18 my $hash = shift; 19 if ( !defined($hash) ) { 20 die("error: must give input"); 21 } 22 if ( !( ref($hash) eq "HASH" ) ) { 23 $hash = { in => $hash }; 24 } 25 if ( !defined( $hash->{in} ) or !length( $hash->{in} ) ) { 26 die("error: must give an input file"); 27 } 28 if ( !-e $hash->{in} ) { 29 die("error: input does not exist"); 30 } 31 if ( -d $hash->{in} ) { 32 return _msgfmt_dir($hash); 33 } else { 34 return _msgfmt($hash); 35 } 36 36 } 37 37 38 38 sub _msgfmt { 39 my $hash = shift;40 if(! defined($hash->{in})) {41 die("error: must give an input file");42 }43 if(! -f $hash->{in}) {44 die("error: input file does not exist");45 }46 if(! defined($hash->{out})) {47 if($hash->{in} =~ /\.po$/) {48 $hash->{out} = $hash->{in};49 $hash->{out} =~ s/po$/mo/;50 } else {51 die("error: must give an output file");52 }53 }54 my $mo = Locale::Msgfmt::mo->new();55 $mo->initialize();56 my $po = Locale::Msgfmt::po->new({fuzzy => $hash->{fuzzy}});57 $po->parse($hash->{in}, $mo);58 $mo->prepare();59 $mo->out($hash->{out});60 print $hash->{in} . " -> " . $hash->{out} . "\n" if($hash->{verbose});61 unlink($hash->{in}) if($hash->{remove});39 my $hash = shift; 40 if ( !defined( $hash->{in} ) ) { 41 die("error: must give an input file"); 42 } 43 if ( !-f $hash->{in} ) { 44 die("error: input file does not exist"); 45 } 46 if ( !defined( $hash->{out} ) ) { 47 if ( $hash->{in} =~ /\.po$/ ) { 48 $hash->{out} = $hash->{in}; 49 $hash->{out} =~ s/po$/mo/; 50 } else { 51 die("error: must give an output file"); 52 } 53 } 54 my $mo = Locale::Msgfmt::mo->new(); 55 $mo->initialize(); 56 my $po = Locale::Msgfmt::po->new( { fuzzy => $hash->{fuzzy} } ); 57 $po->parse( $hash->{in}, $mo ); 58 $mo->prepare(); 59 $mo->out( $hash->{out} ); 60 print $hash->{in} . " -> " . $hash->{out} . "\n" if ( $hash->{verbose} ); 61 unlink( $hash->{in} ) if ( $hash->{remove} ); 62 62 } 63 63 64 64 sub _msgfmt_dir { 65 my $hash = shift;66 if(! -d $hash->{in}) {67 die("error: input directory does not exist");68 }69 if(! defined($hash->{out})) {70 $hash->{out} = $hash->{in};71 }72 if(! -d $hash->{out}) {73 File::Path::mkpath($hash->{out});74 }75 opendir D, $hash->{in};76 my @list = readdir D;77 closedir D;78 my @removelist = ();79 if($hash->{remove}) {80 @removelist = grep /\.pot$/, @list;81 }82 @list = grep /\.po$/, @list;83 my %files;84 foreach(@list) {85 my $in = File::Spec->catfile($hash->{in}, $_);86 my $out = File::Spec->catfile($hash->{out}, substr($_, 0, -3) . ".mo");87 $files{$in} = $out;88 }89 foreach(keys %files) {90 my %newhash = (%{$hash});91 $newhash{in}= $_;92 $newhash{out} = $files{$_};93 _msgfmt(\%newhash);94 }95 foreach(@removelist) {96 my $f = File::Spec->catfile($hash->{in}, $_);97 print "-$f\n" if($hash->{verbose});98 unlink($f);99 }65 my $hash = shift; 66 if ( !-d $hash->{in} ) { 67 die("error: input directory does not exist"); 68 } 69 if ( !defined( $hash->{out} ) ) { 70 $hash->{out} = $hash->{in}; 71 } 72 if ( !-d $hash->{out} ) { 73 File::Path::mkpath( $hash->{out} ); 74 } 75 opendir D, $hash->{in}; 76 my @list = readdir D; 77 closedir D; 78 my @removelist = (); 79 if ( $hash->{remove} ) { 80 @removelist = grep /\.pot$/, @list; 81 } 82 @list = grep /\.po$/, @list; 83 my %files; 84 foreach (@list) { 85 my $in = File::Spec->catfile( $hash->{in}, $_ ); 86 my $out = File::Spec->catfile( $hash->{out}, substr( $_, 0, -3 ) . ".mo" ); 87 $files{$in} = $out; 88 } 89 foreach ( keys %files ) { 90 my %newhash = ( %{$hash} ); 91 $newhash{in} = $_; 92 $newhash{out} = $files{$_}; 93 _msgfmt( \%newhash ); 94 } 95 foreach (@removelist) { 96 my $f = File::Spec->catfile( $hash->{in}, $_ ); 97 print "-$f\n" if ( $hash->{verbose} ); 98 unlink($f); 99 } 100 100 } 101 101 -
trunk/Locale-Msgfmt/lib/Locale/Msgfmt/Utils.pm
r5660 r5854 4 4 use warnings; 5 5 6 our $VERSION = '0.0 8';6 our $VERSION = '0.09'; 7 7 8 8 sub character { 9 return map {pack "N*", $_} @_;9 return map { pack "N*", $_ } @_; 10 10 } 11 11 12 12 sub _from_character { 13 return map {ord($_)} @_;13 return map { ord($_) } @_; 14 14 } 15 15 16 16 sub from_character { 17 return character(_from_character(@_));17 return character( _from_character(@_) ); 18 18 } 19 19 20 20 sub _from_hex { 21 return map {hex($_)} @_;21 return map { hex($_) } @_; 22 22 } 23 23 24 24 sub from_hex { 25 return character(_from_hex(@_));25 return character( _from_hex(@_) ); 26 26 } 27 27 28 28 sub _from_string { 29 return split //, join '', @_;29 return split //, join '', @_; 30 30 } 31 31 32 32 sub from_string { 33 return join_string(from_character(_from_string(@_)));33 return join_string( from_character( _from_string(@_) ) ); 34 34 } 35 35 36 36 sub join_string { 37 return join '', @_;37 return join '', @_; 38 38 } 39 39 40 40 sub number_to_s { 41 return sprintf "%d", shift;41 return sprintf "%d", shift; 42 42 } 43 43 44 44 sub null_terminate { 45 return pack "Z*", shift;45 return pack "Z*", shift; 46 46 } 47 47 48 48 sub null { 49 return null_terminate("");49 return null_terminate(""); 50 50 } 51 51 52 52 sub eot { 53 return chr(4);53 return chr(4); 54 54 } 55 55 -
trunk/Locale-Msgfmt/lib/Locale/Msgfmt/mo.pm
r5660 r5854 4 4 use warnings; 5 5 6 our $VERSION = '0.0 8';6 our $VERSION = '0.09'; 7 7 8 8 use Locale::Msgfmt::Utils; 9 9 10 10 sub new { 11 return bless {}, shift;11 return bless {}, shift; 12 12 } 13 13 14 14 sub initialize { 15 my $self = shift;16 $self->{magic}= "0x950412de";17 $self->{format}= 0;18 $self->{strings} = {};15 my $self = shift; 16 $self->{magic} = "0x950412de"; 17 $self->{format} = 0; 18 $self->{strings} = {}; 19 19 } 20 20 21 21 sub add_string { 22 my ($self, $string, $translation) = @_;23 $self->{strings}->{$string} = $translation;22 my ( $self, $string, $translation ) = @_; 23 $self->{strings}->{$string} = $translation; 24 24 } 25 25 26 26 sub prepare { 27 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 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 } 35 35 } 36 36 37 37 sub out { 38 my $self = shift; 39 my $file = shift; 40 open OUT, ">", $file; 41 binmode OUT; 42 print OUT Locale::Msgfmt::Utils::from_hex($self->{magic}); 43 print OUT Locale::Msgfmt::Utils::character($self->{format}); 44 print OUT Locale::Msgfmt::Utils::character($self->{count}); 45 print OUT Locale::Msgfmt::Utils::character(28); 46 print OUT Locale::Msgfmt::Utils::character(28 + $self->{count} * 8); 47 print OUT Locale::Msgfmt::Utils::character(0); 48 print OUT Locale::Msgfmt::Utils::character(0); 49 foreach(@{$self->{sorted}}) { 50 my $length = length($_); 51 print OUT Locale::Msgfmt::Utils::character($length); 52 print OUT Locale::Msgfmt::Utils::character($self->{free_mem}); 53 $self->{free_mem} += $length + 1; 54 } 55 foreach(@{$self->{translations}}) { 56 my $length = length($_); 57 print OUT Locale::Msgfmt::Utils::character($length); 58 print OUT Locale::Msgfmt::Utils::character($self->{free_mem}); 59 $self->{free_mem} += $length + 1; 60 } 61 foreach(@{$self->{sorted}}) { 62 print OUT Locale::Msgfmt::Utils::null_terminate($_); 63 } 64 foreach(@{$self->{translations}}) { 65 print OUT Locale::Msgfmt::Utils::null_terminate($_); 66 } 67 close OUT; 38 my $self = shift; 39 my $file = shift; 40 open OUT, ">", $file; 41 binmode OUT; 42 print OUT Locale::Msgfmt::Utils::from_hex( $self->{magic} ); 43 print OUT Locale::Msgfmt::Utils::character( $self->{format} ); 44 print OUT Locale::Msgfmt::Utils::character( $self->{count} ); 45 print OUT Locale::Msgfmt::Utils::character(28); 46 print OUT Locale::Msgfmt::Utils::character( 28 + $self->{count} * 8 ); 47 print OUT Locale::Msgfmt::Utils::character(0); 48 print OUT Locale::Msgfmt::Utils::character(0); 49 50 foreach ( @{ $self->{sorted} } ) { 51 my $length = length($_); 52 print OUT Locale::Msgfmt::Utils::character($length); 53 print OUT Locale::Msgfmt::Utils::character( $self->{free_mem} ); 54 $self->{free_mem} += $length + 1; 55 } 56 foreach ( @{ $self->{translations} } ) { 57 my $length = length($_); 58 print OUT Locale::Msgfmt::Utils::character($length); 59 print OUT Locale::Msgfmt::Utils::character( $self->{free_mem} ); 60 $self->{free_mem} += $length + 1; 61 } 62 foreach ( @{ $self->{sorted} } ) { 63 print OUT Locale::Msgfmt::Utils::null_terminate($_); 64 } 65 foreach ( @{ $self->{translations} } ) { 66 print OUT Locale::Msgfmt::Utils::null_terminate($_); 67 } 68 close OUT; 68 69 } 69 70 -
trunk/Locale-Msgfmt/lib/Locale/Msgfmt/po.pm
r5660 r5854 6 6 use warnings; 7 7 8 our $VERSION = '0.0 8';8 our $VERSION = '0.09'; 9 9 10 10 sub new { 11 my $class = shift;12 return bless shift || {}, $class;11 my $class = shift; 12 return bless shift || {}, $class; 13 13 } 14 14 15 15 sub cleanup_string { 16 my $str = shift;17 $str =~ s/\\n/\n/g;18 $str =~ s/\\r/\r/g;19 $str =~ s/\\t/\t/g;20 $str =~ s/\\"/"/g;21 $str =~ s/\\\\/\\/g;22 return $str;16 my $str = shift; 17 $str =~ s/\\n/\n/g; 18 $str =~ s/\\r/\r/g; 19 $str =~ s/\\t/\t/g; 20 $str =~ s/\\"/"/g; 21 $str =~ s/\\\\/\\/g; 22 return $str; 23 23 } 24 24 25 25 sub add_string { 26 my $self = shift; 27 my $hash = shift; 28 my %h = %{$hash}; 29 return if !(defined($h{msgid}) && defined($h{msgstr})); 30 return if ($h{fuzzy} && !$self->{fuzzy} && length($h{msgid}) > 0); 31 my $msgstr = join Locale::Msgfmt::Utils::null(), @{$h{msgstr}}; 32 return if($msgstr eq ""); 33 my $context; 34 my $plural; 35 if($h{msgctxt}) { 36 $context = cleanup_string($h{msgctxt}) . Locale::Msgfmt::Utils::eot(); 37 } else { 38 $context = ""; 39 } 40 if($h{msgid_plural}) { 41 $plural = Locale::Msgfmt::Utils::null() . cleanup_string($h{msgid_plural}); 42 } else { 43 $plural = ""; 44 } 45 $self->{mo}->add_string($context . cleanup_string($h{msgid}) . $plural, cleanup_string($msgstr)); 26 my $self = shift; 27 my $hash = shift; 28 my %h = %{$hash}; 29 return if !( defined( $h{msgid} ) && defined( $h{msgstr} ) ); 30 return if ( $h{fuzzy} && !$self->{fuzzy} && length( $h{msgid} ) > 0 ); 31 my $msgstr = join Locale::Msgfmt::Utils::null(), @{ $h{msgstr} }; 32 return if ( $msgstr eq "" ); 33 my $context; 34 my $plural; 35 36 if ( $h{msgctxt} ) { 37 $context = cleanup_string( $h{msgctxt} ) . Locale::Msgfmt::Utils::eot(); 38 } else { 39 $context = ""; 40 } 41 if ( $h{msgid_plural} ) { 42 $plural = Locale::Msgfmt::Utils::null() . cleanup_string( $h{msgid_plural} ); 43 } else { 44 $plural = ""; 45 } 46 $self->{mo}->add_string( $context . cleanup_string( $h{msgid} ) . $plural, cleanup_string($msgstr) ); 46 47 } 47 48 48 49 sub read_po { 49 my $self = shift; 50 my $pofile = shift; 51 my $mo = $self->{mo}; 52 open F, $pofile; 53 my %h = (); 54 my $type; 55 while (<F>) { 56 s/\r\n/\n/; 57 if(/^(msgid(?:|_plural)|msgctxt) +"(.*)" *$/) { 58 $type = $1; 59 if(defined($h{$type})) { 60 $self->add_string(\%h); 61 %h = (); 62 } 63 $h{$type} = $2; 64 } 65 elsif(/^msgstr(?:\[(\d*)\])? +"(.*)" *$/) { 66 $type = "msgstr"; 67 if(!$h{$type}) { 68 @{$h{$type}} = (); 69 } 70 push @{$h{$type}}, $2; 71 } 72 elsif(/^"(.*)" *$/) { 73 if($type eq "msgstr") { 74 @{$h{$type}}[scalar(@{$h{$type}}) - 1] .= $1; 75 } else { 76 $h{$type} .= $1; 77 } 78 } 79 elsif(/^ *$/) { 80 $self->add_string(\%h); 81 %h = (); 82 $type = undef; 83 } elsif(/^#/) { 84 if(/^#, fuzzy/) { 85 $h{fuzzy} = 1; 86 } elsif (/^#:/) { 87 if(defined($h{msgid})) { 88 $self->add_string(\%h); 89 %h = (); 90 $type = undef; 91 } 92 } 93 } else { 94 print "unknown line: " . $_ . "\n"; 95 } 96 } 97 $self->add_string(\%h); 98 close F; 50 my $self = shift; 51 my $pofile = shift; 52 my $mo = $self->{mo}; 53 open F, $pofile; 54 my %h = (); 55 my $type; 56 while (<F>) { 57 s/\r\n/\n/; 58 if (/^(msgid(?:|_plural)|msgctxt) +"(.*)" *$/) { 59 $type = $1; 60 if ( defined( $h{$type} ) ) { 61 $self->add_string( \%h ); 62 %h = (); 63 } 64 $h{$type} = $2; 65 } elsif (/^msgstr(?:\[(\d*)\])? +"(.*)" *$/) { 66 $type = "msgstr"; 67 if ( !$h{$type} ) { 68 @{ $h{$type} } = (); 69 } 70 push @{ $h{$type} }, $2; 71 } elsif (/^"(.*)" *$/) { 72 if ( $type eq "msgstr" ) { 73 @{ $h{$type} }[ scalar( @{ $h{$type} } ) - 1 ] .= $1; 74 } else { 75 $h{$type} .= $1; 76 } 77 } elsif (/^ *$/) { 78 $self->add_string( \%h ); 79 %h = (); 80 $type = undef; 81 } elsif (/^#/) { 82 if (/^#, fuzzy/) { 83 $h{fuzzy} = 1; 84 } elsif (/^#:/) { 85 if ( defined( $h{msgid} ) ) { 86 $self->add_string( \%h ); 87 %h = (); 88 $type = undef; 89 } 90 } 91 } else { 92 print "unknown line: " . $_ . "\n"; 93 } 94 } 95 $self->add_string( \%h ); 96 close F; 99 97 } 100 98 101 99 sub parse { 102 my $self = shift;103 my ($pofile, $mo) = @_;104 $self->{mo} = $mo;105 $self->read_po($pofile);100 my $self = shift; 101 my ( $pofile, $mo ) = @_; 102 $self->{mo} = $mo; 103 $self->read_po($pofile); 106 104 } 107 105 -
trunk/Locale-Msgfmt/lib/Module/Install/Msgfmt.pm
r5853 r5854 6 6 use Module::Install::Share; 7 7 8 use vars qw{$VERSION @ISA $ISCORE}; 9 $VERSION = '0.01'; 10 @ISA = 'Module::Install::Base'; 8 our $VERSION = '0.09'; 9 our @ISA = 'Module::Install::Base'; 11 10 12 11 sub install_share_with_mofiles { 13 my @orig = (@_); 14 my $self = shift; 15 my $class = ref($self); 16 my $inc_class = join('::', @{$self->_top}{qw(prefix name)}); 17 my $dir = @_ ? pop : 'share'; 18 my $type = @_ ? shift : 'dist'; 19 my $module = @_ ? shift : ''; 20 $self->build_requires( 'Locale::Msgfmt' => '0.09' ); 21 install_share(@orig); 22 my $distname = ""; 23 if ( $type eq 'dist' ) { 24 $distname = $self->name; 25 } else { 26 $distname = Module::Install::_CLASS($module); 27 $distname =~ s/::/-/g; 28 } 29 my $path = File::Spec->catfile('auto', 'share', $type, $distname); 30 $self->postamble(<<"END_MAKEFILE"); 12 my @orig = (@_); 13 my $self = shift; 14 my $class = ref($self); 15 my $inc_class = join( '::', @{ $self->_top }{qw(prefix name)} ); 16 my $dir = @_ ? pop : 'share'; 17 my $type = @_ ? shift : 'dist'; 18 my $module = @_ ? shift : ''; 19 $self->build_requires( 'Locale::Msgfmt' => '0.09' ); 20 install_share(@orig); 21 my $distname = ""; 22 23 if ( $type eq 'dist' ) { 24 $distname = $self->name; 25 } else { 26 $distname = Module::Install::_CLASS($module); 27 $distname =~ s/::/-/g; 28 } 29 my $path = File::Spec->catfile( 'auto', 'share', $type, $distname ); 30 $self->postamble(<<"END_MAKEFILE"); 31 31 config :: 32 32 \t\$(NOECHO) \$(PERL) "-M$inc_class" -e "do_msgfmt(q(\$(INST_LIB)), q($path))" … … 37 37 # blib/lib/auto/share/dist/Padre/locale/he.po 38 38 sub do_msgfmt { 39 my $self= shift;40 my $lib= shift;41 my $sharepath = shift;42 my $fullpath = File::Spec->catfile($lib, $sharepath, 'locale');43 if(! -d $fullpath) {44 die("$fullpath isn't a directory");45 }46 require Locale::Msgfmt;47 Locale::Msgfmt::msgfmt({in => $fullpath, verbose => 1, remove => 1});39 my $self = shift; 40 my $lib = shift; 41 my $sharepath = shift; 42 my $fullpath = File::Spec->catfile( $lib, $sharepath, 'locale' ); 43 if ( !-d $fullpath ) { 44 die("$fullpath isn't a directory"); 45 } 46 require Locale::Msgfmt; 47 Locale::Msgfmt::msgfmt( { in => $fullpath, verbose => 1, remove => 1 } ); 48 48 } -
trunk/Locale-Msgfmt/t/00-load.t
r5526 r5854 4 4 5 5 BEGIN { 6 use_ok( 'Locale::Msgfmt');6 use_ok('Locale::Msgfmt'); 7 7 } 8 8 9 diag( "Testing Locale::Msgfmt $Locale::Msgfmt::VERSION, Perl $], $^X");9 diag("Testing Locale::Msgfmt $Locale::Msgfmt::VERSION, Perl $], $^X"); -
trunk/Locale-Msgfmt/t/interface.t
r5625 r5854 10 10 11 11 sub slurp { 12 open F, shift;13 binmode F;14 my $s = "";15 while(<F>) { $s .= $_; }; 16 close F;17 return $s;12 open F, shift; 13 binmode F; 14 my $s = ""; 15 while (<F>) { $s .= $_; } 16 close F; 17 return $s; 18 18 } 19 19 20 $dir = File::Temp::tempdir(CLEANUP => 0); 21 copy(File::Spec->catfile("t", "samples", "basic.po"), File::Spec->catfile($dir, "basic.po")); 22 msgfmt(File::Spec->catfile($dir, "basic.po")); 23 ok(-f File::Spec->catfile($dir, "basic.mo")); 24 unlink File::Spec->catfile($dir, "basic.mo"); 25 msgfmt({in => File::Spec->catfile($dir, "basic.po")}); 26 ok(-f File::Spec->catfile($dir, "basic.mo")); 27 unlink File::Spec->catfile($dir, "basic.mo"); 28 msgfmt({in => File::Spec->catfile($dir, "basic.po"), out => File::Spec->catfile($dir, "mo")}); 29 ok(-f File::Spec->catfile($dir, "mo")); 30 unlink(File::Spec->catfile($dir, "mo")); 31 mkdir(File::Spec->catdir($dir, "a")); 32 mkdir(File::Spec->catdir($dir, "b")); 33 move(File::Spec->catfile($dir, "basic.po"), File::Spec->catfile($dir, "a", "basic.po")); 34 msgfmt(File::Spec->catdir($dir, "a")); 35 ok(-f File::Spec->catfile($dir, "a", "basic.mo")); 36 unlink File::Spec->catfile($dir, "a", "basic.mo"); 37 msgfmt({in => File::Spec->catdir($dir, "a")}); 38 ok(-f File::Spec->catfile($dir, "a", "basic.mo")); 39 unlink File::Spec->catfile($dir, "a", "basic.mo"); 40 msgfmt({in => File::Spec->catdir($dir, "a"), out => File::Spec->catdir($dir, "b"),}); 41 ok(-f File::Spec->catfile($dir, "b", "basic.mo")); 42 unlink(File::Spec->catfile($dir, "b", "basic.mo")); 43 move(File::Spec->catfile($dir, "a", "basic.po"), File::Spec->catfile($dir, "basic.po")); 44 msgfmt({in => File::Spec->catfile($dir, "basic.po"), fuzzy => 1, out => File::Spec->catfile($dir, "fuzzy")}); 45 msgfmt({in => File::Spec->catfile($dir, "basic.po"), out => File::Spec->catfile($dir, "not_fuzzy")}); 46 ok(!(slurp(File::Spec->catfile($dir, "not_fuzzy")) eq slurp(File::Spec->catfile($dir, "fuzzy")))); 47 unlink(File::Spec->catfile($dir, "not_fuzzy")); 48 unlink(File::Spec->catfile($dir, "fuzzy")); 49 move(File::Spec->catfile($dir, "basic.po"), File::Spec->catfile($dir, "a", "basic.po")); 50 msgfmt({in => File::Spec->catfile($dir, "a"), fuzzy => 1, out => File::Spec->catfile($dir, "b")}); 51 msgfmt({in => File::Spec->catfile($dir, "a"), out => File::Spec->catfile($dir, "c")}); 52 ok(!(slurp(File::Spec->catfile($dir, "b", "basic.mo")) eq slurp(File::Spec->catfile($dir, "c", "basic.mo")))); 53 unlink(File::Spec->catfile($dir, "c", "basic.mo")); 54 unlink(File::Spec->catfile($dir, "b", "basic.mo")); 20 $dir = File::Temp::tempdir( CLEANUP => 0 ); 21 copy( File::Spec->catfile( "t", "samples", "basic.po" ), File::Spec->catfile( $dir, "basic.po" ) ); 22 msgfmt( File::Spec->catfile( $dir, "basic.po" ) ); 23 ok( -f File::Spec->catfile( $dir, "basic.mo" ) ); 24 unlink File::Spec->catfile( $dir, "basic.mo" ); 25 msgfmt( { in => File::Spec->catfile( $dir, "basic.po" ) } ); 26 ok( -f File::Spec->catfile( $dir, "basic.mo" ) ); 27 unlink File::Spec->catfile( $dir, "basic.mo" ); 28 msgfmt( { in => File::Spec->catfile( $dir, "basic.po" ), out => File::Spec->catfile( $dir, "mo" ) } ); 29 ok( -f File::Spec->catfile( $dir, "mo" ) ); 30 unlink( File::Spec->catfile( $dir, "mo" ) ); 31 mkdir( File::Spec->catdir( $dir, "a" ) ); 32 mkdir( File::Spec->catdir( $dir, "b" ) ); 33 move( File::Spec->catfile( $dir, "basic.po" ), File::Spec->catfile( $dir, "a", "basic.po" ) ); 34 msgfmt( File::Spec->catdir( $dir, "a" ) ); 35 ok( -f File::Spec->catfile( $dir, "a", "basic.mo" ) ); 36 unlink File::Spec->catfile( $dir, "a", "basic.mo" ); 37 msgfmt( { in => File::Spec->catdir( $dir, "a" ) } ); 38 ok( -f File::Spec->catfile( $dir, "a", "basic.mo" ) ); 39 unlink File::Spec->catfile( $dir, "a", "basic.mo" ); 40 msgfmt( { in => File::Spec->catdir( $dir, "a" ), out => File::Spec->catdir( $dir, "b" ), } ); 41 ok( -f File::Spec->catfile( $dir, "b", "basic.mo" ) ); 42 unlink( File::Spec->catfile( $dir, "b", "basic.mo" ) ); 43 move( File::Spec->catfile( $dir, "a", "basic.po" ), File::Spec->catfile( $dir, "basic.po" ) ); 44 msgfmt( { in => File::Spec->catfile( $dir, "basic.po" ), fuzzy => 1, out => File::Spec->catfile( $dir, "fuzzy" ) } ); 45 msgfmt( { in => File::Spec->catfile( $dir, "basic.po" ), out => File::Spec->catfile( $dir, "not_fuzzy" ) } ); 46 ok( !( slurp( File::Spec->catfile( $dir, "not_fuzzy" ) ) eq slurp( File::Spec->catfile( $dir, "fuzzy" ) ) ) ); 47 unlink( File::Spec->catfile( $dir, "not_fuzzy" ) ); 48 unlink( File::Spec->catfile( $dir, "fuzzy" ) ); 49 move( File::Spec->catfile( $dir, "basic.po" ), File::Spec->catfile( $dir, "a", "basic.po" ) ); 50 msgfmt( { in => File::Spec->catfile( $dir, "a" ), fuzzy => 1, out => File::Spec->catfile( $dir, "b" ) } ); 51 msgfmt( { in => File::Spec->catfile( $dir, "a" ), out => File::Spec->catfile( $dir, "c" ) } ); 52 ok( !( slurp( File::Spec->catfile( $dir, "b", "basic.mo" ) ) eq slurp( File::Spec->catfile( $dir, "c", "basic.mo" ) ) ) 53 ); 54 unlink( File::Spec->catfile( $dir, "c", "basic.mo" ) ); 55 unlink( File::Spec->catfile( $dir, "b", "basic.mo" ) ); 55 56 -
trunk/Locale-Msgfmt/t/msgfmt.t
r5729 r5854 8 8 9 9 SKIP: { 10 skip "Test needs Locale::Maketext::Gettext", 5 if(!eval("use Locale::Maketext::Gettext; 1;")); 11 sub my_read_mo { 12 my %h = read_mo(shift); 13 return \%h; 14 } 15 sub my_msgfmt { 16 my ($fh, $filename) = File::Temp::tempfile(); 17 close $fh; 18 my $in = shift; 19 my $fuzzy = 0; 20 if(shift) { 21 $fuzzy = 1; 22 } 23 msgfmt({in => $in, out => $filename, fuzzy => $fuzzy}); 24 return $filename; 25 } 26 sub do_one_test { 27 my $basename = shift; 28 my $po = File::Spec->catfile("t", "samples", $basename . ".po"); 29 my $mo = File::Spec->catfile("t", "samples", $basename . ".mo"); 30 my $good = my_read_mo($mo); 31 my $filename = my_msgfmt($po); 32 my $test = my_read_mo($filename); 33 is_deeply($test, $good); 34 if($basename eq "basic") { 35 unlink($filename); 36 $filename = my_msgfmt($po, 1); 37 $good = my_read_mo(File::Spec->catfile("t", "samples", "fuzz.mo")); 38 $test = my_read_mo($filename); 39 is_deeply($test, $good); 40 } 41 unlink($filename); 42 } 43 do_one_test("basic"); 44 do_one_test("ja"); 45 do_one_test("context"); 46 do_one_test("ngettext"); 10 skip "Test needs Locale::Maketext::Gettext", 5 if ( !eval("use Locale::Maketext::Gettext; 1;") ); 11 12 sub my_read_mo { 13 my %h = read_mo(shift); 14 return \%h; 15 } 16 17 sub my_msgfmt { 18 my ( $fh, $filename ) = File::Temp::tempfile(); 19 close $fh; 20 my $in = shift; 21 my $fuzzy = 0; 22 if (shift) { 23 $fuzzy = 1; 24 } 25 msgfmt( { in => $in, out => $filename, fuzzy => $fuzzy } ); 26 return $filename; 27 } 28 29 sub do_one_test { 30 my $basename = shift; 31 my $po = File::Spec->catfile( "t", "samples", $basename . ".po" ); 32 my $mo = File::Spec->catfile( "t", "samples", $basename . ".mo" ); 33 my $good = my_read_mo($mo); 34 my $filename = my_msgfmt($po); 35 my $test = my_read_mo($filename); 36 is_deeply( $test, $good ); 37 if ( $basename eq "basic" ) { 38 unlink($filename); 39 $filename = my_msgfmt( $po, 1 ); 40 $good = my_read_mo( File::Spec->catfile( "t", "samples", "fuzz.mo" ) ); 41 $test = my_read_mo($filename); 42 is_deeply( $test, $good ); 43 } 44 unlink($filename); 45 } 46 do_one_test("basic"); 47 do_one_test("ja"); 48 do_one_test("context"); 49 do_one_test("ngettext"); 47 50 } 48 51
Note: See TracChangeset
for help on using the changeset viewer.
