added internationalized url handling

This commit is contained in:
JT Smith 2004-11-05 00:00:33 +00:00
parent a56ccd9b9e
commit 90419bb59a
3 changed files with 62 additions and 15 deletions

View file

@ -15,7 +15,7 @@ package WebGUI::International;
=cut
use strict;
use strict qw(vars subs);
use WebGUI::Session;
@ -33,12 +33,14 @@ This package provides an interface to the internationalization system.
$string = WebGUI::International::get($internationalId,$namespace);
$hashRef = WebGUI::International::getLanguage($lang);
$hashRef = WebGUI::International::getLanguages();
$url = WebGUI::International::makeUrlCompliant($url);
This package can also be used in object-oriented (OO) style.
use WebGUI::International;
my $i = WebGUI::International->new($namespace);
$i->get($internationalId);
$string = $i->get($internationalId);
$url = $i->makeUrlCompliant($url);
=head1 METHODS
@ -151,6 +153,42 @@ sub getLanguages {
}
#-------------------------------------------------------------------
=head2 makeUrlCompliant ( url [ , language ] )
Manipulates a URL to make sure it will work on the internet. It removes things like non-latin characters, etc.
=head3 url
The URL to manipulate.
=head3 languageId
Specify a default language. Defaults to user preference.
=cut
sub makeUrlCompliant {
my ($language, $url);
if (ref($_[0]) eq "WebGUI::International") {
$url = $_[1];
$language = $_[2] || $_[0]->{_language} || $session{user}{language} || "English";
} else {
$url = $_[0];
$language = $_[1] || $session{user}{language} || "English";
}
my $cmd = "WebGUI::i18n::".$language;
my $load = "use ".$cmd;
eval($load);
WebGUI::ErrorHandler::warn($cmd." failed to compile because ".$@) if ($@);
$cmd = $cmd."::makeUrlCompliant";
my $output = eval{&$cmd($url)};
WebGUI::ErrorHandler::warn("Couldn't execute ".$cmd." because ".$@) if ($@);
return $output;
}
#-------------------------------------------------------------------
=head2 new ( [ namespace, languageId ] )

View file

@ -18,6 +18,7 @@ package WebGUI::URL;
use strict;
use URI;
use URI::Escape;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::Utility;
@ -217,7 +218,7 @@ sub getSiteURL {
=head2 makeCompliant ( string )
Returns a string that has made into a WebGUI compliant URL.
Returns a string that has made into a WebGUI compliant URL based upon the language being submitted.
=head3 string
@ -226,17 +227,8 @@ The string to make compliant. This is usually a page title or a filename.
=cut
sub makeCompliant {
my ($value);
$value = $_[0];
$value =~ s/\s+$//; #removes trailing whitespace
$value =~ s/^\s+//; #removes leading whitespace
$value =~ s/^\\//; #removes leading slash
$value =~ s/ /_/g; #replaces whitespace with underscores
$value =~ s/\.$//; #removes trailing period
$value =~ s/[^A-Za-z0-9\-\.\_\/]//g; #removes all funky characters
$value =~ s/^\///; #removes a preceeding /
$value =~ s/\/\//\//g; #removes double /
return $value;
my $url = shift;
return WebGUI::International::makeUrlCompliant($url);
}
#-------------------------------------------------------------------

View file

@ -1,9 +1,26 @@
package WebGUI::i18n::English;
$LANGUAGE = {
use strict;
our $LANGUAGE = {
label => 'English',
charset => 'UTF-8',
toolbar => 'metal'
};
sub makeUrlCompliant {
my $value = shift;
$value =~ s/\s+$//; #removes trailing whitespace
$value =~ s/^\s+//; #removes leading whitespace
$value =~ s/^\\//; #removes leading slash
$value =~ s/ /_/g; #replaces whitespace with underscores
$value =~ s/\.$//; #removes trailing period
$value =~ s/[^A-Za-z0-9\-\.\_\/]//g; #removes all funky characters
$value =~ s/^\///; #removes a preceeding /
$value =~ s/\/\//\//g; #removes double /
return $value;
}
1;