Refactor code to be more idiomatic and clarify its intention was testing
undef against languages causing log warn was creating and assigning to
unnecessary variables calling accept_language with an undef is an
expensive way to get undef returned to the caller test we are asking it
a meabingful question use any rather than first ( we dont care about
firstness it should be unique anyway but it obscures the meaning of the
test ) split takes a pattern not a string
Signed-off-by: Frédéric Demians <f.demians@tamil.fr>
Having put my hands recently in this part of Koha code, I can confirm
that this patch fix log warnings, and add clarity and conciseness.
http://bugs.koha-community.org/show_bug.cgi?id=7874
Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
use warnings;
use Carp;
use CGI;
use warnings;
use Carp;
use CGI;
-use List::Util qw/first/;
+use List::MoreUtils qw/any/;
# Copyright 2009 Chris Cormack and The Koha Dev Team
#
# Copyright 2009 Chris Cormack and The Koha Dev Team
#
my ($query, $interface) = @_;
# Select a language based on cookie, syspref available languages & browser
my ($query, $interface) = @_;
# Select a language based on cookie, syspref available languages & browser
- my $is_intranet = $interface eq 'intranet';
- my @languages = split(",", C4::Context->preference(
- $is_intranet ? 'language' : 'opaclanguages'));
+ my $preference_to_check =
+ $interface eq 'intranet' ? 'language' : 'opaclanguages';
+ my @languages = split /,/, C4::Context->preference($preference_to_check);
- unless ($lang) {
- my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
- $lang = accept_language( $http_accept_language,
- getTranslatedLanguages($interface,'prog') );
+ if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE} ) {
+ $lang = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE},
+ getTranslatedLanguages( $interface, 'prog' ) );
}
# Ignore a lang not selected in sysprefs
}
# Ignore a lang not selected in sysprefs
- $lang = undef unless first { $_ eq $lang } @languages;
+ if ( $lang && any { $_ eq $lang } @languages ) {
+ return $lang;
+ }
# Fall back to English if necessary
# Fall back to English if necessary
- $lang = 'en' unless $lang;
-
- return $lang;