Diff for /loncom/localize/localize/synch.pl between versions 1.7 and 1.10

version 1.7, 2003/11/28 02:12:05 version 1.10, 2009/01/13 16:46:35
Line 1 Line 1
 #$numbered=1;  #!/usr/bin/perl
   
   use strict;
   use warnings;
   
   # ----------------------------------------------------------------
   # Configuration
   
   #  Add a ascending number after each new translation
   # 1: add, 0: don't add
   my $numbered=0;
   
   # Add a comment after each new translation.
   # This comment contains a combination of translations which are build by using already existing translations.
   # 1: add, 0: don't add
   my $helper=0; 
   
   # Debug Mode
   # Displays additional output for debugging purposes
   my $debug=0;
   
   
   # ----------------------------------------------------------------
   # ----- Sub Routines -----
   
 sub readlexicon {  sub readlexicon {
     my $fn=shift;      my $fn=shift;
Line 17  sub readlexicon { Line 40  sub readlexicon {
   
 sub readnew {  sub readnew {
     open(IN,'newphrases.txt');      open(IN,'newphrases.txt');
     my %lexicon='';      my %lexicon=();
     while (my $line=<IN>) {      while (my $line=<IN>) {
  chomp($line);   chomp($line);
  $lexicon{$line}=$line;   $lexicon{$line}=$line;
           print "    New entry: $line\n" if $debug;
     }      }
     close(IN);      close(IN);
     return %lexicon;      return %lexicon;
 }  }
   
 # ==== Main Program  
   
 my %master=&readnew();  # ----------------------------------------------------------------
   # ----- Main Program -----
   my $i;
   my $num;
   my $dlm;
   my $comment;
   
   print "*** Synching Translation Files ***\n";
   
   # Create master hash for the entire set of all translations
   print "Building master hash:\n";
   
   # Initialy fill master hash with phrases which are additionally needed/wanted.
   print "  Adding new phrases... ";
   my %master=&readnew();
   print "ok.\n";
     
   # Add all the different phrases of all translation files to master hash
 foreach (<*.pm>) {  foreach (<*.pm>) {
     print "Reading: ".$_."\n";      print "  Reading ".$_." ... ";
     %master=(%master,&readlexicon($_));      %master=(%master,&readlexicon($_));
      print "ok.\n";
 }  }
   
 # Remove obsolete from synch  # Ignore all phrases found in removephrases.txt for current synchronization.
   # These phrases would not be removed from a translation file, if they existed in the file.
   # But the phrases will not be added to any translation file even if they were missing in it.
   # Remove these obsolete phrases from master hash
   print "  Removing obsolete phrases... ";
 open(IN,'removephrases.txt');  open(IN,'removephrases.txt');
 while (my $line=<IN>) {  while (my $line=<IN>) {
     chomp($line);      chomp($line);
     delete $master{$line};      delete $master{$line};
 }  }
 close(IN);  close(IN);
   print "ok.\n";
   
   
   print "Synchronization:\n";
 foreach my $fn (<*.pm>) {  foreach my $fn (<*.pm>) {
     print "Synching: ".$fn."\n";      print "  Synching ".$fn." ... ";
       # Build hash with all translations of current translation file
     my %lang=&readlexicon($fn);      my %lang=&readlexicon($fn);
       # Copy current translation file so that the old file could be overwritten with the new content
       # while the copy is used to read from.
     system ("cp $fn $fn.original");      system ("cp $fn $fn.original");
     open(IN,$fn.'.original');      open(IN,$fn.'.original');
       # Rebuild current translation file
       # by writing all exisiting entries until SYNCMARKER
     open(OUT,'>'.$fn);      open(OUT,'>'.$fn);
     my $found=0;      my $found=0;
     while (<IN>) {      while (<IN>) {
  if ($_=~/\#\s*SYNCMARKER/) { $found=1; last; }    if ($_=~/\#\s*SYNCMARKER/) { $found=1; last; } 
  print OUT $_;   print OUT $_;
     }      }
       # Append missing phrases to new version of current translation file
       # by synching old version of current translation file with master hash
     if ($found) {      if ($found) {
  $i=0;   $i=0;
  print OUT "\n\#SYNC ".localtime()."\n";   print OUT "\n\#SYNC ".localtime()."\n";
           # Sync master with current translation file:
  foreach my $key (sort keys %master) {   foreach my $key (sort keys %master) {
     unless ($key) { next; }      unless ($key) { next; }
     unless ($lang{$key}) {      unless ($lang{$key}) {
  my $comment='';                  print "    Found to be added: $key\n" if $debug;
  my $copytrans=$key;                  if ($helper) {
  foreach (reverse sort keys %lang) {      $comment='';
     $copytrans=~s/$_/$lang{$_}/gsi;      my $copytrans=$key;
  }                      # Create comment based on already existing translations:
  if (lc($copytrans) ne lc($key)) {      foreach (reverse sort keys %lang) {
     $comment='# '.$copytrans;          $copytrans=~s/\Q$_\E/$lang{$_}/gsi; # \Q \E: escape meta characters
       }
       if (lc($copytrans) ne lc($key)) {
           $comment='# '.$copytrans;
                       }
                 }                  }
  if ($numbered) {   if ($numbered) {
     $i++;      $i++;
Line 77  foreach my $fn (<*.pm>) { Line 134  foreach my $fn (<*.pm>) {
     $num='';      $num='';
  }   }
  if ($key=~/\'/) {   if ($key=~/\'/) {
     $del='"';      $dlm='"';
  } else {   } else {
     $del="'";      $dlm="'";
  }   }
  print OUT (<<ENDNEW);   if ($helper) {
    $del$key$del      print OUT (<<ENDNEW);
 => $del$key$num$del,     $dlm$key$dlm
   => $dlm$key$num$dlm,
 $comment  $comment
   
 ENDNEW  ENDNEW
    } else {
       print OUT (<<ENDNEW);
      $dlm$key$dlm
   => $dlm$key$num$dlm,
   
   ENDNEW
    }
     }      }
  }   }
   
Line 96  ENDNEW Line 162  ENDNEW
     }      }
     close (IN);      close (IN);
     close (OUT);      close (OUT);
       print"ok.\n";
 }  }
   print "Synchronization completed.\n";
   

Removed from v.1.7  
changed lines
  Added in v.1.10


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>