comparison Lib/IMPL/Web/View/TTContext.pm @ 380:1eca08048ba9

TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
author cin
date Fri, 17 Jan 2014 15:58:57 +0400
parents d5c8b955bf8d
children ced5937ff21a
comparison
equal deleted inserted replaced
379:a471e8b77544 380:1eca08048ba9
14 TypeKeyedCollection => 'IMPL::TypeKeyedCollection', 14 TypeKeyedCollection => 'IMPL::TypeKeyedCollection',
15 ArgException => '-IMPL::InvalidArgumentException', 15 ArgException => '-IMPL::InvalidArgumentException',
16 Resources => 'IMPL::Resources', 16 Resources => 'IMPL::Resources',
17 Loader => 'IMPL::Code::Loader', 17 Loader => 'IMPL::Code::Loader',
18 MetadataBase => '-IMPL::Web::View::Metadata::BaseMeta', 18 MetadataBase => '-IMPL::Web::View::Metadata::BaseMeta',
19 Metadata => 'IMPL::Web::View::Metadata::ObjectMeta' 19 Metadata => 'IMPL::Web::View::Metadata::ObjectMeta',
20 StringMap => 'IMPL::Resources::StringLocaleMap'
20 }, 21 },
21 base => [ 22 base => [
22 'Template::Context' => '@_' 23 'Template::Context' => '@_'
23 ] 24 ]
24 }; 25 };
36 prefix 37 prefix
37 cache 38 cache
38 includes 39 includes
39 modules 40 modules
40 aliases 41 aliases
41
42 id 42 id
43 metadata 43 metadata
44 model 44 model
45 templateInfo
45 )) { 46 )) {
46 my $t = $prop; 47 my $t = $prop;
47 48
48 *{__PACKAGE__ . '::' . $prop} = sub { 49 *{__PACKAGE__ . '::' . $prop} = sub {
49 my $this = shift; 50 my $this = shift;
91 if(my $tpl = $cache->{$name}) { 92 if(my $tpl = $cache->{$name}) {
92 return $tpl; 93 return $tpl;
93 } 94 }
94 95
95 my @inc = ($this->base, @{$this->includes || []}); 96 my @inc = ($this->base, @{$this->includes || []});
97 #my @inc = @{$this->includes || []};
96 98
97 my $ext = $this->tt_ext || ""; 99 my $ext = $this->tt_ext || "";
98 100
99 my $file; 101 my $file;
100 102
101 foreach my $dir (@inc) { 103 foreach my $dir (@inc) {
102 $file = $dir ? "$dir/$name" : $name; 104 $file = $dir ? "$dir/$name" : $name;
103 105
104 my $base = join('/',splice([split(/\/+/,$file)],0,-1)); 106 my @parts = split(/\/+/,$file);
107
108 my $templateName = pop @parts;
109
110 my $base = join('/',@parts);
105 111
106 $file = $ext ? "$file.$ext" : $file; 112 $file = $ext ? "$file.$ext" : $file;
107 113
108 if (exists($this->modules->{$file})) { 114 if (exists($this->modules->{$file})) {
109 my $info = $this->modules->{$file}; 115 my $info = $this->modules->{$file};
116 $class = $this->aliases->{$class} || $class; 122 $class = $this->aliases->{$class} || $class;
117 Loader->safe->Require($class); 123 Loader->safe->Require($class);
118 } 124 }
119 my $info = { 125 my $info = {
120 base => $base, 126 base => $base,
121 labels => $this->load_labels($file), 127 name => $templateName,
122 template => $tt, 128 template => $tt,
123 initialized => 0, 129 initialized => 0,
124 class => $class 130 class => $class,
131 file => $file
125 }; 132 };
126 $this->modules->{$file} = $info; 133 $this->modules->{$file} = $info;
127 return $cache->{$name} = $info; 134 return $cache->{$name} = $info;
128 } else { 135 } else {
129 my $err = $@; 136 my $err = $@;
244 $ctx->render(@_); 251 $ctx->render(@_);
245 }, 252 },
246 display_model => sub { 253 display_model => sub {
247 $ctx->display_model(@_); 254 $ctx->display_model(@_);
248 }, 255 },
249 tt_cache => {} 256 tt_cache => {},
257 labels => sub {
258 $ctx->load_labels(@_);
259 }
250 }, 260 },
251 $env 261 $env
252 ) 262 )
253 ); 263 );
254 264
307 if (my $class = $info->{class}) { 317 if (my $class = $info->{class}) {
308 $class->new($ctx,$info->{template},$args)->Render({}); 318 $class->new($ctx,$info->{template},$args)->Render({});
309 } else { 319 } else {
310 return $ctx->include($info->{template},$args); 320 return $ctx->include($info->{template},$args);
311 } 321 }
312 }, 322 },
313 hashMerge( 323 {
314 $info->{labels} || {}, 324 base => $info->{base},
315 { 325 parent => $this,
316 base => $info->{base}, 326 id => $this->get_next_id,
317 parent => $this, 327 templateInfo => $info
318 id => $this->get_next_id 328 }
319 }
320 )
321 ) 329 )
322 } 330 }
323 331
324 sub resolve_model { 332 sub resolve_model {
325 my ($this,$prefix) = @_; 333 my ($this,$prefix) = @_;
391 } 399 }
392 400
393 sub get_real_file { 401 sub get_real_file {
394 my ($this,$fname) = @_; 402 my ($this,$fname) = @_;
395 403
404 return unless length $fname;
405
396 my @path = split(/\/+/,$fname); 406 my @path = split(/\/+/,$fname);
397 407
398 foreach my $provider (@{$this->load_templates || []}) { 408 foreach my $provider (@{$this->load_templates || []}) {
399 foreach my $dir (@{$provider->paths || []}) { 409 foreach my $dir (@{$provider->paths || []}) {
400 my $realName = File::Spec->catfile($dir,@path); 410 my $realName = File::Spec->catfile($dir,@path);
402 } 412 }
403 } 413 }
404 } 414 }
405 415
406 sub load_labels { 416 sub load_labels {
407 my ($this,$fname) = @_; 417 my ($this,$data) = @_;
408 418
409 $fname = $this->get_real_file($fname); 419 die ArgException->new("A hash reference is required")
410 420 unless ref($data) eq 'HASH';
411 my %vars; 421
412 422 my $stringMap = StringMap->new($data);
413 my $flabels = "$fname.labels"; 423
414 424 $this->stash->update({
415 if (-f $flabels) { 425 map {
416 426 my $id = $_;
417 my %labels; 427 $id,
418 $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels); 428 sub {
419 429 $stringMap->GetString($id,@_);
420 while(my($label,$text) = each %{$labels{default}}) { 430 };
421 $vars{$label} = sub { 431 } keys %$data
422 my ($params) = @_; 432 });
423 my $locale = Resources->currentLocale; 433
424 434 my $ti = $this->templateInfo || {};
425 unless ($labels{$locale}) { 435
426 $labels{$locale} = -f "$fname.$locale" ? 436 if (my $fullName = $this->get_real_file($ti->{file})) {
427 IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") : 437 my ($vol,$dir,$fname) = File::Spec->splitpath($fullName);
428 {}; 438
429 } 439 my $name = $this->templateInfo->{name};
430 440
431 return FormatMessage(($labels{$locale}{$label} || $text),$params); 441 my $localePath = File::Spec->catpath($vol, File::Spec->catdir($dir,'locale'),'');
432 } 442
433 } 443 $stringMap->name($name);
434 } 444 $stringMap->paths($localePath);
435 445 }
436 return \%vars; 446 return;
437 } 447 }
438 448
439 1; 449 1;
440 450
441 __END__ 451 __END__