Nigel Horne > CGI-Lingua-0.50 > CGI::Lingua



Annotate this POD

View/Report Bugs
Module Version: 0.50   Source   Latest Release: CGI-Lingua-0.52


CGI::Lingua - Create a multilingual web page


Version 0.50


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 optional parameter cache, an object which is used to cache country lookups. This cache object is an object that understands get() and set() messages, such as an CHI object.

Takes an optional boolean parameter syslog, to log messages to Sys::Syslog.

Takes optional parameter logger, an object which is used for warnings and traces. This logger object is an object that understands warn() and trace() messages, such as a Log::Log4perl object.

Since emitting warnings from a CGI class can result in messages being lost (you may forget to look in your server's log), or appearing to the client in amongst HTML causing invalid HTML, it is recommended either either syslog or logger (or both) are set. If neither is given, Carp will be used.

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.


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'.

If none of the requested languages is included within the supported lists, language() returns 'Unknown'.

    use CGI::Lingua;
    # 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 satisfy 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 seem 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', or 'Unknown' if it can't be determined.


Gives the two character representation of the supported language, e.g. 'en' when you've asked for en-gb.

If none of the requested languages is included within the supported lists, language_code_alpha2() returns undef.


Synonym for language_code_alpha2, kept for historical reasons.


Gives the two character representation of the supported language, e.g. 'gb' when you've asked for en-gb, or undef.


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.

If Geo::IP is installed, CGI::Lingua will make use of that, otherwise it will do a Whois lookup. If you do not have Geo::IP installed, I recommend you make use of the caching capability of CGI::Lingua.


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.


Nigel Horne, <njh at>


If HTTP_ACCEPT_LANGUAGE is 3 characters, e.g., es-419, sublanguage() returns undef.

Please report any bugs or feature requests to bug-cgi-lingua at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


Locale::Country::Object HTTP::BrowserDetect


You can find documentation for this module with the perldoc command.

    perldoc CGI::Lingua

You can also look for information at:



Copyright 2010-2013 Nigel Horne.

This program is released under the following licence: GPL

syntax highlighting: