added picklanguage macro to international

This commit is contained in:
Bart Jol 2009-10-23 16:16:01 +02:00 committed by Graham Knop
parent bbfcd3c9a2
commit 951439a017
3 changed files with 170 additions and 0 deletions

View file

@ -811,6 +811,7 @@
"Page" : "Page",
"PageTitle" : "PageTitle",
"PageUrl" : "PageUrl",
"PickLanguage" : "PickLanguage",
"RandomAssetProxy" : "RandomAssetProxy",
"RandomThread" : "RandomThread",
"RootTitle" : "RootTitle",

View file

@ -0,0 +1,65 @@
package WebGUI::Macro::PickLanguage; # edit this line to match your own macro name
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 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 strict;
=head1 NAME
Package WebGUI::Macro::PickLanguage
=head1 DESCRIPTION
This macro makes a link for each installed language so when clicked the SetLanguage contetntHandler is called and sets the language in the scratch. The link text is the label from the language.
=head2 process( $session )
The main macro class, Macro.pm, will call this subroutine and pass it
=over 4
=item *
A session variable
=item templateId
This macro takes a templateId to show the links
=back
=cut
#-------------------------------------------------------------------
sub process {
my $session = shift;
my $templateId = shift;
my $template = WebGUI::Asset::Template->new($session, $templateId);
return "Could not instanciate template with id [$templateId]" unless $template;
my $i18n = WebGUI::International->new($session);
my $languages = $i18n->getLanguages();
my $vars = {'lang_loop' => []};
foreach my $language ( keys %$languages ) {
my $langVars = {};
$langVars->{ 'language_url' } = '?op=setLanguage;language=' . $language ;
$langVars->{ 'language_lang' } = $i18n->getLanguage($language , 'label');
$langVars->{ 'language_langAbbr' } = $i18n->getLanguage($language, 'languageAbbreviation');
$langVars->{ 'language_langAbbrLoc' } = $i18n->getLanguage($language, 'locale');
$langVars->{ 'language_langEng' } = $language;
push(@{$vars->{lang_loop}}, $langVars);
}
return $template->process($vars);
}
1;
#vim:ft=perl

104
t/Macro/PickLanguage.t Normal file
View file

@ -0,0 +1,104 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 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 WebGUI::Asset::Template;
use Test::More; # increment this value for each test you create
use Test::Deep;
use Test::MockObject;
use Test::MockObject::Extends;
my $session = WebGUI::Test->session;
my $numTests = 3;
$numTests += 1; #For the use_ok
plan tests => $numTests;
my $macro = 'WebGUI::Macro::PickLanguage';
my $loaded = use_ok($macro);
my $macroMock = Test::MockObject->new({});
$macroMock->set_isa('WebGUI::Macro::PickLanguage');
$macroMock->set_true('process');
#test for normal use
my $templateId = 'PICKLANGUAGE_TEMPLATE_';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
WebGUI::Macro::PickLanguage::process($session,$templateMock->getId);
cmp_deeply(
$templateVars,
{
lang_loop => [
{ 'language_url' => '?op=setLanguage;language=English',
'language_lang' => 'English',
'language_langAbbr' => 'en',
'language_langAbbrLoc' => 'US',
'language_langEng' => 'English'
},
],
},
'some template variables are created'
);
WebGUI::Test->unmockAssetId($templateId);
}
#test when template Id is left empty
$templateId = '';
my $templateNoId = $templateMock->mock('process','');
$templateMock->set_always('getId', $templateId);
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
my $error;
{
WebGUI::Test->mockAssetId($templateNoId, $templateMock);
$error = WebGUI::Macro::PickLanguage::process($session,$templateMock->getId);
is($error,'Could not instanciate template with id []',"Empty template Id should return error");
WebGUI::Test->unmockAssetId($templateNoId);
}
#test for an incorrect template Id
$templateId = '1234567890123456789012';
my $templateWrongId = $templateMock->mock('process','');
$templateMock->set_always('getId', $templateId);
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
my $error;
{
WebGUI::Test->mockAssetId($templateWrongId, $templateMock);
$error = WebGUI::Macro::PickLanguage::process($session,$templateMock->getId);
is($error,'Could not instanciate template with id [1234567890123456789012]',"Template from the wrong namespace should not be initiated");
WebGUI::Test->unmockAssetId($templateWrongId);
}