Skip to content

Commit

Permalink
fix issue#1 - support for recursive directory lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
jquelin committed May 20, 2011
1 parent 170f15f commit ad347f1
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for {{ $dist->name }}

{{ $NEXT }}
* fix issue#1 - support for recursive directory lookup (jérôme quelin)
* added test suite (jérôme quelin)

1.202 2010-11-05 23:47:26 America/New_York
Expand Down
38 changes: 30 additions & 8 deletions lib/Dist/Zilla/Plugin/LocaleMsgfmt.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Dist::Zilla::Plugin::LocaleMsgfmt;

use Locale::Msgfmt 0.14;
use Moose;
use MooseX::Has::Sugar;
use Path::Class;

with 'Dist::Zilla::Role::BeforeBuild';
Expand All @@ -14,16 +15,21 @@ with 'Dist::Zilla::Role::BeforeBuild';
# be stored in an arrayref
sub mvp_multivalue_args { qw(locale) }


=attr recursive
Whether to look up in the locale files recursively.
=attr locale
Path to the directory containing the locale files.
=cut

has recursive => ( ro, isa=>'Bool', default=>1 );
has locale => (
is => 'ro',
ro, lazy, auto_deref,
isa => 'ArrayRef[Str]',
lazy => 1,
default => sub {
my $self = shift;
my $path = dir( $self->zilla->root, 'share', 'locale' );
Expand All @@ -39,14 +45,30 @@ has locale => (
sub before_build {
my ( $self, $args ) = @_;

for my $dir ( @{ $self->locale } ) {
for my $dir ( $self->locale ) {
my $path = dir($dir);
if ( -e $path ) {
$self->log("Generating .mo files from .po files in $path");
msgfmt( { in => $path->absolute, verbose => 1, remove => 0 } );
}
else {
if ( ! -e $path ) {
warn "Skipping invalid path: $path";
next;
}

# find directories if recursive behaviour wanted
my @pathes;
if ( $self->recursive ) {
$path->recurse(
callback => sub {
my $p = shift;
push @pathes, $p if -d $p;
}
);
} else {
push @pathes, $path;
}

# generating mo files
foreach my $p ( @pathes ) {
$self->log("Generating .mo files from .po files in $p");
msgfmt( { in => $p->absolute, verbose => 1, remove => 0 } );
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion t/msgfmt.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Dist::Zilla 1.093250;
use Dist::Zilla::Tester;
use Path::Class;
use Test::File;
use Test::More tests => 3;
use Test::More tests => 8;
use Test::Warn;

# build fake repository
Expand All @@ -21,3 +21,10 @@ my $share = $tzil->root->subdir( "share" );
file_exists_ok( $share->file( "locale1", "basic.mo" ), "1st dir" );
file_exists_ok( $share->file( "locale2", "basic.mo" ), "2nd dir" );

# recursive
file_exists_ok( $share->file( "locale1", "a", "a", "basic.mo" ), "recurse" );
file_exists_ok( $share->file( "locale1", "a", "b", "basic.mo" ), "recurse" );
file_exists_ok( $share->file( "locale1", "b", "a", "basic.mo" ), "recurse" );
file_exists_ok( $share->file( "locale1", "b", "b", "basic.mo" ), "recurse" );
file_exists_ok( $share->file( "locale1", "b", "basic.mo" ), "recurse" );

1 change: 1 addition & 0 deletions t/msgfmt/dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ copyright_holder = foobar
copyright_year = 2009

[LocaleMsgfmt]
recursive = 1
locale = share/locale1
locale = share/locale2
locale = share/locale3
Expand Down
6 changes: 6 additions & 0 deletions t/msgfmt/share/locale1/a/a/basic.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
msgid "foo"
msgstr "bar"

#, fuzzy
msgid "fuzz"
msgstr "fuzz"
6 changes: 6 additions & 0 deletions t/msgfmt/share/locale1/a/b/basic.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
msgid "foo"
msgstr "bar"

#, fuzzy
msgid "fuzz"
msgstr "fuzz"
6 changes: 6 additions & 0 deletions t/msgfmt/share/locale1/b/a/basic.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
msgid "foo"
msgstr "bar"

#, fuzzy
msgid "fuzz"
msgstr "fuzz"
6 changes: 6 additions & 0 deletions t/msgfmt/share/locale1/b/b/basic.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
msgid "foo"
msgstr "bar"

#, fuzzy
msgid "fuzz"
msgstr "fuzz"
6 changes: 6 additions & 0 deletions t/msgfmt/share/locale1/b/basic.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
msgid "foo"
msgstr "bar"

#, fuzzy
msgid "fuzz"
msgstr "fuzz"

0 comments on commit ad347f1

Please sign in to comment.