CGI-Lingua
view release on metacpan or search on metacpan
If [IP::Country](https://metacpan.org/pod/IP%3A%3ACountry), [Geo::IPfree](https://metacpan.org/pod/Geo%3A%3AIPfree) or [Geo::IP](https://metacpan.org/pod/Geo%3A%3AIP) is installed,
CGI::Lingua will make use of that, otherwise, it will do a Whois lookup.
If you do not have any of those installed I recommend you use the
caching capability of CGI::Lingua.
## 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](https://metacpan.org/pod/Locale%3A%3AObject%3A%3ACountry) 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.
## time\_zone
lib/CGI/Lingua.pm view on Meta::CPAN
$self->{_rlanguage} = 'Unknown';
}
}
$self->{_slanguage} = 'Unknown';
}
if($self->{_dont_use_ip}) {
return;
}
# The client hasn't said which to use, so guess from their IP address,
# or the requested language(s) isn't/aren't supported so use the IP
# address for an alternative
my $country = $self->country();
if((!defined($country)) && (my $c = $self->_what_language())) {
if($c =~ /^(..)_(..)/) {
$country = $2; # Best guess
} elsif($c =~ /^(..)$/) {
$country = $1; # Wrong, but maybe something will drop out
}
}
if(defined($country)) {
$self->_debug("country: $country");
# Determine the first official language of the country
my $from_cache;
lib/CGI/Lingua.pm view on Meta::CPAN
} else {
$self->{_have_geoip} = 0;
}
}
=head2 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 L<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.
=cut
$ENV{'HTTP_ACCEPT_LANGUAGE'} = 'en-029';
$ENV{'REMOTE_ADDR'} = '201.229.32.134';
my $l = new_ok('CGI::Lingua' => [
supported => ['en', 'nl', 'fr', 'fr-fr', 'de', 'id', 'il', 'ja', 'ko', 'pt', 'ru', 'es', 'tr', 'es-419'],
]);
ok(defined $l);
ok($l->isa('CGI::Lingua'));
SKIP: {
skip 'Tests require Internet access', 4 unless(-e 't/online.enabled');
ok(defined($l->country()));
# Sometimes in aw sometimes in uy. I guess the databases out
# there aren't consistent
ok(($l->country() eq 'aw') || ($l->country() eq 'uy'));
ok($l->language_code_alpha2() eq 'en');
ok($l->language() eq 'English');
}
ok(defined($l->requested_language()));
TODO: {
local $TODO = "sublanguage doesn't handle 3 characters";
ok(defined($l->sublanguage()));
$ENV{'HTTP_ACCEPT_LANGUAGE'} = 'es-419,es;q=0.8';
$ENV{'REMOTE_ADDR'} = '201.213.196.117';
my $l = new_ok('CGI::Lingua' => [
supported => ['en', 'nl', 'fr', 'fr-fr', 'de', 'id', 'il', 'ja', 'ko', 'pt', 'ru', 'es', 'tr', 'es-419'],
]);
ok(defined $l);
ok($l->isa('CGI::Lingua'));
SKIP: {
skip 'Tests require Internet access', 5 unless(-e 't/online.enabled');
ok(defined($l->country()));
# Sometimes in ar sometimes in uy. I guess the databases out
# there aren't consistent
ok(($l->country() eq 'ar') || ($l->country() eq 'uy'));
ok($l->language_code_alpha2() eq 'es');
ok($l->language() eq 'Spanish');
ok(defined($l->requested_language()));
}
TODO: {
local $TODO = "sublanguage doesn't handle 3 characters";
SKIP: {
( run in 1.069 second using v1.01-cache-2.11-cpan-748bfb374f4 )