Stop unnecessary warnings in get_language
authorColin Campbell <colin.campbell@ptfs-europe.com>
Mon, 2 Apr 2012 14:06:28 +0000 (15:06 +0100)
committerPaul Poulain <paul.poulain@biblibre.com>
Wed, 4 Apr 2012 16:33:11 +0000 (18:33 +0200)
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>
C4/Templates.pm

index 0741040..d6e3ce4 100644 (file)
@@ -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;
-