Mercurial > pub > Impl
diff Lib/IMPL/Resources/StringMap.pm @ 377:a0d342ac9a36
sync
author | cin |
---|---|
date | Tue, 14 Jan 2014 20:06:36 +0400 |
parents | a54a2faf2f7e |
children | 2eed076cb944 |
line wrap: on
line diff
--- a/Lib/IMPL/Resources/StringMap.pm Mon Jan 13 17:52:04 2014 +0400 +++ b/Lib/IMPL/Resources/StringMap.pm Tue Jan 14 20:06:36 2014 +0400 @@ -21,7 +21,7 @@ unless ref($data) eq 'HASH'; die ArgException->new( data => 'A hash must contain either scalars or subs') - if grep not($_) || (ref($_) and ref($_) ne 'CODE'), values %$data; + if ref($_) && ref($_) ne 'CODE', values %$data; $this->_data($data); $this->_parent($parent); @@ -33,11 +33,23 @@ if(my $format = $this->_data->{$id}) { return $this->FormatString($format,$args); } else { - return $this->_parent? $this->_parent->GetString($id,$args) : "[$id]"; + return $this->_parent? $this->_parent->GetString($id,$args) : "[ $id ]"; } } +sub AddFormat { + my ($this,$id,$format) = @_; + + die ArgException->new( id => 'A format id is required' ) + unless $id; + + die ArgException->new( format => 'A format must be a scalar or a sub' ) + if ref($format) and ref($format) ne 'CODE'; + + $this->_data->{$id} = $format; +} + sub FormatString { my ($self,$text,$args) = @_; @@ -95,13 +107,13 @@ =head1 SYNOPSIS -My/App/locale/default/Search.labels - -My/App/locale/en/Search.map - =begin code -{ +use IMPL::require { + StringMap => 'IMPL::Resources::StringMap' +}; + +my $data = { TitleLabel => 'Search results', ViewLabel => 'View %name%', # same as sub { $_[0]->Format('View %name%',$_[1]) } ResultsCountLabel => sub { @@ -114,15 +126,25 @@ } elsif($args->{count} == 1) { return "Found one item"; } else { - return $self->Format("Found %count% items", $args); + return $self->Format('Found %count% items', $args); } } } +my $def = StringMap->new({ + ResultsCountLabel => 'Found %count% items' +}); + +my $map = StringMap->new($data, $def); + +print $map->GetString('TitleLabel'); +print $map->GetString(ResultsCountLabel => { count => 0 }); # will print "No items found" + + =end code =head1 DESCRIPTION -=head +=head1 MEMBERS =cut \ No newline at end of file