Mercurial > pub > Impl
comparison Lib/IMPL/Resources/StringMap.pm @ 377:a0d342ac9a36
sync
| author | cin |
|---|---|
| date | Tue, 14 Jan 2014 20:06:36 +0400 |
| parents | a54a2faf2f7e |
| children | 2eed076cb944 |
comparison
equal
deleted
inserted
replaced
| 376:a54a2faf2f7e | 377:a0d342ac9a36 |
|---|---|
| 19 | 19 |
| 20 die ArgException->new( data => 'A hash reference is required' ) | 20 die ArgException->new( data => 'A hash reference is required' ) |
| 21 unless ref($data) eq 'HASH'; | 21 unless ref($data) eq 'HASH'; |
| 22 | 22 |
| 23 die ArgException->new( data => 'A hash must contain either scalars or subs') | 23 die ArgException->new( data => 'A hash must contain either scalars or subs') |
| 24 if grep not($_) || (ref($_) and ref($_) ne 'CODE'), values %$data; | 24 if ref($_) && ref($_) ne 'CODE', values %$data; |
| 25 | 25 |
| 26 $this->_data($data); | 26 $this->_data($data); |
| 27 $this->_parent($parent); | 27 $this->_parent($parent); |
| 28 } | 28 } |
| 29 | 29 |
| 31 my ($this,$id,$args) = @_; | 31 my ($this,$id,$args) = @_; |
| 32 | 32 |
| 33 if(my $format = $this->_data->{$id}) { | 33 if(my $format = $this->_data->{$id}) { |
| 34 return $this->FormatString($format,$args); | 34 return $this->FormatString($format,$args); |
| 35 } else { | 35 } else { |
| 36 return $this->_parent? $this->_parent->GetString($id,$args) : "[$id]"; | 36 return $this->_parent? $this->_parent->GetString($id,$args) : "[ $id ]"; |
| 37 } | 37 } |
| 38 | 38 |
| 39 } | |
| 40 | |
| 41 sub AddFormat { | |
| 42 my ($this,$id,$format) = @_; | |
| 43 | |
| 44 die ArgException->new( id => 'A format id is required' ) | |
| 45 unless $id; | |
| 46 | |
| 47 die ArgException->new( format => 'A format must be a scalar or a sub' ) | |
| 48 if ref($format) and ref($format) ne 'CODE'; | |
| 49 | |
| 50 $this->_data->{$id} = $format; | |
| 39 } | 51 } |
| 40 | 52 |
| 41 sub FormatString { | 53 sub FormatString { |
| 42 my ($self,$text,$args) = @_; | 54 my ($self,$text,$args) = @_; |
| 43 | 55 |
| 93 | 105 |
| 94 C<IMPL::Web::Resources::StringMap> | 106 C<IMPL::Web::Resources::StringMap> |
| 95 | 107 |
| 96 =head1 SYNOPSIS | 108 =head1 SYNOPSIS |
| 97 | 109 |
| 98 My/App/locale/default/Search.labels | |
| 99 | |
| 100 My/App/locale/en/Search.map | |
| 101 | |
| 102 =begin code | 110 =begin code |
| 103 | 111 |
| 104 { | 112 use IMPL::require { |
| 113 StringMap => 'IMPL::Resources::StringMap' | |
| 114 }; | |
| 115 | |
| 116 my $data = { | |
| 105 TitleLabel => 'Search results', | 117 TitleLabel => 'Search results', |
| 106 ViewLabel => 'View %name%', # same as sub { $_[0]->Format('View %name%',$_[1]) } | 118 ViewLabel => 'View %name%', # same as sub { $_[0]->Format('View %name%',$_[1]) } |
| 107 ResultsCountLabel => sub { | 119 ResultsCountLabel => sub { |
| 108 my ($self,$args) = @_; | 120 my ($self,$args) = @_; |
| 109 | 121 |
| 112 if (not $args->{count}) { | 124 if (not $args->{count}) { |
| 113 return "No items found"; | 125 return "No items found"; |
| 114 } elsif($args->{count} == 1) { | 126 } elsif($args->{count} == 1) { |
| 115 return "Found one item"; | 127 return "Found one item"; |
| 116 } else { | 128 } else { |
| 117 return $self->Format("Found %count% items", $args); | 129 return $self->Format('Found %count% items', $args); |
| 118 } | 130 } |
| 119 } | 131 } |
| 120 } | 132 } |
| 133 | |
| 134 my $def = StringMap->new({ | |
| 135 ResultsCountLabel => 'Found %count% items' | |
| 136 }); | |
| 137 | |
| 138 my $map = StringMap->new($data, $def); | |
| 139 | |
| 140 print $map->GetString('TitleLabel'); | |
| 141 print $map->GetString(ResultsCountLabel => { count => 0 }); # will print "No items found" | |
| 142 | |
| 121 | 143 |
| 122 =end code | 144 =end code |
| 123 | 145 |
| 124 =head1 DESCRIPTION | 146 =head1 DESCRIPTION |
| 125 | 147 |
| 126 =head | 148 =head1 MEMBERS |
| 127 | 149 |
| 128 =cut | 150 =cut |
