annotate Lib/IMPL/Resources/StringMap.pm @ 376:a54a2faf2f7e

added localizable string maps
author cin
date Mon, 13 Jan 2014 17:52:04 +0400
parents
children a0d342ac9a36
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
376
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
1 package IMPL::Web::Resources::StringMap;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
2 use strict;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
3
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
4 use IMPL::Const qw(:prop);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
5 use IMPL::declare {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
6 require => {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
7 Exception => 'IMPL::Exception',
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
8 IOException => '-IMPL::IOException',
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
9 ArgException => '-IMPL::InvalidArgumentException'
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
10 },
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
11 props => [
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
12 _data => PROP_RW,
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
13 _parent => PROP_RW
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
14 ]
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
15 };
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
16
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
17 sub CTOR {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
18 my ($this,$data,$parent) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
19
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
20 die ArgException->new( data => 'A hash reference is required' )
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
21 unless ref($data) eq 'HASH';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
22
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
23 die ArgException->new( data => 'A hash must contain either scalars or subs')
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
24 if grep not($_) || (ref($_) and ref($_) ne 'CODE'), values %$data;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
25
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
26 $this->_data($data);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
27 $this->_parent($parent);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
28 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
29
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
30 sub GetString {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
31 my ($this,$id,$args) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
32
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
33 if(my $format = $this->_data->{$id}) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
34 return $this->FormatString($format,$args);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
35 } else {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
36 return $this->_parent? $this->_parent->GetString($id,$args) : "[$id]";
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
37 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
38
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
39 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
40
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
41 sub FormatString {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
42 my ($self,$text,$args) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
43
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
44 $args ||= {};
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
45 $resolver ||= \&_defaultResolver;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
46 $text ||= '';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
47
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
48 $string =~ s/%(\w+(?:\.\w+)*)%/$self->GetValue($args,$1,"\[$1\]")/ge;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
49
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
50 return $string;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
51
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
52 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
53
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
54 sub GetValue {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
55 my ($self,$obj,$path,$default) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
56
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
57 foreach my $chunk (split /\./,$path) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
58 return $default unless $obj;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
59 if (ref $obj eq 'HASH') {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
60 $obj = $obj->{$chunk};
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
61 } else {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
62 $obj = $self->Resolve($obj,$chunk);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
63 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
64 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
65 return $obj||'<undef>';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
66 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
67
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
68 sub Resolve {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
69 my ($self,$obj,$prop) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
70
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
71 return ( eval { $obj->can($prop) } ? $obj->$prop() : undef );
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
72 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
73
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
74 sub _LoadMap {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
75 my ($self,$file,$parent) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
76
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
77 my $data = do $file;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
78 my $e = $@;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
79 die Exception->new("Failed to load file '$file'", $e) if $e;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
80 die IOException->new("Failed to load file '$file'", $!) if not defined $data and $!;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
81 die Exception->new("Failed to load file '$file'", "A hash data is expected") unless ref($data) eq 'HASH';
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
82
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
83 return $self->new($data,$parent);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
84 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
85
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
86 1;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
87
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
88 __END__
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
89
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
90 =pod
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
91
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
92 =head1 NAME
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
93
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
94 C<IMPL::Web::Resources::StringMap>
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
95
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
96 =head1 SYNOPSIS
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
97
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
98 My/App/locale/default/Search.labels
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
99
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
100 My/App/locale/en/Search.map
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
101
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
102 =begin code
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
103
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
104 {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
105 TitleLabel => 'Search results',
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
106 ViewLabel => 'View %name%', # same as sub { $_[0]->Format('View %name%',$_[1]) }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
107 ResultsCountLabel => sub {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
108 my ($self,$args) = @_;
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
109
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
110 $args ||= {};
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
111
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
112 if (not $args->{count}) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
113 return "No items found";
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
114 } elsif($args->{count} == 1) {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
115 return "Found one item";
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
116 } else {
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
117 return $self->Format("Found %count% items", $args);
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
118 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
119 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
120 }
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
121
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
122 =end code
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
123
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
124 =head1 DESCRIPTION
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
125
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
126 =head
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
127
a54a2faf2f7e added localizable string maps
cin
parents:
diff changeset
128 =cut