From 90419bb59a062c38f9b8e9d87308a989ec5f2e17 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Fri, 5 Nov 2004 00:00:33 +0000 Subject: [PATCH] added internationalized url handling --- lib/WebGUI/International.pm | 42 +++++++++++++++++++++++++++++++++++-- lib/WebGUI/URL.pm | 16 ++++---------- lib/WebGUI/i18n/English.pm | 19 ++++++++++++++++- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/lib/WebGUI/International.pm b/lib/WebGUI/International.pm index 5b6850f49..dfbabd613 100644 --- a/lib/WebGUI/International.pm +++ b/lib/WebGUI/International.pm @@ -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 ] ) diff --git a/lib/WebGUI/URL.pm b/lib/WebGUI/URL.pm index c62044bb7..47c1d7173 100644 --- a/lib/WebGUI/URL.pm +++ b/lib/WebGUI/URL.pm @@ -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); } #------------------------------------------------------------------- diff --git a/lib/WebGUI/i18n/English.pm b/lib/WebGUI/i18n/English.pm index 641a6bf29..04abb5757 100644 --- a/lib/WebGUI/i18n/English.pm +++ b/lib/WebGUI/i18n/English.pm @@ -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;