[Koha-patches] [PATCH] [Bug 7874] Stop unnecessary warnings in get_language

Colin Campbell colin.campbell at ptfs-europe.com
Mon Apr 2 16:50:30 CEST 2012


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
---
 C4/Templates.pm |   24 +++++++++++-------------
 1 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/C4/Templates.pm b/C4/Templates.pm
index 0741040..d6e3ce4 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;
-
-- 
1.7.7.6



More information about the Koha-patches mailing list