Annotation of loncom/localize/localize/synch.pl, revision 1.9

1.9     ! bisitz      1: #!/usr/bin/perl
        !             2: 
        !             3: use strict;
        !             4: use warnings;
        !             5: 
        !             6: # ----------------------------------------------------------------
        !             7: # Configuration
        !             8: 
        !             9: #  Add a ascending number after each new translation
        !            10: # 1: add, 0: don't add
        !            11: my $numbered=0;
        !            12: 
        !            13: # Add a comment after each new translation.
        !            14: # This comment contains a combination of translations which are build by using already existing translations.
        !            15: # 1: add, 0: don't add
        !            16: my $helper=0; 
        !            17: 
        !            18: 
        !            19: # ----------------------------------------------------------------
        !            20: # ----- Sub Routines -----
1.3       www        21: 
1.1       www        22: sub readlexicon {
                     23:     my $fn=shift;
                     24:     open(IN,$fn);
                     25:     my %lexicon=();
                     26:     my $contents=join('',<IN>);
                     27:     close(IN);
                     28:     $contents=~s/package Apache\:[^\;]+//;
                     29:     $contents=~s/use base[^\;]+//;
                     30:     eval($contents.'; %lexicon=%Lexicon;');
                     31:     delete $lexicon{'_AUTO'};
                     32:     delete $lexicon{'char_encoding'};
                     33:     delete $lexicon{'language_code'};
                     34:     return %lexicon;
                     35: }
                     36: 
1.5       www        37: sub readnew {
                     38:     open(IN,'newphrases.txt');
1.9     ! bisitz     39:     my %lexicon=();
1.5       www        40:     while (my $line=<IN>) {
                     41: 	chomp($line);
                     42: 	$lexicon{$line}=$line;
                     43:     }
                     44:     close(IN);
                     45:     return %lexicon;
                     46: }
                     47: 
1.9     ! bisitz     48: 
        !            49: # ----------------------------------------------------------------
        !            50: # ----- Main Program -----
1.5       www        51: 
                     52: my %master=&readnew();
1.9     ! bisitz     53: my $i;
        !            54: my $num;
        !            55: my $dlm;
        !            56: my $comment;
        !            57:   
1.2       www        58: foreach (<*.pm>) {
                     59:     print "Reading: ".$_."\n";
                     60:     %master=(%master,&readlexicon($_));
                     61: }
1.4       www        62: 
1.6       www        63: # Remove obsolete from synch
1.9     ! bisitz     64: # Ignore all phrases in removephrases.txt for current synchronization
1.6       www        65: open(IN,'removephrases.txt');
                     66: while (my $line=<IN>) {
                     67:     chomp($line);
                     68:     delete $master{$line};
                     69: }
                     70: close(IN);
                     71: 
                     72: 
1.4       www        73: foreach my $fn (<*.pm>) {
1.3       www        74:     print "Synching: ".$fn."\n";
1.4       www        75:     my %lang=&readlexicon($fn);
                     76:     system ("cp $fn $fn.original");
1.2       www        77:     open(IN,$fn.'.original');
                     78:     open(OUT,'>'.$fn);
                     79:     my $found=0;
1.8       bisitz     80:     # Rebuild current translation file until SYNCMARKER:
1.4       www        81:     while (<IN>) {
1.2       www        82: 	if ($_=~/\#\s*SYNCMARKER/) { $found=1; last; } 
                     83: 	print OUT $_;
                     84:     }
                     85:     if ($found) {
1.7       www        86: 	$i=0;
1.4       www        87: 	print OUT "\n\#SYNC ".localtime()."\n";
1.8       bisitz     88:         # Sync master with current translation file:
1.5       www        89: 	foreach my $key (sort keys %master) {
                     90: 	    unless ($key) { next; }
                     91: 	    unless ($lang{$key}) {
1.9     ! bisitz     92:                 if ($helper) {
        !            93: 		    $comment='';
        !            94: 		    my $copytrans=$key;
        !            95:                     # Create comment based on already existing translations:
        !            96: 		    foreach (reverse sort keys %lang) {
        !            97: 		        $copytrans=~s/\Q$_\E/$lang{$_}/gsi; # \Q \E: escape meta characters
        !            98: 		    }
        !            99: 		    if (lc($copytrans) ne lc($key)) {
        !           100: 		        $comment='# '.$copytrans;
        !           101:                     }
1.5       www       102:                 }
1.7       www       103: 		if ($numbered) {
                    104: 		    $i++;
                    105: 		    $num=' ('.$i.')';
                    106: 		} else {
                    107: 		    $num='';
                    108: 		}
                    109: 		if ($key=~/\'/) {
1.9     ! bisitz    110: 		    $dlm='"';
1.7       www       111: 		} else {
1.9     ! bisitz    112: 		    $dlm="'";
1.7       www       113: 		}
1.9     ! bisitz    114: 		if ($helper) {
        !           115: 		    print OUT (<<ENDNEW);
        !           116:    $dlm$key$dlm
        !           117: => $dlm$key$num$dlm,
1.5       www       118: $comment
1.9     ! bisitz    119: 
1.4       www       120: ENDNEW
1.9     ! bisitz    121: 		} else {
        !           122: 		    print OUT (<<ENDNEW);
        !           123:    $dlm$key$dlm
        !           124: => $dlm$key$num$dlm,
        !           125: 
        !           126: ENDNEW
        !           127: 		}
1.4       www       128: 	    }
                    129: 	}
1.2       www       130: 
                    131: 	print OUT "\n\#SYNCMARKER\n";
                    132: 	foreach (<IN>) {
                    133: 	    print OUT $_;
                    134: 	}
                    135:     }
                    136:     close (IN);
                    137:     close (OUT);
                    138: }

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