From 8d09a801541287ac0c3fc20ed7a16eef765f0759 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 19 Feb 2010 11:41:45 -0600 Subject: allow setting defaults for the generated stub msgstrs --- lib/Locale/POFileManager.pm | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'lib/Locale/POFileManager.pm') diff --git a/lib/Locale/POFileManager.pm b/lib/Locale/POFileManager.pm index 67a63d9..ea906c1 100644 --- a/lib/Locale/POFileManager.pm +++ b/lib/Locale/POFileManager.pm @@ -1,6 +1,7 @@ package Locale::POFileManager; use Moose; use MooseX::Types::Path::Class qw(Dir); +use Scalar::Util qw(reftype weaken); =head1 NAME @@ -44,7 +45,11 @@ sub _build_files { for my $file ($dir->children) { next if $file->is_dir; next unless $file->stringify =~ /\.po$/; - push @files, Locale::POFileManager::File->new(file => $file); + my $msgstr = $self->stub_msgstr; + push @files, Locale::POFileManager::File->new( + file => $file, + defined($msgstr) ? (stub_msgstr => $msgstr) : (), + ); } return \@files; @@ -56,6 +61,12 @@ has canonical_language => ( required => 1, # TODO: make this not required at some point? ); +has _stub_msgstr => ( + is => 'ro', + isa => 'Str|CodeRef', + init_arg => 'stub_msgstr', +); + sub BUILD { my $self = shift; @@ -63,6 +74,27 @@ sub BUILD { unless $self->has_language($self->canonical_language); } +sub stub_msgstr { + my $self = shift; + my $msgstr = $self->_stub_msgstr; + return unless defined($msgstr); + return $msgstr if !reftype($msgstr); + my $weakself = $self; + weaken($weakself); + return sub { + my %args = @_; + my $canonical_msgstr; + $canonical_msgstr = + $weakself->canonical_language_file->entry_for($args{msgid})->msgstr + if $weakself; + $canonical_msgstr =~ s/^"|"$//g if defined($canonical_msgstr); + return $msgstr->( + %args, + defined($canonical_msgstr) ? (canonical_msgstr => $canonical_msgstr) : (), + ); + } +} + sub has_language { my $self = shift; my ($lang) = @_; @@ -84,7 +116,11 @@ sub add_language { confess("Can't overwrite existing language file for $lang") if -e $file->stringify; - my $pofile = Locale::POFileManager::File->new(file => $file); + my $msgstr = $self->stub_msgstr; + my $pofile = Locale::POFileManager::File->new( + file => $file, + defined($msgstr) ? (stub_msgstr => $msgstr) : (), + ); $pofile->save; $self->_add_file($pofile); -- cgit v1.2.3-54-g00ecf