From ce046f9a732a74a7b6a5ddca228d2579c08d288d Mon Sep 17 00:00:00 2001 From: Colin Campbell Date: Mon, 2 Apr 2012 15:06:28 +0100 Subject: [PATCH] Stop unnecessary warnings in get_language MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 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 --- C4/Templates.pm | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/C4/Templates.pm b/C4/Templates.pm index 0741040492..d6e3ce46e4 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -4,7 +4,7 @@ use strict; 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 # @@ -313,9 +313,9 @@ sub getlanguage { 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); my $lang; @@ -326,20 +326,18 @@ sub getlanguage { } # HTTP_ACCEPT_LANGUAGE - 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 - $lang = undef unless first { $_ eq $lang } @languages; + if ( $lang && any { $_ eq $lang } @languages ) { + return $lang; + } # Fall back to English if necessary - $lang = 'en' unless $lang; - - return $lang; + return 'en'; } 1; - -- 2.39.5