From 76e39750b7d1f767b9fd93ce328ab25a829898e3 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 26 Feb 2014 13:20:37 +0100 Subject: [PATCH] Bug 11848: Move language detection function in C4::Languages Also store interface (intranet, opac) in context to not have to pass it as parameter. Signed-off-by: Bernardo Gonzalez Kriegel No koha-qa errors Signed-off-by: Katrin Fischer Comments on last patch. Signed-off-by: Galen Charlton --- C4/Auth.pm | 7 ++-- C4/Context.pm | 11 +++++++ C4/Languages.pm | 59 ++++++++++++++++++++++++++++++++-- C4/Templates.pm | 51 ++--------------------------- catalogue/search.pl | 5 ++- opac/opac-search.pl | 4 ++- t/{Templates.t => Languages.t} | 9 +++--- 7 files changed, 86 insertions(+), 60 deletions(-) rename t/{Templates.t => Languages.t} (79%) diff --git a/C4/Auth.pm b/C4/Auth.pm index 9deeb9bd19..f6d3b55372 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -27,6 +27,7 @@ use CGI::Session; require Exporter; use C4::Context; use C4::Templates; # to get the template +use C4::Languages; use C4::Branch; # GetBranches use C4::Search::History; use C4::VirtualShelves; @@ -134,6 +135,8 @@ sub get_template_and_user { my $in = shift; my ( $user, $cookie, $sessionID, $flags ); + C4::Context->interface($in->{type}); + my $template = C4::Templates::gettemplate( $in->{'template_name'}, $in->{'type'}, @@ -480,9 +483,9 @@ sub get_template_and_user { # Check if we were asked using parameters to force a specific language if ( defined $in->{'query'}->param('language') ) { - # Extract the language, let C4::Templates::getlanguage choose + # Extract the language, let C4::Languages::getlanguage choose # what to do - my $language = C4::Templates::getlanguage($in->{'query'},$in->{'type'}); + my $language = C4::Languages::getlanguage($in->{'query'}); my $languagecookie = C4::Templates::getlanguagecookie($in->{'query'},$language); if ( ref $cookie eq 'ARRAY' ) { push @{ $cookie }, $languagecookie; diff --git a/C4/Context.pm b/C4/Context.pm index 71a0f8c908..1bc70b2946 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -1261,6 +1261,17 @@ sub IsSuperLibrarian { return ($userenv->{flags}//0) % 2; } +sub interface { + my ($class, $interface) = @_; + + if (defined $interface) { + $interface ||= 'opac'; + $context->{interface} = $interface; + } + + return $context->{interface}; +} + 1; __END__ diff --git a/C4/Languages.pm b/C4/Languages.pm index 6e258cb2ad..930db2b507 100644 --- a/C4/Languages.pm +++ b/C4/Languages.pm @@ -19,9 +19,12 @@ package C4::Languages; # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -use strict; -#use warnings; FIXME - Bug 2505 +use strict; +use warnings; + use Carp; +use CGI; +use List::MoreUtils qw( any ); use C4::Context; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG); @@ -46,7 +49,7 @@ BEGIN { &getLanguages &getAllLanguages ); - @EXPORT_OK = qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages getLanguages get_bidi regex_lang_subtags language_get_description accept_language); + @EXPORT_OK = qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages getLanguages get_bidi regex_lang_subtags language_get_description accept_language getlanguage); $DEBUG = 0; } @@ -552,6 +555,56 @@ sub accept_language { return $secondaryMatch if $secondaryMatch; return undef; # else, we got nothing. } + +=head2 getlanguage + + Select a language based on the URL parameter 'language', a cookie, + syspref available languages & browser + +=cut + +sub getlanguage { + my ($cgi) = @_; + + $cgi //= new CGI; + my $interface = C4::Context->interface; + my $language; + + my $preference_to_check = + $interface eq 'intranet' ? 'language' : 'opaclanguages'; + # Get the available/valid languages list + my @languages = split /,/, C4::Context->preference($preference_to_check); + + # Chose language from the URL + $language = $cgi->param( 'language' ); + if ( defined $language && any { $_ eq $language } @languages) { + return $language; + } + + # cookie + if ($language = $cgi->cookie('KohaOpacLanguage') ) { + $language =~ s/[^a-zA-Z_-]*//; # sanitize cookie + } + + # HTTP_ACCEPT_LANGUAGE + if ( !$language && $ENV{HTTP_ACCEPT_LANGUAGE} ) { + $language = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE}, + getTranslatedLanguages( $interface, 'prog' ) ); + } + + # Ignore a lang not selected in sysprefs + if ( $language && any { $_ eq $language } @languages ) { + return $language; + } + + # Pick the first selected syspref language + $language = shift @languages; + return $language if $language; + + # Fall back to English if necessary + return 'en'; +} + 1; __END__ diff --git a/C4/Templates.pm b/C4/Templates.pm index 0b498626da..8d13a49ef0 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -285,7 +285,7 @@ sub themelanguage { ($query) or warn "no query in themelanguage"; # Select a language based on cookie, syspref available languages & browser - my $lang = getlanguage($query, $interface); + my $lang = C4::Languages::getlanguage($query); # Select theme my $is_intranet = $interface eq 'intranet'; @@ -307,6 +307,7 @@ sub themelanguage { sub setlanguagecookie { my ( $query, $language, $uri ) = @_; + my $cookie = $query->cookie( -name => 'KohaOpacLanguage', -value => $language, @@ -339,52 +340,4 @@ sub getlanguagecookie { return $cookie; } -=head2 getlanguage - - Select a language based on the URL parameter 'language', a cookie, - syspref available languages & browser - -=cut - -sub getlanguage { - my ($query, $interface) = @_; - - my $preference_to_check = - $interface eq 'intranet' ? 'language' : 'opaclanguages'; - # Get the available/valid languages list - my @languages = split /,/, C4::Context->preference($preference_to_check); - - my $lang; - - # Chose language from the URL - $lang = $query->param( 'language' ); - if ( defined $lang && any { $_ eq $lang } @languages) { - return $lang; - } - - # cookie - if ($query and $query->cookie('KohaOpacLanguage') ) { - $lang = $query->cookie('KohaOpacLanguage'); - $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie - } - - # HTTP_ACCEPT_LANGUAGE - if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE} ) { - $lang = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE}, - getTranslatedLanguages( $interface, 'prog' ) ); - } - - # Ignore a lang not selected in sysprefs - if ( $lang && any { $_ eq $lang } @languages ) { - return $lang; - } - - # Pick the first selected syspref language - $lang = shift @languages; - return $lang if $lang; - - # Fall back to English if necessary - return 'en'; -} - 1; diff --git a/catalogue/search.pl b/catalogue/search.pl index 29f5c834bb..cda5dad7ac 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -160,7 +160,7 @@ my $DisplayMultiPlaceHold = C4::Context->preference("DisplayMultiPlaceHold"); use CGI qw('-no_undef_params'); my $cgi = new CGI; -my $lang = C4::Templates::getlanguage($cgi, 'intranet'); +my ($template,$borrowernumber,$cookie); # decide which template to use my $template_name; my $template_type; @@ -181,6 +181,9 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({ flagsrequired => { catalogue => 1 }, } ); + +my $lang = C4::Languages::getlanguage($cgi); + if (C4::Context->preference("marcflavour") eq "UNIMARC" ) { $template->param('UNIMARC' => 1); } diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 00590c25b8..8e46005ead 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -91,7 +91,6 @@ BEGIN { } my ($template,$borrowernumber,$cookie); -my $lang = C4::Templates::getlanguage($cgi, 'opac'); # decide which template to use my $template_name; my $template_type = 'basic'; @@ -121,6 +120,9 @@ else { authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ), } ); + +my $lang = C4::Languages::getlanguage($cgi); + if ($template_name eq 'opac-results.tmpl') { $template->param('COinSinOPACResults' => C4::Context->preference('COinSinOPACResults')); } diff --git a/t/Templates.t b/t/Languages.t similarity index 79% rename from t/Templates.t rename to t/Languages.t index 9343b3806e..9cafbd77bd 100644 --- a/t/Templates.t +++ b/t/Languages.t @@ -1,6 +1,7 @@ #!/usr/bin/perl # Copyright 2013 Equinox Software, Inc. +# Copyright 2014 BibLibre # # This file is part of Koha. # @@ -22,7 +23,7 @@ use Test::MockModule; use CGI; BEGIN { - use_ok('C4::Templates'); + use_ok('C4::Languages'); } my @languages = (); # stores the list of active languages @@ -41,11 +42,11 @@ $module_context->mock( }, ); -delete $ENV{TTTP_ACCEPT_LANGUAGE}; +delete $ENV{HTTP_ACCEPT_LANGUAGE}; my $query = CGI->new(); @languages = ('de-DE', 'fr-FR'); -is(C4::Templates::getlanguage($query, 'opac'), 'de-DE', 'default to first language specified in syspref (bug 10560)'); +is(C4::Languages::getlanguage($query), 'de-DE', 'default to first language specified in syspref (bug 10560)'); @languages = (); -is(C4::Templates::getlanguage($query, 'opac'), 'en', 'default to English if no language specified in syspref (bug 10560)'); +is(C4::Languages::getlanguage($query), 'en', 'default to English if no language specified in syspref (bug 10560)'); -- 2.39.5