diff Lib/IMPL/Resources/Strings.pm @ 378:2eed076cb944

rewritten IMPL::Resources::Strings + tests
author cin
date Wed, 15 Jan 2014 17:20:54 +0400
parents f4e14f32cf54
children
line wrap: on
line diff
--- a/Lib/IMPL/Resources/Strings.pm	Tue Jan 14 20:06:36 2014 +0400
+++ b/Lib/IMPL/Resources/Strings.pm	Wed Jan 15 17:20:54 2014 +0400
@@ -3,12 +3,10 @@
 
 use File::Spec;
 use List::Util qw(first);
-use IMPL::Resources::Format qw(FormatMessage);
 use IMPL::require {
-    Resources => 'IMPL::Resources'
+    StringMap => 'IMPL::Resources::StringLocaleMap'
 };
 
-our $Encoding ||= 'utf-8';
 our @Locations;
 my %maps;
 
@@ -21,37 +19,27 @@
     my $methods = $options{methods};
     
     if (ref $refStrings eq 'HASH') {
-        my $map = ( $maps{$class} ||= {} );
-        while(my ($name,$format) = each %$refStrings) {
-            $map->{default}{$name} = $format;
-            
-            *{"${class}::$name"} = sub {
+        my $map = $self->_GetMapForClass($class,$refStrings);
+        
+        while(my ($str,$format) = each %$refStrings) {
+            *{"${class}::$str"} = sub {
                 shift if $methods;
                 my $args = @_ == 1 ? shift : { @_ };
                 
-                return _FormatMapMessage($class,$name,$map,Resources->currentLocale,$args);
+                return $map->GetString($str,$args);
             }
         }
     }    
 }
 
-sub _FormatMapMessage {
-    my ($class,$msg,$map,$locale,$args) = @_;
-    
-    if (not exists $map->{$locale} ) {
-        $map->{$locale} = LoadStrings($class,$locale);        
-    }
-    
-    return FormatMessage( ($map->{$locale} || $map->{default})->{$msg}, $args );
-}
-
-sub LoadStrings {
-    my ($class,$locale) = @_;
-    
-    # Foo::Bar -> ('Foo','Bar')
-    my @classNamespace = split /::/,$class;
+sub _GetResourceLocations {
+	my ($self,$class) = @_;
+	
+	my @classNamespace = split /::/,$class;
     
     my $classShortName = pop @classNamespace;
+    
+    my @paths = map File::Spec->catdir($_,@classNamespace), @Locations;
 
     # Foo::Bar -> 'Foo/Bar.pm'    
     my $classModuleName = File::Spec->catfile(@classNamespace,"${classShortName}.pm");
@@ -59,51 +47,34 @@
     # 'Foo/Bar.pm' -> '/full/path/to/Foo/Bar.pm'
     my $fullModulePath = first { -f } map( File::Spec->catfile($_,$classModuleName), @INC );
     
-    my @ways = map {
-        my @path = ($_);
-        push @path,Resources->currentLocale;
-        
-        File::Spec->catfile($_,Resources->currentLocale,@classNamespace,$classShortName);
-    } @Locations;
-    
-    
     if ($fullModulePath) {
 
-        # '/full/path/to/Foo/Bar.pm' -> '/full/path/to/Foo' 
+        # '/full/path/to/Foo/Bar.pm' -> '/full/path/to/Foo/locale/' 
         my ($vol,$dir,$file) = File::Spec->splitpath($fullModulePath);
-        my $baseDir = File::Spec->catpath($vol,$dir,'');
-
-        # '/full/path/to/Foo' -> '/full/path/to/Foo/locale/En_US/Bar' 
-        push @ways, File::Spec->catfile($baseDir,'locale',Resources->currentLocale,$classShortName);
+        push @paths, File::Spec->catpath($vol,File::Spec->catdir($dir,'locale'),'');
     }
     
-    my $mapFile = first { -f } @ways;
-    
-    return unless $mapFile;
-    
-    return ParseStringsMap($mapFile);
+    return \@paths, $classShortName;
 }
 
-sub ParseStringsMap {
-    my ($fname) = @_;
-    
-    open my $hRes, "<:encoding($Encoding)", $fname or die "Failed to open file $fname: $!";
-    local $_;
-    my %Map;
-    my $line = 1;
-    while (<$hRes>) {
-        chomp;
-        $line ++ and next if /^\s*$/;
-        
-        if (/^(\w+)\s*=\s*(.*)$/) {
-            $Map{$1} = $2;
-        } else {
-            die "Invalid resource format in $fname at $line";
-        }
-        $line ++;
-    }
-    
-    return \%Map;
+sub _GetMapForClass {
+	my ($self,$class,$data) = @_;
+	
+	my $map;
+	
+	unless ($map) {
+	
+		my ($paths,$name) = $self->_GetResourceLocations($class);
+		
+		$map = StringMap->new($data);
+		$map->name($name);
+		$map->paths($paths);
+		
+		$maps{$class} = $map;
+	
+	}
+	
+	return $map;
 }
 
 1;