| CGI::Lingua - Natural language choices for CGI programs |
CGI::Lingua - Natural language choices for CGI programs
Version 0.22
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);
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 one optional parameter, a CHI object which is used to cache Whois lookups.
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 Britsh 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.
Synonym for language, for compatibility with Local::Object::Language
Tells the CGI what variant to use e.g. 'United Kingdom'.
Gives the two character representation of the supported language, e.g. 'en'.
Gives a human readable rendition of what language the user asked for whether or not it is supported.
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.
HTTP doesn't have a way of transmitting a brower's localisation informtion 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 object.
Nigel Horne, <njh at bandsman.co.uk>
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. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
Locale::Object
You can find documentation for this module with the perldoc command.
perldoc CGI::Lingua
You can also look for information at:
Copyright 2010-2011 Nigel Horne.
This program is released under the following licence: GPL
| CGI::Lingua - Natural language choices for CGI programs |