diff 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
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/TTContext.pm	Thu Oct 03 19:48:57 2013 +0400
+++ b/Lib/IMPL/Web/View/TTContext.pm	Fri Oct 04 17:06:34 2013 +0400
@@ -2,14 +2,18 @@
 use strict;
 use Template::Base;
 use Carp qw(carp);
+use File::Spec();
+use IMPL::Resources::Format qw(FormatMessage);
+use IMPL::Resources::Strings();
 
 use IMPL::Exception();
-use IMPL::lang qw(is typeof hashApply);
+use IMPL::lang qw(is typeof hashApply hashMerge);
 use IMPL::declare {
 	require => {
 	   Document => '-Template::Document',
 	   TypeKeyedCollection => 'IMPL::TypeKeyedCollection',
-	   ArgException => "-IMPL::InvalidArgumentException"
+	   ArgException => '-IMPL::InvalidArgumentException',
+	   Resources => 'IMPL::Resources'
 	},
 	base => [
 		'Template::Context' => '@_'
@@ -87,6 +91,7 @@
 		
 		return $cache->{$name} = {
 			base => $base,
+			labels => $this->load_labels($file),
 			template => $tt,
 		} if $tt;
 	}
@@ -108,7 +113,9 @@
 	
 	my $prefix = $this->prefix;
 	
-	if (not(($args and delete $args->{'-no-resolve'}) or ref $model)) {
+	warn "no resolve" if $args and $args->{_no_resolve};
+	
+	if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
 		$prefix = $prefix ? "${prefix}.${model}" : $model;
 		$model = $this->resolve_model($model);
 	} else {
@@ -169,12 +176,13 @@
 	
 	#TODO handle classes
 	
-	my $base;
+	my ($base,$labels);
 	
 	$template = $this->find_template($template) unless ref $template;
 	
 	if (ref $template eq 'HASH') {
         $base = $template->{base};
+        $labels = $template->{labels};
         $template = $template->{template};
     } else {
         carp "got an invalid template object: $template";
@@ -185,10 +193,13 @@
 	   sub {
 	       return shift->include($template,$args);
 	   },
-	   {
-	   	base => $base,
-	   	parent => $this
-	   }
+	   hashMerge(
+	       $labels || {},
+		   {
+		   	base => $base,
+		   	parent => $this
+		   }
+	   )
 	)
 }
 
@@ -240,6 +251,52 @@
 	return;
 }
 
+sub get_real_file {
+	my ($this,$fname) = @_;
+	
+	my @path = split(/\/+/,$fname);
+	
+	foreach my $provider (@{$this->load_templates || []}) {
+		foreach my $dir (@{$provider->paths || []}) {
+			my $realName = File::Spec->catfile($dir,@path);
+			return $realName if -f $realName; 
+		}
+	}
+}
+
+sub load_labels {
+    my ($this,$fname) = @_;
+    
+    $fname = $this->get_real_file($fname);
+    
+    my %vars;
+    
+    my $flabels = "$fname.labels";
+        
+    if (-f $flabels) {
+        
+        my %labels;
+        $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels);
+        
+        while(my($label,$text) = each %{$labels{default}}) {
+            $vars{$label} = sub {
+                my ($params) = @_;
+                my $locale = Resources->currentLocale;
+            
+                unless ($labels{$locale}) {
+                $labels{$locale} = -f "$fname.$locale" ? 
+                    IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") :
+                    {};
+                }
+                    
+                return FormatMessage(($labels{$locale}{$label} || $text),$params);
+            }
+        }
+    }
+    
+    return \%vars;
+}
+
 1;
 
 __END__