lib/Locale/Places.pm

filter: Statementstmt 50.0% 82 / 164 Branchbran 21.2% 20 / 94 Conditioncond 31.8% 14 / 44 MC/DCmcdc 7.6% 2 / 26 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 39.3% 136 / 346 CC 67 SCAR 60.9
CC
67
Cov
38%
translate
879.2
new
45.3
_get_language
25.4
CRAP 441.0
↑ Summary blib/lib/Locale/Places/GB.pm »
1 package Locale::Places;
2  
3 # TODO: Investigate https://github.com/x88/i18nGeoNamesDB
4 # TODO: US State names
5 # TODO: support src/dest as well as from/to to have an API more consistent with Lingua::Translate
6  
7 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Database::Abstraction;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use I18N::LangTags::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Module::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Object::Configure 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Params::Get 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Params::Validate::Strict 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 Locale::Places - Translate places between different languages using http://download.geonames.org/
26  
27 =head1 VERSION
28  
29 Version 0.17
30  
31 =cut
32  
33 7 our $VERSION = '0.17';
34  
35 =head1 SYNOPSIS
36  
37 Provides the functionality for translating place names between different languages using data from GeoNames.
38 It currently supports places in Great Britain (GB) and the United States (US) and relies on localized databases.
39 For example, London is Londres in French.
40  
41 =head1 METHODS
42  
43 =head2 new
44  
45 Create a Locale::Places object.
46  
47 Arguments:
48  
49 Takes different argument formats (hash or positional)
50  
51 =over 4
52  
53 =item * C<cache>
54  
55 Place to store results.
56 If none is given, the results will be stored in a temporary internal cache.
57  
58 =item * C<config_file>
59  
60 Points to a configuration file which contains the parameters to C<new()>.
61 The file can be in any common format,
62 including C<YAML>, C<XML>, and C<INI>.
63 This allows the parameters to be set at run time.
64  
65 =item * C<directory>
66  
67 Tells the object where to find a directory called 'data' containing GB.sql and US.sql
68 If that parameter isn't given,
69 the module will attempt to find the databases,
70 but that can't be guaranteed.
71  
72 =back
73  
74 Any other options are passed to the underlying database driver.
75  
76 =cut
77  
78 6 ▶sub new {
Subroutine new: called CC=14 SCAR=38.1
Pod documented
79 6 my $class = shift;
80  
81 # Handle hash or hashref arguments
82 6 my $params = Params::Get::get_params(undef, \@_);
83  
84 6 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 0 6 6
elsif (builtin::blessed($class)) { } 0 6 6
85 0 ▶ if((scalar keys %{$params}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$params;} > 0) 0 0 0
86 # Locale::Places::new() used rather than Locale::Places->new()
87 0 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
88 0 Carp::croak(__PACKAGE__ . '->new() must be called as a class or object method');
89 }
90  
91 # FIXME: this only works when no arguments are given
92 0 $class = __PACKAGE__;
93 } elsif(Scalar::Util::blessed($class)) {
94 # If $class is an object, clone it with new arguments
95 0 ▶ if($params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) 0 0 0
96 0 return bless { %{$class}, %{$params} }, ref($class);
97 }
98 0 return bless $class, ref($class);
99 }
100  
101 # Load the configuration from a config file, if provided
102  
103 6 $params = Object::Configure::configure($class, $params);
104  
105 5 my $directory = delete $params->{'directory'};
106 5 ▶ if(!defined($directory)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $directory) 5 0 5
107 # Module::Info path detection may fail under fatpacking or PAR.
108 5 $directory = Module::Info->new_from_loaded(__PACKAGE__)->file();
109 5 $directory =~ s/\.pm$//;
110 }
111 5 $directory = File::Spec->catfile($directory, 'data');
112  
113 5 ▶ unless((-d $directory) && (-r $directory)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $directory and -r $directory) 5 0 5
Condition33%1/3
-d $directory and -r $directory
!ll&&!rl&&r
500
MC/DC0%0/2
-d $directory and -r $directory
-d $directory -r $directory
Truth table33%1/3
A and B
A= -d $directoryB= -r $directory
ABresult
0X0
100
111
114 5 ▶ unless($ENV{'AUTOMATED_TESTING'}) { # Allow some sanity tests to be run
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"AUTOMATED_TESTING"}) 0 5 5
115 0 ▶ if($params->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$params{"logger"}) 0 0 0
116 0 $params->{'logger'}->warn("$class: can't find the data directory $directory: $!");
117 }
118 0 Carp::carp("$class: can't find the data directory $directory: $!");
119 0 return;
120 }
121 }
122  
123 5 ▶ $params->{'cache'} ||= CHI->new(driver => 'Memory', datastore => {}, expires_in => $params->{'cache_duration'} || $params->{'expires_in'} || '1 hour');
Condition50%4/8
$$params{"cache"} ||= "CHI"->new("driver", "Memory", "datastore", {}, "expires_in", $$params{"cache_duration"} || $$params{"expires_in"} || "1 hour")
l!l&&r!l&&!r
230
$$params{'cache_duration'} || $$params{'expires_in'} || '1 hour'
l!l
03
$$params{"cache_duration"} or $$params{"expires_in"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$$params{"cache"} ||= "CHI"->new("driver", "Memory", "datastore", {}, "expires_in", $$params{"cache_duration"} || $$params{"expires_in"} || "1 hour")
$$params{"cache"} "CHI"->new("driver", "Memory", "datastore", {}, "expires_in", $$params{"cache_duration"} || $$params{"expires_in"} || "1 hour")
MC/DC0%0/2
$$params{'cache_duration'} || $$params{'expires_in'} || '1 hour'
$$params{"cache_duration"} $$params{"expires_in"}
Truth table66%2/3
A ||= B
A= $$params{"cache"}B= "CHI"->new("driver", "Memory", "datastore", {}, "expires_in", $$params{"cache_duration"} || $$params{"expires_in"} || "1 hour")
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $$params{"cache_duration"}B= $$params{"expires_in"}
ABresult
1X1
011
000
124  
125 Database::Abstraction::init({
126 no_entry => 1,
127 cache => $params->{cache},
128 cache_duration => $params->{'cache_duration'} || '1 week',
129 5 ▶ %{$params},
Condition50%1/2
$$params{'cache_duration'} || '1 week'
l!l
05
MC/DC0%0/1
$$params{'cache_duration'} || '1 week'
$$params{'cache_duration'}
Truth table50%1/2
A
A= $$params{'cache_duration'}
Aresult
11
00
130 directory => $directory
131 });
132  
133 # Return the blessed object
134 return bless {
135 5 %{$params},
136 directory => $directory
137 }, $class;
138 }
139  
140 =head2 translate
141  
142 Translate a city into a different language.
143  
144 Parameters:
145 - place (mandatory): The name of the place to translate.
146 - from: The source language (optional; defaults to environment language).
147 - to: The target language (mandatory).
148 - country: The country where the place is located (optional; defaults to 'GB').
149  
150 Returns:
151 - Translated name if found, or undef if no translation exists.
152  
153 Example:
154 use Locale::Places;
155  
156 # Prints "Douvres"
157 print Locale::Places->new()->translate({ place => 'Dover', country => 'GB', from => 'en', to => 'fr' });
158  
159 =head3 Translation Resolution Order
160  
161 =over 4
162  
163 =item 1. Preferred names
164  
165 =item 2. Non-preferred names
166  
167 =item 3. Short names
168  
169 =item 4. Non-short names
170  
171 =item 5. Single-translation disambiguation
172  
173 =item 6. Identity fallback
174  
175 =back
176  
177 =head3 API SPECIFICATION
178  
179 =head4 INPUT
180  
181 {
182 'place' => { 'type' => 'string', 'min' => 2, 'max' => 64 },
183 'from' => { 'type' => 'string', 'min' => 2, 'max' => 64, optional => 1 },
184 'to' => { 'type' => 'string', 'min' => 2, 'max' => 64, optional => 1 },
185 'country' => { 'type' => 'string', 'min' => 2, 'max' => 64, optional => 1 }
186 }
187  
188 =head4 OUTPUT
189  
190 Argument error: croak
191 No matches found: undef
192  
193 {
194 'type' => 'string',
195 'min' => 1
196 }
197  
198 =cut
199  
200 sub translate
201 6 ▶{
Subroutine translate: called CC=40 SCAR=67.8
Pod documented
202 6 my $self = shift;
203  
204 # Ensure $self is valid
205 6 ▶ Carp::croak('translate() must be called on an object') unless(Scalar::Util::blessed($self));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 1 5 6
206  
207 # Handle hash or hashref arguments
208 5 my $params = Params::Validate::Strict::validate_strict({
209 args => Params::Get::get_params('place', @_),
210 schema => {
211 'place' => { 'type' => 'string', 'min' => 2, 'max' => 64 },
212 'from' => { 'type' => 'string', 'min' => 2, 'max' => 64, optional => 1 },
213 'to' => { 'type' => 'string', 'min' => 2, 'max' => 64, optional => 1 },
214 'country' => { 'type' => 'string', 'min' => 2, 'max' => 64, optional => 1 }
215 }
216 });
217  
218 4 ▶ if(scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 3 1 4
219 3 ▶ $params->{'from'} ||= 'en';
Condition100%2/2
$$params{"from"} ||= "en"
l!l
12
MC/DC100%1/1
$$params{"from"} ||= "en"
$$params{"from"}
Truth table100%2/2
A
A= $$params{"from"}
Aresult
11
00
220 }
221  
222 4 my $place = $params->{place};
223 4 ▶ unless(defined $place) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $place) 0 4 4
224 0 ▶ if(my $logger = $self->logger()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $self->logger) 0 0 0
225 0 $logger->warn(__PACKAGE__ . ': usage: translate(place => $place, from => $language1, to => $language2 [ , country => $country ])');
226 }
227 0 Carp::carp(__PACKAGE__, ': usage: translate(place => $place, from => $language1, to => $language2 [ , country => $country ])');
228 0 return;
229 }
230  
231 # Validate 'from' and 'to' languages
232 4 ▶ my $from = $params->{from} || $self->_get_language();
Condition66%2/3
$$params{'from'} || $self->_get_language
l!l&&r!l&&!r
301
MC/DC50%1/2
$$params{'from'} || $self->_get_language
$$params{'from'} $self->_get_language
Truth table66%2/3
A || B
A= $$params{'from'}B= $self->_get_language
ABresult
1X1
011
000
233 4 ▶ my $to = $params->{to} || $self->_get_language();
Condition33%1/3
$$params{'to'} || $self->_get_language
l!l&&r!l&&!r
004
MC/DC0%0/2
$$params{'to'} || $self->_get_language
$$params{'to'} $self->_get_language
Truth table33%1/3
A || B
A= $$params{'to'}B= $self->_get_language
ABresult
1X1
011
000
234 4 ▶ if(!defined($from)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from) 1 3 4
235 1 ▶ if(!defined($to)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to) 1 0 1
236 1 Carp::carp(__PACKAGE__, ': usage: translate(place => $place, from => $language1, to => $language2 [ , country => $country ])');
237 1 return;
238 }
239 0 Carp::carp(__PACKAGE__, ": can't work out which language to translate from");
240 0 return;
241 }
242 3 ▶ if(!defined($to)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to) 3 0 3
243 3 Carp::carp(__PACKAGE__, ": can't work out which language to translate to");
244 3 return;
245 }
246  
247 # Return early if 'from' and 'to' languages are the same
248 0 ▶ return $place if($to eq $from);
Branch: 1 branch
BranchTrueFalseTotal
if $to eq $from 0 0 0
249  
250 # Select database based on country, defaulting to GB
251 0 ▶ my $country = $params->{country} || 'GB';
Condition0%0/2
$$params{'country'} || 'GB'
l!l
00
MC/DC0%0/1
$$params{'country'} || 'GB'
$$params{'country'}
Truth table0%0/2
A
A= $$params{'country'}
Aresult
11
00
252  
253 0 my $cache_key = join('|', $place, $from, $to, $country);
254 0 ▶ if(my $cached_result = $self->{cache}->get($cache_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached_result = $$self{"cache"}->get($cache_key)) 0 0 0
255 0 return $cached_result;
256 }
257  
258 0 ▶ my $db = $self->{$country} ||= do {
Condition0%0/3
$$self{$country} ||= do { my $class = "Locale::Places::$country"; eval "require $class; 1" and $class->import unless $class->can("new"); Carp::croak("Unsupported country: $country") if $@; $class->new("directory", $$self{"directory"}) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{$country} ||= do { my $class = "Locale::Places::$country"; eval "require $class; 1" and $class->import unless $class->can("new"); Carp::croak("Unsupported country: $country") if $@; $class->new("directory", $$self{"directory"}) }
$$self{$country} do { my $class = "Locale::Places::$country"; eval "require $class; 1" and $class->import unless $class->can("new"); Carp::croak("Unsupported country: $country") if $@; $class->new("directory", $$self{"directory"}) }
Truth table0%0/3
A ||= B
A= $$self{$country}B= do { my $class = "Locale::Places::$country"; eval "require $class; 1" and $class->import unless $class->can("new"); Carp::croak("Unsupported country: $country") if $@; $class->new("directory", $$self{"directory"}) }
ABresult
1X1
011
000
259 0 my $class = "Locale::Places::$country";
260  
261 0 ▶ eval "require $class; 1" && $class->import() unless $class->can('new');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 0 0 0
Condition0%0/6
$class->can('new') || eval "require $class; 1" && $class->import
l!l&&r!l&&!r
000
eval "require $class; 1" and $class->import
!ll&&!rl&&r
000
MC/DC0%0/3
$class->can('new') || eval "require $class; 1" && $class->import
$class->can('new') eval "require $class; 1" $class->import
Truth table0%0/4
A || B and C
A= $class->can('new')B= eval "require $class; 1"C= $class->import
ABCresult
1XX1
0111
00X0
0100
262 0 ▶ Carp::croak("Unsupported country: $country") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
263 0 $class->new(directory => $self->{directory});
264 };
265  
266 # my @places = @{$db->selectall_hashref({ type => $from, data => $place, ispreferredname => 1 })};
267 # ::diag("$place: $from => $to");
268 0 my @places = $db->code2({ type => $from, data => $place, ispreferredname => 1 });
269 # ::diag(__LINE__, ': Number of matches = ', scalar(@places));
270 0 ▶ if(scalar(@places) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @places == 0) 0 0 0
271 # @places = @{$db->selectall_hashref({ type => $from, data => $place })};
272 0 @places = $db->code2({ type => $from, data => $place });
273 # ::diag(__LINE__, ': Number of matches = ', scalar(@places));
274 }
275  
276 0 ▶ if(scalar(@places) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @places == 1) { } 0 0 0
elsif (scalar @places > 1) { } 0 0 0
277 0 ▶ if(my $data = $db->data({ type => $to, code2 => $places[0] })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $db->data({"type", $to, "code2", $places[0]})) 0 0 0
278 # if(my $data = $db->data({ type => $to, code2 => $places[0]->{'code2'} })) {
279 # ::diag(__LINE__, ": $places[0]: $data");
280 0 $self->{cache}->set($cache_key, $data);
281 0 return $data;
282 }
283 } elsif(scalar(@places) > 1) {
284 # Handle the case when there is more than one preferred value
285 # but either not all translate or they all translate to the same
286 # value, in which case the duplicate can be ignored
287  
288 # If none of them match, then assume there are no translations
289 # available and return that
290  
291 0 my $candidate;
292 my $found_something;
293 0 foreach my $place(@places) {
294 0 ▶ if(my $data = $db->data({ type => $to, code2 => $place })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $db->data({"type", $to, "code2", $place})) 0 0 0
295 0 $found_something = 1;
296 0 ▶ if(defined($candidate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $candidate) { } 0 0 0
297 0 ▶ if($data ne $candidate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne $candidate) 0 0 0
298 0 $candidate = undef;
299 }
300 } else {
301 0 $candidate = $data;
302 }
303 }
304 }
305 0 ▶ return $candidate if(defined($candidate));
Branch: 1 branch
BranchTrueFalseTotal
if defined $candidate 0 0 0
306 0 ▶ return $place if(!defined($found_something));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found_something 0 0 0
307  
308 0 @places = $db->code2({ type => $from, data => $place, ispreferredname => 1, isshortname => undef });
309 0 ▶ if(scalar(@places) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @places == 1) { } 0 0 0
elsif (scalar @places == 0) { } 0 0 0
310 0 ▶ if(my $data = $db->data({ type => $to, code2 => $places[0] })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $db->data({"type", $to, "code2", $places[0]})) 0 0 0
311 0 return $self->{cache}->set($cache_key, $data);
312 }
313 0 @places = $db->code2({ type => $from, data => $place, ispreferredname => 1, isshortname => 1 });
314 0 ▶ if(scalar(@places) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @places == 1) 0 0 0
315 0 ▶ if(my $data = $db->data({ type => $to, code2 => $places[0] })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $db->data({"type", $to, "code2", $places[0]})) 0 0 0
316 0 return $self->{cache}->set($cache_key, $data);
317 }
318 # Can't find anything
319 0 return $self->{cache}->set($cache_key, $place);
320 }
321 } elsif(scalar(@places) == 0) {
322 0 @places = $db->code2({ type => $from, data => $place, isshortname => undef });
323 0 ▶ if((scalar(@places) == 1) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @places == 1 and my $data = $db->data({"type", $to, "code2", $places[0]})) 0 0 0
Condition0%0/3
scalar @places == 1 and my $data = $db->data({"type", $to, "code2", $places[0]})
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @places == 1 and my $data = $db->data({"type", $to, "code2", $places[0]})
scalar @places == 1 my $data = $db->data({"type", $to, "code2", $places[0]})
Truth table0%0/3
A and B
A= scalar @places == 1B= my $data = $db->data({"type", $to, "code2", $places[0]})
ABresult
0X0
100
111
324 (my $data = $db->data({ type => $to, code2 => $places[0] }))) {
325 0 return $self->{cache}->set($cache_key, $data);
326 }
327 0 @places = $db->code2({ type => $from, data => $place });
328 0 ▶ if((scalar(@places) == 1) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @places == 1 and my $data = $db->data({"type", $to, "code2", $places[0]})) 0 0 0
Condition0%0/3
scalar @places == 1 and my $data = $db->data({"type", $to, "code2", $places[0]})
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @places == 1 and my $data = $db->data({"type", $to, "code2", $places[0]})
scalar @places == 1 my $data = $db->data({"type", $to, "code2", $places[0]})
Truth table0%0/3
A and B
A= scalar @places == 1B= my $data = $db->data({"type", $to, "code2", $places[0]})
ABresult
0X0
100
111
329 (my $data = $db->data({ type => $to, code2 => $places[0] }))) {
330 0 return $self->{cache}->set($cache_key, $data);
331 }
332 } else {
333 # Handle multiple translations - see if they happen to be the same
334 0 my %translations;
335 0 foreach my $entry (@places) {
336 0 my $data = $db->data({ type => $to, code2 => $entry });
337 0 ▶ $translations{$data}++ if(defined $data);
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
338 }
339 0 ▶ if(keys(%translations) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %translations == 1) 0 0 0
340 0 return (keys %translations)[0];
341 }
342 }
343 # foreach (@places) {
344 # if(my $data = $db->data({ type => $to, code2 => $_ })) {
345 # ::diag(">>>>>$data");
346 # }
347 # }
348 0 Carp::croak(__PACKAGE__, ': database has ', scalar(@places), " entries for $place in language $to: ", join(', ', @places));
349 # foreach my $p(@places) {
350 # if(my $line = $db->fetchrow_hashref({ type => $to, code2 => $p->{'code2'} })) {
351 # return $line->{'data'};
352 # }
353 # }
354 }
355 0 return; # Return undef if no translation is found
356 }
357  
358 # Determines the system's default language using environment variables:
359 # 'LANGUAGE', 'LC_ALL', 'LC_MESSAGES', $ENV{'LANG'}.
360 # Defaults to English ('en') if no valid language is found.
361  
362 # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
363 # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
364 sub _get_language
365 5 ▶{
Subroutine _get_language: called CC=10 SCAR=32.3
366 5 for my $tag (I18N::LangTags::Detect::detect()) {
367 0 ▶ if ($tag =~ /^([a-z]{2})/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^([a-z]{2})/i) 0 0 0
368 0 return lc($1);
369 }
370 }
371 5 ▶ if(($ENV{'LANGUAGE'}) && ($ENV{'LANGUAGE'} =~ /^([a-z]{2})/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i) 0 5 5
Condition66%2/3
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
!ll&&!rl&&r
320
MC/DC0%0/2
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
$ENV{"LANGUAGE"} $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
Truth table66%2/3
A and B
A= $ENV{"LANGUAGE"}B= $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
ABresult
0X0
100
111
372 0 return lc($1);
373 }
374  
375 5 foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
376 15 my $val = $ENV{$variable};
377 15 ▶ next unless(defined($val));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 15 0 15
378  
379 0 ▶ if($val =~ /^([a-z]{2})/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^([a-z]{2})/i) 0 0 0
380 0 return lc($1);
381 }
382 }
383  
384 # if(defined($ENV{'LANG'}) && (($ENV{'LANG'} =~ /^C\./) || ($ENV{'LANG'} eq 'C'))) {
385 # return 'en';
386 # }
387 5 ▶ return 'en' if (defined $ENV{'LANG'}) && $ENV{'LANG'} =~ /^C(\.|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"LANG"} and $ENV{"LANG"} =~ /^C(\.|$)/ 0 5 5
Condition33%1/3
defined $ENV{"LANG"} and $ENV{"LANG"} =~ /^C(\.|$)/
!ll&&!rl&&r
500
MC/DC0%0/2
defined $ENV{"LANG"} and $ENV{"LANG"} =~ /^C(\.|$)/
defined $ENV{"LANG"} $ENV{"LANG"} =~ /^C(\.|$)/
Truth table33%1/3
A and B
A= defined $ENV{"LANG"}B= $ENV{"LANG"} =~ /^C(\.|$)/
ABresult
0X0
100
111
388 5 return; # undef
389 }
390  
391 =head2 AUTOLOAD
392  
393 Translate to the given language, where the routine's name will be the target language.
394  
395 # Prints 'Virginie', since that's Virginia in French
396 print $places->fr({ place => 'Virginia', from => 'en', country => 'US' });
397  
398 Extracts the target language from the method name and calls C<translate()> internally.
399  
400 Returns a string containing the translated name if found, or undef if no translation exists.
401  
402 =cut
403  
404 sub AUTOLOAD
405 5 ▶{
Subroutine AUTOLOAD: called CC=6 SCAR=27.3
406 5 our $AUTOLOAD;
407 5 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 5 0 5
408 5 ▶ return if(!defined($AUTOLOAD));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Locale::Places::AUTOLOAD 0 5 5
409  
410 # Extract the target language from the AUTOLOAD variable
411 5 my ($to) = $AUTOLOAD =~ /::(\w+)$/;
412  
413 5 ▶ return if($to eq 'DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "DESTROY" 5 0 5
414  
415 0 my $params = Params::Get::get_params('place', \@_);
416  
417 # Validate method name - only allow safe to languages
418 0 ▶ if($to =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) 0 0 0
419 0 return $self->translate(to => $to, %{$params});
420 }
421 0 ▶ if($self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"logger"}) 0 0 0
422 0 $self->{'logger'}->notice(__PACKAGE__ . ": Invalid language name: $to");
423 }
424 0 Carp::croak(__PACKAGE__, ": Invalid language name: $to");
425 }
426  
427 =head1 AUTHOR
428  
429 Nigel Horne, C<< <njh at nigelhorne.com> >>
430  
431 =head1 BUGS
432  
433 Only supports places in GB and the US at the moment.
434  
435 Canterbury no longer translates to Cantorbéry in French.
436 This is a problem with the data, which has this line:
437  
438 16324587 2653877 fr Canterbury 1
439  
440 which overrides the translation by setting the 'isPreferredName' flag
441  
442 Can't specify below the country level.
443 For example, is Virginia a state, a town in Illinois or one in Minnesota?
444  
445 =head1 SEE ALSO
446  
447 =over 4
448  
449 =item * L<Test Coverage Report|https://nigelhorne.github.io/Locale-Places/coverage/>
450  
451 =item * L<Locale::Country::Multilingual> to translate country names.
452  
453 =back
454  
455 =head1 SUPPORT
456  
457 This module is provided as-is without any warranty.
458  
459 You can find documentation for this module with the perldoc command.
460  
461 perldoc Locale::Places
462  
463 You can also look for information at:
464  
465 =over 4
466  
467 =item * MetaCPAN
468  
469 L<https://metacpan.org/release/Locale-Places>
470  
471 =item * GitHub
472  
473 L<https://github.com/nigelhorne/Locale-Places>
474  
475 =item * CPANTS
476  
477 L<http://cpants.cpanauthors.org/dist/Locale-Places>
478  
479 =item * CPAN Testers' Matrix
480  
481 L<http://matrix.cpantesters.org/?dist=Locale-Places>
482  
483 =item * CPAN Testers Dependencies
484  
485 L<http://deps.cpantesters.org/?module=Locale::Places>
486  
487 =item * Geonames Discussion Group
488  
489 L<https://groups.google.com/g/geonames>
490  
491 =back
492  
493 =head1 LICENCE AND COPYRIGHT
494  
495 Copyright 2020-2026 Nigel Horne.
496  
497 This program is released under the following licence: GPL2
498  
499 This product uses data from Geonames, available at L<http://download.geonames.org>.
500  
501 =cut
502  
503 7 1;
↑ Summary blib/lib/Locale/Places/GB.pm »