Bug 7597 - Part 2 - move sub writeoff to C4::Accounts
[koha_gimpoz] / misc / translator / LangInstaller.pm
index a1fef0f..c8a204a 100644 (file)
@@ -21,6 +21,8 @@ use strict;
 use warnings;
 
 use C4::Context;
+# WARNING: Any other tested YAML library fails to work properly in this
+# script content
 use YAML::Syck qw( Dump LoadFile );
 use Locale::PO;
 use FindBin qw( $Bin );
@@ -68,11 +70,11 @@ sub new {
     $self->{interface} = {
         opac => {
             dir    => $context->config('opachtdocs') . '/prog',
-            suffix => '-i-opac-t-prog-v-3002000.po',
+            suffix => '-i-opac-t-prog-v-3006000.po',
         },
         intranet => {
             dir    => $context->config('intrahtdocs') . '/prog',
-            suffix => '-i-staff-t-prog-v-3002000.po',
+            suffix => '-i-staff-t-prog-v-3006000.po',
         }
     };
 
@@ -84,7 +86,7 @@ sub po_filename {
     my $self = shift;
 
     my $context    = C4::Context->new;
-    my $trans_path = $context->config('intranetdir') . '/misc/translator/po';
+    my $trans_path = $Bin . '/po';
     my $trans_file = "$trans_path/" . $self->{lang} . "-pref.po";
     return $trans_file;
 }
@@ -129,7 +131,7 @@ sub add_prefs {
                     }
                 }
             }
-            elsif ( $element ) {
+            elsif ( $element && $pref_name ) {
                 $self->po_append( $self->{file} . "#$pref_name# $element", $comment );
             }
         }
@@ -171,8 +173,9 @@ sub update_tab_prefs {
                     }
                 }
             }
-            elsif ( $element ) {
-                my $text = $self->get_trans_text( $self->{file} . "#$pref_name# $element" );
+            elsif ( $element && $pref_name ) {
+                my $id = $self->{file} . "#$pref_name# $element";
+                my $text = $self->get_trans_text( $id );
                 $p->[$i] = $text if $text;
             }
         }
@@ -186,7 +189,8 @@ sub get_po_from_prefs {
     for my $file ( @{$self->{pref_files}} ) {
         my $pref = LoadFile( $self->{path_pref_en} . "/$file" );
         $self->{file} = $file;
-        #print Dump($pref), "\n";
+        # Entries for tab titles
+        $self->po_append( $self->{file}, $_ ) for keys %$pref;
         while ( my ($tab, $tab_content) = each %$pref ) {
             if ( ref($tab_content) eq 'ARRAY' ) {
                 $self->add_prefs( $tab, $tab_content );
@@ -210,10 +214,9 @@ sub save_po {
 }
 
 
-sub update_prefs {
+sub get_po_merged_with_en {
     my $self = shift;
 
-    print "Update '", $self->{lang}, "' preferences .po file from 'en' .pref files\n";
     # Get po from current 'en' .pref files
     $self->get_po_from_prefs();
     my $po_current = $self->{po};
@@ -222,12 +225,19 @@ sub update_prefs {
     my $po_previous = Locale::PO->load_file_ashash( $self->po_filename );
 
     for my $id ( keys %$po_current ) {
-        my $po =  $po_previous->{'"'.$id.'"'};
+        my $po =  $po_previous->{Locale::PO->quote($id)};
         next unless $po;
         my $text = Locale::PO->dequote( $po->msgstr );
         $po_current->{$id}->msgstr( $text );
     }
+}
+
 
+sub update_prefs {
+    my $self = shift;
+    print "Update '", $self->{lang},
+          "' preferences .po file from 'en' .pref files\n";
+    $self->get_po_merged_with_en();
     $self->save_po();
 }
 
@@ -240,13 +250,19 @@ sub install_prefs {
         exit;
     }
 
-    # Update the language .po file with last modified 'en' preferences
-    # and load it.
-    $self->update_prefs();
+    # Get the language .po file merged with last modified 'en' preferences
+    $self->get_po_merged_with_en();
 
     for my $file ( @{$self->{pref_files}} ) {
         my $pref = LoadFile( $self->{path_pref_en} . "/$file" );
         $self->{file} = $file;
+        # First, keys are replaced (tab titles)
+        $pref = do {
+            my %pref = map { 
+                $self->get_trans_text( $self->{file} ) || $_ => $pref->{$_}
+            } keys %$pref;
+            \%pref;
+        };
         while ( my ($tab, $tab_content) = each %$pref ) {
             if ( ref($tab_content) eq 'ARRAY' ) {
                 $self->update_tab_prefs( $pref, $tab_content );
@@ -257,7 +273,8 @@ sub install_prefs {
             }
             my $ntab = {};
             for my $section ( keys %$tab_content ) {
-                my $text = $self->get_trans_text($self->{file} . " $section");
+                my $id = $self->{file} . " $section";
+                my $text = $self->get_trans_text($id);
                 my $nsection = $text ? $text : $section;
                 $ntab->{$nsection} = $tab_content->{$section};
             }
@@ -347,11 +364,23 @@ sub install {
 }
 
 
+sub get_all_langs {
+    my $self = shift;
+    opendir( my $dh, $self->{path_po} );
+    my @files = grep { $_ =~ /-i-opac-t-prog-v-3006000.po$/ }
+        readdir $dh;
+    @files = map { $_ =~ s/-i-opac-t-prog-v-3006000.po$//; $_ } @files;
+}
+
+
 sub update {
     my $self = shift;
-    return unless $self->{lang};
-    $self->update_tmpl() unless $self->{pref_only};
-    $self->update_prefs();
+    my @langs = $self->{lang} ? ($self->{lang}) : $self->get_all_langs();
+    for my $lang ( @langs ) {
+        $self->set_lang( $lang );
+        $self->update_tmpl() unless $self->{pref_only};
+        $self->update_prefs();
+    }
 }