Mercurial > pub > Impl
changeset 318:1838bdb4d238
corrected support of resources localization
author | cin |
---|---|
date | Tue, 14 May 2013 03:38:47 +0400 |
parents | 96a522aeb359 |
children | d485467eca92 |
files | Lib/IMPL/Resources.pm Lib/IMPL/Resources/Strings.pm |
diffstat | 2 files changed, 26 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/Lib/IMPL/Resources.pm Thu May 09 04:10:00 2013 +0400 +++ b/Lib/IMPL/Resources.pm Tue May 14 03:38:47 2013 +0400 @@ -1,59 +1,28 @@ package IMPL::Resources; use strict; -use warnings; -our $Encoding ||= 'utf-8'; -our %Files; - -my %Data; - - - foreach my $group (keys %Files) { - $Data{$group} = ParseResource($Files{$group}); - } +our $CurrentLocale ||= 'default'; -sub findFile { - my ($fname) = @_; - - foreach my $dir (',',@INC) { - my $fullfname = "$dir/$fname"; - return $fullfname if -f $fullfname; - } - - return $fname; -} - -sub ParseResource { - my ($fname) = @_; - - open my $hRes, "<:encoding($Encoding)", findFile($fname) or die "Failed to open file $fname: $!"; - - 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 CurrentLocale { + $CurrentLocale; } -sub import { - my ($class,@groups) = @_; - my $caller = caller; - my %merged = map %{$Data{$_} || {} }, @groups; +sub SetLocale { + my ($self,$locale) = @_; - no strict 'refs'; - foreach my $item ( keys %merged ) { - *{"${caller}::ids_$item"} = sub { sprintf($merged{$item},@_) } - } + $CurrentLocale = $locale; +} + +sub InvokeInLocale { + my ($this,$locale,$code) = @_; + + local $CurrentLocale = $locale; + eval { + &$code() + if $code; + }; + die $@ + if $@; } 1;
--- a/Lib/IMPL/Resources/Strings.pm Thu May 09 04:10:00 2013 +0400 +++ b/Lib/IMPL/Resources/Strings.pm Tue May 14 03:38:47 2013 +0400 @@ -1,13 +1,13 @@ +package IMPL::Resources::Strings; use strict; -use warnings; - -package IMPL::Resources::Strings; use File::Spec; use List::Util qw(first); use IMPL::Resources::Format qw(FormatMessage); +use IMPL::require { + Resources => 'IMPL::Resources' +}; -our $Locale ||= 'default'; our $Encoding ||= 'utf-8'; our @Locations; my %maps; @@ -29,7 +29,7 @@ shift if $methods; my $args = @_ == 1 ? shift : { @_ }; - return _FormatMapMessage($class,$name,$map,$Locale,$args); + return _FormatMapMessage($class,$name,$map,Resources->currentLocale,$args); } } } @@ -61,9 +61,9 @@ my @ways = map { my @path = ($_); - push @path,$Locale; + push @path,Resources->currentLocale; - File::Spec->catfile($_,$Locale,@classNamespace,$classShortName); + File::Spec->catfile($_,Resources->currentLocale,@classNamespace,$classShortName); } @Locations; @@ -74,7 +74,7 @@ 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); + push @ways, File::Spec->catfile($baseDir,'locale',Resources->currentLocale,$classShortName); } my $mapFile = first { -f } @ways;