CGI-Lingua
view release on metacpan or search on metacpan
10. Try geoplugin.net JSON API (LWP::Simple::WithCache or LWP::Simple)
11. Last resort: Net::Whois::IP then Net::Whois::IANA
12. Sanitise: discard numeric, normalise HK->CN, handle EU special case
13. Store in CHI cache; return result
## 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.
### EXAMPLE
local $ENV{REMOTE_ADDR} = '8.8.8.8';
my $locale = $l->locale();
if (defined $locale) {
print $locale->name(); # e.g. "United States"
## country
country : CGI::Lingua -> Str(2,lowercase) | undef
-- 'Unknown' returned only in the EU/Baidu special case
result = lc(code) where code satisfies ISO 3166-1 alpha-2
| undef when IP is private, loopback, or unresolvable
## locale
locale : CGI::Lingua -> Locale::Object::Country | undef
-- Best-guess detection; not guaranteed accurate.
result = first defined value from:
1. UA parenthetical language tag
2. HTTP::BrowserDetect country
3. country() IP lookup
4. GEOIP_COUNTRY_CODE env var
## time\_zone
time_zone : CGI::Lingua -> Str | undef
result is an IANA timezone name (e.g. 'Europe/London') or undef
lib/CGI/Lingua.pm view on Meta::CPAN
} else {
$self->{_geoip} = Geo::IP->new(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.
=head3 EXAMPLE
local $ENV{REMOTE_ADDR} = '8.8.8.8';
my $locale = $l->locale();
if (defined $locale) {
print $locale->name(); # e.g. "United States"
lib/CGI/Lingua.pm view on Meta::CPAN
=head2 country
country : CGI::Lingua -> Str(2,lowercase) | undef
-- 'Unknown' returned only in the EU/Baidu special case
result = lc(code) where code satisfies ISO 3166-1 alpha-2
| undef when IP is private, loopback, or unresolvable
=head2 locale
locale : CGI::Lingua -> Locale::Object::Country | undef
-- Best-guess detection; not guaranteed accurate.
result = first defined value from:
1. UA parenthetical language tag
2. HTTP::BrowserDetect country
3. country() IP lookup
4. GEOIP_COUNTRY_CODE env var
=head2 time_zone
time_zone : CGI::Lingua -> Str | undef
result is an IANA timezone name (e.g. 'Europe/London') or undef
$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.762 second using v1.01-cache-2.11-cpan-0fb53d1c279 )