NAME
CGI::Lingua - Natural language choices for CGI programs
VERSION
Version 0.39
SYNOPSIS
No longer does your website need to be in English only. CGI::Lingua provides a simple basis to determine which language to display a website. The website tells CGI::Lingua which languages it supports. Based on that list CGI::Lingua tells the application which language the user would like to use.
use CGI::Lingua;
# ...
my $l = CGI::Lingua->new(supported => ['en', 'fr', 'en-gb', 'en-us']);
my $language = $l->language();
if ($language eq 'English') {
print '<P>Hello</P>';
} elsif($language eq 'French') {
print '<P>Bonjour</P>';
} else { # $language eq 'Unknown'
my $rl = $l->requested_language();
print "<P>Sorry for now this page is not available in $rl.</P>";
}
my $c = $l->country();
if ($c eq 'us') {
# print contact details in the US
} elsif ($c eq 'ca') {
# print contact details in Canada
} else {
# print worldwide contact details
}
# ...
use CHI;
use CGI::Lingua;
# ...
my $cache = CHI->new(driver => 'File', root_dir => '/tmp/cache', namespace => 'CGI::Lingua-countries');
my $l = CGI::Lingua->new(supported => ['en', 'fr'], cache => $cache);
SUBROUTINES/METHODS
new
Creates a CGI::Lingua object.
Takes one mandatory parameter: a list of languages, in RFC-1766 format, that the website supports. Language codes are of the form primary-code [ - country-code ] e.g. 'en', 'en-gb' for English and British English respectively.
For a list of primary-codes refer to ISO-639 (e.g. 'en' for English). For a list of country-codes refer to ISO-3166 (e.g. 'gb' for United Kingdom).
# We support English, French, British and American English, in that order
my $l = CGI::Lingua(supported => [('en', 'fr', 'en-gb', 'en-us')]);
Takes optional parameter, an object which is used to cache Whois lookups. This cache object will be an instantiation of a class that understands get and set, such as CHI.
Takes an optional boolean parameter syslog, to log messages to Sys::Syslog.
Takes an optional parameter dont_use_ip. By default, if none of the requested languages are supported, CGI::Lingua->language() looks in the IP address for the language to use. This may be not what you want, so use this option to disable the feature.
language
Tells the CGI application what language to display its messages in. The language is the natural name e.g. 'English' or 'Japanese'.
Sublanguages are handled sensibly, so that if a client requests U.S. English on a site that only serves British English, language() will return 'English'.
# Site supports English and British English
my $l = CGI::Lingua->new(supported => ['en', 'fr', 'en-gb']);
# If the browser requests 'en-us' , then language will be 'English' and
# sublanguage will be undefined because we weren't able to satisy the
# request
# Site supports British English only
my $l = CGI::Lingua->new(supported => ['fr', 'en-gb']);
# If the browser requests 'en-us' , then language will be 'English' and
# sublanguage will also be undefined, which may streem strange, but it
# ensures that sites behave sensibly.
name
Synonym for language, for compatibility with Local::Object::Language
sublanguage
Tells the CGI what variant to use e.g. 'United Kingdom'.
code_alpha2
Gives the two character representation of the supported language, e.g. 'en' when you've asked for en-gb.
sublanguage_code_alpha2
Gives the two character representation of the supported language, e.g. 'gb' when you've asked for en-gb.
requested_language
Gives a human readable rendition of what language the user asked for whether or not it is supported.
country
Returns the two character country code of the remote end. This only does a Whois lookup, but it is useful to have this method so that it can use the cache.
locale
HTTP doesn't have a way of transmitting a browser's localisation information which would be useful for default currency, date formatting etc.
This method attempts to detect the information, but it is a best guess and is not 100% reliable. But it's better than nothing ;-)
Returns a Locale::Object::Country object.
To be clear, if you're in the US and request the language in Spanish, and the site supports it, language() will return 'Spanish', and locale() will try to return the Locale::Object::Country for the US.
AUTHOR
Nigel Horne, <njh at bandsman.co.uk>
BUGS
Please report any bugs or feature requests to bug-cgi-lingua at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Lingua. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SEE ALSO
Locale::Country::Object HTTP::BrowserDetect
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc CGI::Lingua
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2010-2012 Nigel Horne.
This program is released under the following licence: GPL