Added setNamespace and getNamespace methods to WebGUI::International.

Added a baseline test for WebGUI::International which also tests the new methods.
Fixed codespace caching in WebGUI::Operation::Help.  Codespace caching is
done via symbol table lookups, and it saves doing an eval.
Implemented the same type of caching in WebGUI::International.  This replaced
the in-memory cache which would slowly accumulate a complete copy of the
i18n entries as they were fetched.
This commit is contained in:
Colin Kuskie 2006-11-26 04:07:32 +00:00
parent 2f1408e852
commit 85eb6ddbeb
4 changed files with 98 additions and 7 deletions

View file

@ -17,8 +17,6 @@ package WebGUI::International;
use strict qw(vars subs);
our %i18nCache;
=head1 NAME
Package WebGUI::International
@ -102,8 +100,14 @@ sub get {
$id =~ s/$safeRe//g;
$language =~ s/$safeRe//g;
$namespace =~ s/$safeRe//g;
return $i18nCache{$language}{$namespace}{$id}{message} if $i18nCache{$language}{$namespace}{$id}{message};
my $cmd = "WebGUI::i18n::".$language."::".$namespace;
use Data::Dumper;
if (defined *{"$cmd\::I18N"}) { ##Symbol table lookup
our $table;
*table = *{"$cmd\::I18N"}; ##Create alias into symbol table
return $table->{$id}->{message}; ##return key
}
my $load = "use ".$cmd;
eval($load);
$self->session->errorHandler->warn($cmd." failed to compile because ".$@) if ($@);
@ -111,7 +115,6 @@ sub get {
my $output = eval($cmd);
$self->session->errorHandler->warn("Couldn't get value from ".$cmd." because ".$@) if ($@);
$output = $self->get($id,$namespace,"English") if ($output eq "" && $language ne "English");
$i18nCache{$language}{$namespace}{$id}{message} = $output;
return $output;
}
@ -153,6 +156,19 @@ sub getLanguage {
}
#-------------------------------------------------------------------
=head2 getNamespace ( )
Returns the default namespace set in the object when created.
=cut
sub getNamespace {
my ($self) = @_;
return $self->{_namespace};
}
#-------------------------------------------------------------------
=head2 getLanguages ( )
@ -208,6 +224,23 @@ sub makeUrlCompliant {
}
#-------------------------------------------------------------------
=head2 setNamespace ( namespace )
Set the default namespace for pulling internationalized labels.
=head3 namespace
The namespace to make the new default.
=cut
sub setNamespace {
my ($self, $namespace) = @_;
$self->{_namespace} = $namespace;
}
#-------------------------------------------------------------------
=head2 new ( session, [ namespace, language ] )
@ -222,7 +255,7 @@ The current user's session variable
=head3 namespace
Specify a default namespace. Defaults to "WebGUI".
Specify a default namespace.
=head3 language

View file

@ -10,7 +10,7 @@ package WebGUI::Operation::Help;
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use strict qw(vars subs);
use Tie::IxHash;
use WebGUI::AdminConsole;
use WebGUI::International;
@ -41,7 +41,12 @@ been already and logs errors during the load.
sub _loadHelp {
my $session = shift;
my $helpPackage = shift;
return $helpPackage::HELP if defined $helpPackage::HELP; ##Already loaded
if (defined *{"$helpPackage\::HELP"}) { ##Symbol table lookup
our $table;
*table = *{"$helpPackage\::HELP"}; ##Create alias into symbol table
return $table; ##return whole hashref
}
$session->errorHandler->warn("cache miss for $helpPackage");
my $load = sprintf 'use %-s; $%-s::HELP', $helpPackage, $helpPackage;
my $help = eval($load);
if ($@) {