Skip to content

Commit

Permalink
Refactor get_language
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Nov 22, 2024
1 parent 438c096 commit 5d96e0c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
34 changes: 29 additions & 5 deletions dynamic-site/lib/Ged2site/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ BEGIN {
Log::Any::Adapter->set('Log4perl');
}

=head1 SUBROUTINES/METHODS
=head2 create_disc_cache
Initialise a disc-based cache using the CHI module.
Supports multiple cache drivers, including BerkeleyDB, DBI, and Redis.
=cut

sub create_disc_cache {
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

Expand Down Expand Up @@ -94,6 +103,13 @@ sub create_disc_cache {
return CHI->new(%chi_args);
}

=head2 create_memory_cache
Initialise a memory-based cache using the CHI module.
Supports multiple cache drivers, including SharedMem, Memory, and Redis.
=cut

sub create_memory_cache {
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

Expand Down Expand Up @@ -167,18 +183,26 @@ sub create_memory_cache {
return CHI->new(%chi_args);
}

# From http://www.geodatasource.com/developers/perl
# FIXME: use Math::Trig
=head2 distance
Calculate the distance between two geographical points using latitude and longitude.
Supports distance in kilometres (K), nautical miles (N), or miles.
From L<http://www.geodatasource.com/developers/perl>
FIXME: use Math::Trig
=cut

sub distance {
my ($lat1, $lon1, $lat2, $lon2, $unit) = @_;
my $theta = $lon1 - $lon2;
my $dist = sin(_deg2rad($lat1)) * sin(_deg2rad($lat2)) + cos(_deg2rad($lat1)) * cos(_deg2rad($lat2)) * cos(_deg2rad($theta));
$dist = _acos($dist);
$dist = _rad2deg($dist);
$dist = $dist * 60 * 1.1515;
if ($unit eq "K") {
$dist = $dist * 1.609344;
} elsif ($unit eq "N") {
if ($unit eq 'K') {
$dist = $dist * 1.609344; # number of kilometres in a mile
} elsif ($unit eq 'N') {
$dist = $dist * 0.8684;
}
return ($dist);
Expand Down
60 changes: 24 additions & 36 deletions ged2site
Original file line number Diff line number Diff line change
Expand Up @@ -14122,8 +14122,8 @@ sub HTML::GoogleMaps::V3::_text_to_point {
# IE, already a long/lat pair
return [ reverse @$point_text ] if ref( $point_text ) eq 'ARRAY';

if ( my @loc = $self->{geocoder}->geocode( location => $point_text ) ) {
if ( my $location = $loc[0] ) {
if( my @loc = $self->{geocoder}->geocode( location => $point_text ) ) {
if( my $location = $loc[0] ) {
##################################################
# ADDITION:
if(ref($location) eq 'Geo::Location::Point') {
Expand All @@ -14135,7 +14135,7 @@ sub HTML::GoogleMaps::V3::_text_to_point {
# END OF ADDITION
##################################################

if ( ref( $location ) ne 'HASH' ) {
if( ref( $location ) ne 'HASH' ) {
warn "$point_text didn't return a HASH ref as first element from ->geocode";
return 0;
}
Expand Down Expand Up @@ -14757,44 +14757,32 @@ sub get_year_from_date($)
# https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
sub get_language
{
my %langs = (
'br' => 'Breton',
'de' => 'German',
'en' => 'English',
'fr' => 'French',
'fa' => 'Farsi',
'la' => 'Latin',
);

# Check the LANGUAGE environment variable
if($ENV{'LANGUAGE'}) {
my %langs = (
'br' => 'Breton',
'de' => 'German',
'en' => 'English',
'fr' => 'French',
'fa' => 'Farsi',
'la' => 'Latin'
);
foreach my $language(split/:/, $ENV{'LANGUAGE'}) {
if(my $rc = $langs{$language}) {
return $rc;
}
for my $language (split /:/, $ENV{'LANGUAGE'}) {
return $langs{$language} if exists $langs{$language};
}
}
foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
my $val = $ENV{$variable};
next unless(defined($val));

if($val =~ /^en/) {
return 'English';
}
if($val =~ /^fr/) {
return 'French';
}
if($val =~ /^de/) {
return 'German';
}
if($val =~ /^fa/) {
return 'Farsi';
}
if($val =~ /^la/) {
return 'Latin';
}
if($val =~ /^br/) {
return 'Breton';
# Check other environment variables
for my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
if(my $val = $ENV{$variable}) {
if(my ($prefix) = $val =~ /^(\w{2})/) {
return $langs{$prefix} if exists $langs{$prefix};
}
}
}

# Fallback
complain("Can't determine the language, falling back to English");
return 'English';
}
Expand Down Expand Up @@ -15100,7 +15088,7 @@ sub twins
$d = $dfn->parse_datetime($d->{'canonical'});

# Check if the sibling is a twin
if ($d == $birth_dt || $d == ($birth_dt - $oneday) || $d == ($birth_dt + $oneday)) {
if($d == $birth_dt || $d == ($birth_dt - $oneday) || $d == ($birth_dt + $oneday)) {
push @twins, { sibling => $sibling, dob => $d };
}
}
Expand Down

0 comments on commit 5d96e0c

Please sign in to comment.