X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FLanguages.pm;h=95bc1144e442b811a7dc45ae3832b83d8a4995d9;hb=765535c09ac4cf9c22438e54048f185adfb4a6ac;hp=3a5ee2ad8babdc324557a36bb20a74a0c7542f1f;hpb=5c52ab34d01471a6e8f88f22c8c53e2f91863bc2;p=koha.git diff --git a/C4/Languages.pm b/C4/Languages.pm index 3a5ee2ad8b..95bc1144e4 100644 --- a/C4/Languages.pm +++ b/C4/Languages.pm @@ -5,23 +5,26 @@ package C4::Languages; # Portions Copyright 2009 Chris Cormack and the Koha Dev Team # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -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); @@ -43,9 +46,10 @@ BEGIN { @EXPORT = qw( &getFrameworkLanguages &getTranslatedLanguages + &getLanguages &getAllLanguages ); - @EXPORT_OK = qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages 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; } @@ -175,17 +179,36 @@ Returns a reference to an array of hashes: print "$language->{language_locale_name}\n"; } +This routine is a wrapper for getLanguages(). + =cut sub getAllLanguages { + return getLanguages(shift); +} + +=head2 getLanguages + + my $lang_arrayref = getLanguages([$lang[, $isFiltered]]); + +Returns a reference to an array of hashes of languages. + +- If no parameter is passed to the function, it returns english languages names +- If a $lang parameter conforming to RFC4646 syntax is passed, the function returns languages names translated in $lang + If a language name is not translated in $lang in database, the function returns english language name +- If $isFiltered is set to true, only the detail of the languages selected in system preferences AdvanceSearchLanguages is returned. + +=cut + +sub getLanguages { my $lang = shift; -# if no parameter is passed to the function, it returns english languages names -# if a $lang parameter conforming to RFC4646 syntax is passed, the function returns languages names translated in $lang -# if a language name is not translated in $lang in database, the function returns english language name + my $isFiltered = shift; + my @languages_loop; my $dbh=C4::Context->dbh; my $default_language = 'en'; my $current_language = $default_language; + my $language_list = $isFiltered ? C4::Context->preference("AdvancedSearchLanguages") : undef; if ($lang) { $current_language = regex_lang_subtags($lang)->{'language'}; } @@ -226,7 +249,10 @@ sub getAllLanguages { $language_subtag_registry->{language_description} = $language_descriptions->{description}; } } - push @languages_loop, $language_subtag_registry; + # Do not push unless valid iso639-2 code + if ( $language_subtag_registry->{ iso639_2_code } and ( !$language_list || index ( $language_list, $language_subtag_registry->{ iso639_2_code } ) >= 0) ) { + push @languages_loop, $language_subtag_registry; + } } return \@languages_loop; } @@ -244,7 +270,7 @@ sub _get_themes { my $interface = shift; my $htdocs; my @themes; - if ( $interface eq 'intranet' ) { + if ( $interface && $interface eq 'intranet' ) { $htdocs = C4::Context->config('intrahtdocs'); } else { @@ -267,16 +293,19 @@ Internal function, returns an array of directory names, excluding non-language d sub _get_language_dirs { my ($htdocs,$theme) = @_; + $htdocs //= ''; + $theme //= ''; my @lang_strings; opendir D, "$htdocs/$theme"; for my $lang_string ( readdir D ) { next if $lang_string =~/^\./; next if $lang_string eq 'all'; next if $lang_string =~/png$/; + next if $lang_string =~/js$/; next if $lang_string =~/css$/; next if $lang_string =~/CVS$/; next if $lang_string =~/\.txt$/i; #Don't read the readme.txt ! - next if $lang_string =~/img|images|famfam|sound|pdf/; + next if $lang_string =~/img|images|famfam|js|less|lib|sound|pdf/; push @lang_strings, $lang_string; } return (@lang_strings); @@ -292,6 +321,7 @@ FIXME: this could be rewritten and simplified using map sub _build_languages_arrayref { my ($translated_languages,$current_language,$enabled_languages) = @_; + $current_language //= ''; my @translated_languages = @$translated_languages; my @languages_loop; # the final reference to an array of hashrefs my @enabled_languages = @$enabled_languages; @@ -329,7 +359,7 @@ sub _build_languages_arrayref { my $enabled; for my $enabled_language (@enabled_languages) { my $regex_enabled_language = regex_lang_subtags($enabled_language); - $enabled = 1 if $key eq $regex_enabled_language->{language}; + $enabled = 1 if $key eq ($regex_enabled_language->{language} // ''); } push @languages_loop, { # this is only use if there is one @@ -338,7 +368,7 @@ sub _build_languages_arrayref { language => $key, sublanguages_loop => $value, plural => $track_language_groups->{$key} >1 ? 1 : 0, - current => $current_language_regex->{language} eq $key ? 1 : 0, + current => ($current_language_regex->{language} // '') eq $key ? 1 : 0, group_enabled => $enabled, }; } @@ -529,6 +559,61 @@ 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 $theme = C4::Context->preference( ( $interface eq 'opac' ) ? 'opacthemes' : 'template' ); + my $language; + + my $preference_to_check = + $interface eq 'intranet' ? 'language' : 'opaclanguages'; + # Get the available/valid languages list + my @languages; + my $preference_value = C4::Context->preference($preference_to_check); + if ($preference_value) { + @languages = split /,/, $preference_value; + } + + # 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, $theme ) ); + } + + # 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__