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

@ -7,6 +7,11 @@
test. An environment variable, CODE_COP, is used to enable the long
i18n/label.t and help/setHelp.t tests.
- documented the Deactivate Account Template.
- Added the setNamespace and getNamespace methods to WebGUI::International.
- Fixed bad caching via codespace in Operation::Help. The original failed all the time.
- Implemented codespace caching in WebGUI::International. This replaces the
in-memory cache by symbol table lookups into the code itself and saves
duplicating the i18n entries.
7.2.3
- fix: minor bug with new template vars in Auth::createAccount

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 ($@) {

48
t/International.t Normal file
View file

@ -0,0 +1,48 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2006 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use FindBin;
use strict;
use lib "$FindBin::Bin/lib";
use WebGUI::Test;
use WebGUI::Session;
use Test::More; # increment this value for each test you create
my $session = WebGUI::Test->session;
my $numTests = 1; ##For conditional load check
$numTests += 9;
plan tests => $numTests;
my $loaded = use_ok('WebGUI::International');
SKIP: {
skip 'Module was not loaded, skipping all tests', $numTests-1 unless $loaded;
my $i18n = WebGUI::International->new($session, undef, 'English');
isa_ok($i18n, 'WebGUI::International', 'object of correct type created');
is($i18n->getNamespace(), undef, 'getNamespace: default namespace is undef');
is($i18n->get('topicName'), 'WebGUI', 'get: get English label for topicName with default namespace: WebGUI');
$i18n->setNamespace('WebGUI');
is($i18n->getNamespace(), 'WebGUI', 'getNamespace: set namespace to WebGUI');
is($i18n->get('topicName'), 'WebGUI', 'get: get English label for topicName: WebGUI');
is($i18n->get('84'), 'Group Name', 'get: get English label for 84: Group Name');
$i18n->setNamespace('Asset');
is($i18n->getNamespace(), 'Asset', 'getNamespace: set namespace to Asset');
is($i18n->get('topicName'), 'Assets', 'get: get English label for topicName in Asset: Assets');
is($i18n->get('topicName', 'WebGUI'), 'WebGUI', 'get: test manual namespace override');
}