# 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 <http://www.gnu.org/licenses>.
-use strict;
-#use warnings; FIXME - Bug 2505
-use Carp;
-use C4::Context;
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
-
-eval {
- if (C4::Context->ismemcached) {
- require Memoize::Memcached;
- import Memoize::Memcached qw(memoize_memcached);
+use strict;
+use warnings;
- memoize_memcached('getTranslatedLanguages', memcached => C4::Context->memcached);
- memoize_memcached('getFrameworkLanguages' , memcached => C4::Context->memcached);
- memoize_memcached('getAllLanguages', memcached => C4::Context->memcached);
- }
-};
+use Carp qw( carp );
+use CGI;
+use List::MoreUtils qw( any );
+use C4::Context;
+use Koha::Caches;
+use Koha::Cache::Memory::Lite;
+our (@ISA, @EXPORT_OK);
BEGIN {
- $VERSION = 3.00;
require Exporter;
@ISA = qw(Exporter);
- @EXPORT = qw(
- &getFrameworkLanguages
- &getTranslatedLanguages
- &getAllLanguages
+ @EXPORT_OK = qw(
+ getFrameworkLanguages
+ getTranslatedLanguages
+ getLanguages
+ getAllLanguages
);
- @EXPORT_OK = qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages get_bidi regex_lang_subtags language_get_description accept_language);
- $DEBUG = 0;
+ push @EXPORT_OK, qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages getLanguages get_bidi regex_lang_subtags language_get_description accept_language getlanguage get_rfc4646_from_iso639);
}
=head1 NAME
sub getTranslatedLanguages {
my ($interface, $theme, $current_language, $which) = @_;
- my $htdocs;
- my $all_languages = getAllLanguages();
my @languages;
- my @enabled_languages;
-
+ my @enabled_languages =
+ ( $interface && $interface eq 'intranet' )
+ ? split ",", C4::Context->preference('language')
+ : split ",", C4::Context->preference('OPACLanguages');
+
+ my $cache = Koha::Caches->get_instance;
+ my $cache_key = "languages_${interface}_${theme}";
if ($interface && $interface eq 'opac' ) {
- @enabled_languages = split ",", C4::Context->preference('opaclanguages');
- $htdocs = C4::Context->config('opachtdocs');
- if ( $theme and -d "$htdocs/$theme" ) {
- (@languages) = _get_language_dirs($htdocs,$theme);
- }
- else {
- for my $theme ( _get_themes('opac') ) {
- push @languages, _get_language_dirs($htdocs,$theme);
- }
+ my $htdocs = C4::Context->config('opachtdocs');
+ my $cached = $cache->get_from_cache($cache_key);
+ if ( $cached ) {
+ @languages = @{$cached};
+ } else {
+ @languages = _get_opac_language_dirs( $htdocs, $theme );
+ $cache->set_in_cache($cache_key, \@languages );
}
}
elsif ($interface && $interface eq 'intranet' ) {
- @enabled_languages = split ",", C4::Context->preference('language');
- $htdocs = C4::Context->config('intrahtdocs');
- if ( $theme and -d "$htdocs/$theme" ) {
- @languages = _get_language_dirs($htdocs,$theme);
- }
- else {
- foreach my $theme ( _get_themes('intranet') ) {
- push @languages, _get_language_dirs($htdocs,$theme);
- }
+ my $htdocs = C4::Context->config('intrahtdocs');
+ my $cached = $cache->get_from_cache($cache_key);
+ if ( $cached ) {
+ @languages = @{$cached};
+ } else {
+ @languages = _get_intranet_language_dirs( $htdocs, $theme );
+ $cache->set_in_cache($cache_key, \@languages );
}
}
else {
- @enabled_languages = split ",", C4::Context->preference('opaclanguages');
my $htdocs = C4::Context->config('intrahtdocs');
- foreach my $theme ( _get_themes('intranet') ) {
- push @languages, _get_language_dirs($htdocs,$theme);
- }
+ push @languages, _get_intranet_language_dirs( $htdocs );
+
$htdocs = C4::Context->config('opachtdocs');
- foreach my $theme ( _get_themes('opac') ) {
- push @languages, _get_language_dirs($htdocs,$theme);
- }
+ push @languages, _get_opac_language_dirs( $htdocs );
+
my %seen;
$seen{$_}++ for @languages;
@languages = keys %seen;
}
- return _build_languages_arrayref($all_languages,\@languages,$current_language,\@enabled_languages);
+ return _build_languages_arrayref(\@languages,$current_language,\@enabled_languages);
}
=head2 getAllLanguages
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;
+ my $isFiltered = shift;
+
my @languages_loop;
my $dbh=C4::Context->dbh;
- my $current_language = shift || 'en';
- my $sth = $dbh->prepare('SELECT * FROM language_subtag_registry WHERE type=\'language\'');
+ 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'};
+ }
+ my $sth = $dbh->prepare('SELECT * FROM language_subtag_registry WHERE type=\'language\' ORDER BY description');
$sth->execute();
while (my $language_subtag_registry = $sth->fetchrow_hashref) {
-
- # pull out all the script descriptions for each language
+ my $desc;
+ # check if language name is stored in current language
+ my $sth4= $dbh->prepare("SELECT description FROM language_descriptions WHERE type='language' AND subtag =? AND lang = ?");
+ $sth4->execute($language_subtag_registry->{subtag},$current_language);
+ while (my $language_desc = $sth4->fetchrow_hashref) {
+ $desc=$language_desc->{description};
+ }
my $sth2= $dbh->prepare("SELECT * FROM language_descriptions LEFT JOIN language_rfc4646_to_iso639 on language_rfc4646_to_iso639.rfc4646_subtag = language_descriptions.subtag WHERE type='language' AND subtag =? AND language_descriptions.lang = ?");
- $sth2->execute($language_subtag_registry->{subtag},$current_language);
-
+ if ($desc) {
+ $sth2->execute($language_subtag_registry->{subtag},$current_language);
+ }
+ else {
+ $sth2->execute($language_subtag_registry->{subtag},$default_language);
+ }
my $sth3 = $dbh->prepare("SELECT description FROM language_descriptions WHERE type='language' AND subtag=? AND lang=?");
-
# add the correct description info
while (my $language_descriptions = $sth2->fetchrow_hashref) {
$sth3->execute($language_subtag_registry->{subtag},$language_subtag_registry->{subtag});
$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;
}
+sub _get_opac_language_dirs {
+ my ( $htdocs, $theme ) = @_;
+
+ my @languages;
+ if ( $theme and -d "$htdocs/$theme" ) {
+ (@languages) = _get_language_dirs($htdocs,$theme);
+ }
+ else {
+ for my $theme ( _get_themes('opac') ) {
+ push @languages, _get_language_dirs($htdocs,$theme);
+ }
+ }
+ return @languages;
+}
+
+
+sub _get_intranet_language_dirs {
+ my ( $htdocs, $theme ) = @_;
+
+ my @languages;
+ if ( $theme and -d "$htdocs/$theme" ) {
+ @languages = _get_language_dirs($htdocs,$theme);
+ }
+ else {
+ foreach my $theme ( _get_themes('intranet') ) {
+ push @languages, _get_language_dirs($htdocs,$theme);
+ }
+ }
+ return @languages;
+}
+
=head2 _get_themes
Internal function, returns an array of all available themes.
my $interface = shift;
my $htdocs;
my @themes;
- if ( $interface eq 'intranet' ) {
+ if ( $interface && $interface eq 'intranet' ) {
$htdocs = C4::Context->config('intrahtdocs');
}
else {
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);
=cut
sub _build_languages_arrayref {
- my ($all_languages,$translated_languages,$current_language,$enabled_languages) = @_;
+ 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;
# how many languages are enabled, if one, take note, some contexts won't need to display it
- my %seen_languages; # the language tags we've seen
- my %found_languages;
my $language_groups;
my $track_language_groups;
my $current_language_regex = regex_lang_subtags($current_language);
push ( @{ $language_groups->{$language_subtags_hashref->{language}} }, $language_subtags_hashref );
}
# $key is a language subtag like 'en'
- while( my ($key, $value) = each %$language_groups) {
+ my %idx = map { $enabled_languages->[$_] => $_ } reverse 0 .. @$enabled_languages-1;
+ my @ordered_keys = sort {
+ my $aa = $language_groups->{$a}->[0]->{rfc4646_subtag};
+ my $bb = $language_groups->{$b}->[0]->{rfc4646_subtag};
+ ( exists $idx{$aa} and exists $idx{$bb} and ( $idx{$aa} cmp $idx{$bb} ) )
+ || ( exists $idx{$aa} and exists $idx{$bb} )
+ || exists $idx{$bb}
+ } keys %$language_groups;
+
+ for my $key ( @ordered_keys ) {
+ my $value = $language_groups->{$key};
# is this language group enabled? are any of the languages within it enabled?
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
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,
};
}
}
# No primary matches. Secondary? (ie, en-us requested and en supported)
return $secondaryMatch if $secondaryMatch;
- return undef; # else, we got nothing.
+ return; # 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) = @_;
+
+ my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
+ my $cache_key = "getlanguage";
+ unless ( $cgi and $cgi->param('language') ) {
+ my $cached = $memory_cache->get_from_cache($cache_key);
+ return $cached if $cached;
+ }
+
+ $cgi //= CGI->new;
+ 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
+ my $cgi_param_language = $cgi->param( 'language' );
+ if ( defined $cgi_param_language && any { $_ eq $cgi_param_language } @languages) {
+ $language = $cgi_param_language;
+ }
+
+ # cookie
+ if (not $language and my $cgi_cookie_language = $cgi->cookie('KohaOpacLanguage') ) {
+ ( $language = $cgi_cookie_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 && not any { $_ eq $language } @languages ) {
+ $language = undef;
+ }
+
+ # Pick the first selected syspref language
+ $language = shift @languages unless $language;
+
+ # Fall back to English if necessary
+ $language ||= 'en';
+
+ $memory_cache->set_in_cache( $cache_key, $language );
+ return $language;
+}
+
+=head2 get_rfc4646_from_iso639
+
+ Select a language rfc4646 code given an iso639 code
+
+=cut
+
+sub get_rfc4646_from_iso639 {
+
+ my $iso_code = shift;
+ my $rfc_subtag = Koha::Database->new()->schema->resultset('LanguageRfc4646ToIso639')->find({iso639_2_code=>$iso_code});
+ if ( $rfc_subtag ) {
+ return $rfc_subtag->rfc4646_subtag;
+ } else {
+ return;
+ }
+
}
+
1;
__END__