comparison Lib/IMPL/Web/View/TTContext.pm @ 349:86b470004d47

added lables loading
author cin
date Fri, 04 Oct 2013 17:06:34 +0400
parents f116cd9fe7d9
children cfd7570c2af2
comparison
equal deleted inserted replaced
348:f116cd9fe7d9 349:86b470004d47
1 package IMPL::Web::View::TTContext; 1 package IMPL::Web::View::TTContext;
2 use strict; 2 use strict;
3 use Template::Base; 3 use Template::Base;
4 use Carp qw(carp); 4 use Carp qw(carp);
5 use File::Spec();
6 use IMPL::Resources::Format qw(FormatMessage);
7 use IMPL::Resources::Strings();
5 8
6 use IMPL::Exception(); 9 use IMPL::Exception();
7 use IMPL::lang qw(is typeof hashApply); 10 use IMPL::lang qw(is typeof hashApply hashMerge);
8 use IMPL::declare { 11 use IMPL::declare {
9 require => { 12 require => {
10 Document => '-Template::Document', 13 Document => '-Template::Document',
11 TypeKeyedCollection => 'IMPL::TypeKeyedCollection', 14 TypeKeyedCollection => 'IMPL::TypeKeyedCollection',
12 ArgException => "-IMPL::InvalidArgumentException" 15 ArgException => '-IMPL::InvalidArgumentException',
16 Resources => 'IMPL::Resources'
13 }, 17 },
14 base => [ 18 base => [
15 'Template::Context' => '@_' 19 'Template::Context' => '@_'
16 ] 20 ]
17 }; 21 };
85 89
86 my $tt = eval { $this->template($file) }; 90 my $tt = eval { $this->template($file) };
87 91
88 return $cache->{$name} = { 92 return $cache->{$name} = {
89 base => $base, 93 base => $base,
94 labels => $this->load_labels($file),
90 template => $tt, 95 template => $tt,
91 } if $tt; 96 } if $tt;
92 } 97 }
93 98
94 $this->throw(Template::Constants::ERROR_FILE, "$name: not found"); 99 $this->throw(Template::Constants::ERROR_FILE, "$name: not found");
106 $args = shift; 111 $args = shift;
107 } 112 }
108 113
109 my $prefix = $this->prefix; 114 my $prefix = $this->prefix;
110 115
111 if (not(($args and delete $args->{'-no-resolve'}) or ref $model)) { 116 warn "no resolve" if $args and $args->{_no_resolve};
117
118 if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
112 $prefix = $prefix ? "${prefix}.${model}" : $model; 119 $prefix = $prefix ? "${prefix}.${model}" : $model;
113 $model = $this->resolve_model($model); 120 $model = $this->resolve_model($model);
114 } else { 121 } else {
115 $prefix = ""; 122 $prefix = "";
116 } 123 }
167 174
168 $args ||= {}; 175 $args ||= {};
169 176
170 #TODO handle classes 177 #TODO handle classes
171 178
172 my $base; 179 my ($base,$labels);
173 180
174 $template = $this->find_template($template) unless ref $template; 181 $template = $this->find_template($template) unless ref $template;
175 182
176 if (ref $template eq 'HASH') { 183 if (ref $template eq 'HASH') {
177 $base = $template->{base}; 184 $base = $template->{base};
185 $labels = $template->{labels};
178 $template = $template->{template}; 186 $template = $template->{template};
179 } else { 187 } else {
180 carp "got an invalid template object: $template"; 188 carp "got an invalid template object: $template";
181 $base = $this->base; 189 $base = $this->base;
182 } 190 }
183 191
184 return $this->invoke_environment( 192 return $this->invoke_environment(
185 sub { 193 sub {
186 return shift->include($template,$args); 194 return shift->include($template,$args);
187 }, 195 },
188 { 196 hashMerge(
189 base => $base, 197 $labels || {},
190 parent => $this 198 {
191 } 199 base => $base,
200 parent => $this
201 }
202 )
192 ) 203 )
193 } 204 }
194 205
195 sub resolve_model { 206 sub resolve_model {
196 my ($this,$prefix) = @_; 207 my ($this,$prefix) = @_;
238 } 249 }
239 250
240 return; 251 return;
241 } 252 }
242 253
254 sub get_real_file {
255 my ($this,$fname) = @_;
256
257 my @path = split(/\/+/,$fname);
258
259 foreach my $provider (@{$this->load_templates || []}) {
260 foreach my $dir (@{$provider->paths || []}) {
261 my $realName = File::Spec->catfile($dir,@path);
262 return $realName if -f $realName;
263 }
264 }
265 }
266
267 sub load_labels {
268 my ($this,$fname) = @_;
269
270 $fname = $this->get_real_file($fname);
271
272 my %vars;
273
274 my $flabels = "$fname.labels";
275
276 if (-f $flabels) {
277
278 my %labels;
279 $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels);
280
281 while(my($label,$text) = each %{$labels{default}}) {
282 $vars{$label} = sub {
283 my ($params) = @_;
284 my $locale = Resources->currentLocale;
285
286 unless ($labels{$locale}) {
287 $labels{$locale} = -f "$fname.$locale" ?
288 IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") :
289 {};
290 }
291
292 return FormatMessage(($labels{$locale}{$label} || $text),$params);
293 }
294 }
295 }
296
297 return \%vars;
298 }
299
243 1; 300 1;
244 301
245 __END__ 302 __END__
246 303
247 =pod 304 =pod