Bug 9044: (follow-up) fix merge conflict typo that broke this script
[koha.git] / C4 / Languages.pm
index 415a798..2a74b92 100644 (file)
@@ -23,9 +23,19 @@ use strict;
 #use warnings; FIXME - Bug 2505
 use Carp;
 use C4::Context;
-use Koha::Cache;
 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);
+
+        memoize_memcached('getTranslatedLanguages', memcached => C4::Context->memcached);
+        memoize_memcached('getFrameworkLanguages' , memcached => C4::Context->memcached);
+        memoize_memcached('getAllLanguages',        memcached => C4::Context->memcached);
+    }
+};
+
 BEGIN {
     $VERSION = 3.07.00.049;
     require Exporter;
@@ -67,15 +77,6 @@ Returns a reference to an array of hashes:
 =cut
 
 sub getFrameworkLanguages {
-
-    my $cache;
-    if (Koha::Cache->is_cache_active()) {
-        $cache = Koha::Cache->new();
-        if (defined $cache) {
-            my $cached = $cache->get_from_cache("getFrameworkLanguages");
-            return $cached if $cached;
-        }
-    }
     # get a hash with all language codes, names, and locale names
     my $all_languages = getAllLanguages();
     my @languages;
@@ -98,9 +99,6 @@ sub getFrameworkLanguages {
             }
         }
     }
-    if (Koha::Cache->is_cache_active() && defined $cache) {
-        $cache->set_in_cache("getFrameworkLanguages",\@languages,1000)
-    }
     return \@languages;
 }
 
@@ -121,7 +119,6 @@ Returns a reference to an array of hashes:
 sub getTranslatedLanguages {
     my ($interface, $theme, $current_language, $which) = @_;
     my $htdocs;
-    my $all_languages = getAllLanguages();
     my @languages;
     my @enabled_languages;
  
@@ -163,7 +160,7 @@ sub getTranslatedLanguages {
         $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
@@ -181,30 +178,35 @@ Returns a reference to an array of hashes:
 =cut
 
 sub getAllLanguages {
-    # retrieve from cache if applicable
-    my $cache;
-    if (Koha::Cache->is_cache_active()) {
-        $cache = Koha::Cache->new();
-        if (defined $cache) {
-            my $cached = $cache->get_from_cache("getAllLanguages");
-            if ($cached) {
-                return $cached;
-            }
-        }
-    }
+    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 @languages_loop;
     my $dbh=C4::Context->dbh;
-    my $current_language = shift || 'en';
+    my $default_language = 'en';
+    my $current_language = $default_language;
+    if ($lang) {
+        $current_language = regex_lang_subtags($lang)->{'language'};
+    }
     my $sth = $dbh->prepare('SELECT * FROM language_subtag_registry WHERE type=\'language\'');
     $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});
@@ -226,9 +228,6 @@ sub getAllLanguages {
         }
         push @languages_loop, $language_subtag_registry;
     }
-    if (Koha::Cache->is_cache_active() && defined $cache) {
-        $cache->set_in_cache("getAllLanguages",\@languages_loop,1000);
-    }
     return \@languages_loop;
 }
 
@@ -274,10 +273,11 @@ sub _get_language_dirs {
         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,7 +292,7 @@ FIXME: this could be rewritten and simplified using map
 =cut
 
 sub _build_languages_arrayref {
-        my ($all_languages,$translated_languages,$current_language,$enabled_languages) = @_;
+        my ($translated_languages,$current_language,$enabled_languages) = @_;
         my @translated_languages = @$translated_languages;
         my @languages_loop; # the final reference to an array of hashrefs
         my @enabled_languages = @$enabled_languages;