Mercurial > pub > Impl
diff Lib/IMPL/Resources/Strings.pm @ 245:7c517134c42f
Added Unsupported media type Web exception
corrected resourceLocation setting in the resource
Implemented localizable resources for text messages
fixed TT view scopings, INIT block in controls now sets globals correctly.
author | sergey |
---|---|
date | Mon, 29 Oct 2012 03:15:22 +0400 |
parents | 2b9b55cfb79b |
children | bbc0da7ef90e |
line wrap: on
line diff
--- a/Lib/IMPL/Resources/Strings.pm Mon Oct 22 04:09:27 2012 +0400 +++ b/Lib/IMPL/Resources/Strings.pm Mon Oct 29 03:15:22 2012 +0400 @@ -1,53 +1,87 @@ use strict; use warnings; -package IMPL::Resources::Strings::Storage; package IMPL::Resources::Strings; + use File::Spec; +use List::Util qw(first); +use IMPL::Resources::Format qw(FormatMessage); our $Locale ||= 'default'; -our $Base ||='locale'; our $Encoding ||= 'utf-8'; our @Locations; -@Locations = ('.') unless @Locations; - sub import { my ($self,$refStrings,%options) = @_; - my ($class,$pathModule) = caller; + no strict 'refs'; + + my $class = caller; + + if (ref $refStrings eq 'HASH') { + my %map; + while(my ($name,$format) = each %$refStrings) { + $map{default}{$name} = $format; + + *{"${class}::$name"} = sub { + my $args = @_ == 1 ? shift : { @_ }; + + return _FormatMapMessage($class,$name,\%map,$Locale,$args); + } + } + } +} + +sub _FormatMapMessage { + my ($class,$msg,$map,$locale,$args) = @_; - my ($vol,$dir,$file) = File::Spec->splitpath($pathModule); - my $baseDir = File::Spec->catpath($vol,$dir,''); + if (not exists $map->{$locale} ) { + $map->{$locale} = LoadStrings($class,$locale); + } + + return FormatMessage( ($map->{$locale} || $map->{default})->{$msg}, $args ); +} + +sub LoadStrings { + my ($class,$locale) = @_; - my @pathClass = split /::/,$class; - my $fileClass = pop @pathClass; + # Foo::Bar -> ('Foo','Bar') + my @classNamespace = split /::/,$class; + + my $classShortName = pop @classNamespace; + + # Foo::Bar -> 'Foo/Bar.pm' + my $classModuleName = File::Spec->catfile(@classNamespace,"${classShortName}.pm"); + + # '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,$Base; push @path,$Locale; - File::Spec->catfile(@path,@pathClass,$fileClass); + File::Spec->catfile($_,$Locale,@classNamespace,$classShortName); } @Locations; - push @ways, File::Spec->catfile($baseDir,'locale',$Locale,$fileClass); - - - my $stringsStorage = findResource($class,@ways); + if ($fullModulePath) { + + # '/full/path/to/Foo/Bar.pm' -> '/full/path/to/Foo' + 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',$Locale,$classShortName); + } + my $mapFile = first { -f } @ways; + + return unless $mapFile; + + return ParseStringsMap($mapFile); } -sub findResource { - my ($class,$refWays) = @_; - - -} - - - -sub parseResource { +sub ParseStringsMap { my ($fname) = @_; open my $hRes, "<:encoding($Encoding)", findFile($fname) or die "Failed to open file $fname: $!"; @@ -69,13 +103,6 @@ return \%Map; } -package IMPL::Resources::Strings::Storage; -use parent qw(IMPL::Object); - -sub get { - my ($this,$msg_name) = @_; -} - 1; __END__ @@ -93,7 +120,7 @@ package Foo; use IMPL::Resources::Strings { - msg_say_hello => "Hello, %name!", + msg_say_hello => "Hello, %name%!", msg_module_name => "Simple Foo class" }, auto => 1, locale => 'en-US'; @@ -114,7 +141,7 @@ При импорте ищутся модули по следующему алгоритму: В каталогах из массива C<@Locations> ищется файл с относительным путем -C<$Base/$Locale/$ModName>, где C<$Base>, C<$Locale> - глобальные переменные +C<$Locale/$ModName>, где C<$Locale> - глобальная переменная модуля C<IMPL::Resourses::Strings>, а переменная C<$ModName> получена путем замены 'C<::>' в имени целевого модуля на 'C</>'.