--- loncom/localize/localize/synch.pl 2003/09/26 18:22:59 1.3 +++ loncom/localize/localize/synch.pl 2009/01/13 12:31:13 1.9 @@ -1,4 +1,23 @@ +#!/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; + + +# ---------------------------------------------------------------- +# ----- Sub Routines ----- sub readlexicon { my $fn=shift; @@ -15,24 +34,99 @@ sub readlexicon { return %lexicon; } -my %master=(); +sub readnew { + open(IN,'newphrases.txt'); + my %lexicon=(); + while (my $line=) { + chomp($line); + $lexicon{$line}=$line; + } + close(IN); + return %lexicon; +} + + +# ---------------------------------------------------------------- +# ----- Main Program ----- + +my %master=&readnew(); +my $i; +my $num; +my $dlm; +my $comment; + foreach (<*.pm>) { print "Reading: ".$_."\n"; %master=(%master,&readlexicon($_)); } -foreach $fn (<*.pm>) { + +# Remove obsolete from synch +# Ignore all phrases in removephrases.txt for current synchronization +open(IN,'removephrases.txt'); +while (my $line=) { + chomp($line); + delete $master{$line}; +} +close(IN); + + +foreach my $fn (<*.pm>) { print "Synching: ".$fn."\n"; - my %lang=&readlexicon($_); - system ("cp $fn $fn.original"); + my %lang=&readlexicon($fn); + system ("cp $fn $fn.original"); open(IN,$fn.'.original'); open(OUT,'>'.$fn); my $found=0; - foreach () { + # Rebuild current translation file until SYNCMARKER: + while () { if ($_=~/\#\s*SYNCMARKER/) { $found=1; last; } print OUT $_; } if ($found) { - print OUT "\n\#SYNC ".localtime."\n"; + $i=0; + print OUT "\n\#SYNC ".localtime()."\n"; + # Sync master with current translation file: + foreach my $key (sort keys %master) { + unless ($key) { next; } + unless ($lang{$key}) { + if ($helper) { + $comment=''; + my $copytrans=$key; + # Create comment based on already existing translations: + foreach (reverse sort keys %lang) { + $copytrans=~s/\Q$_\E/$lang{$_}/gsi; # \Q \E: escape meta characters + } + if (lc($copytrans) ne lc($key)) { + $comment='# '.$copytrans; + } + } + if ($numbered) { + $i++; + $num=' ('.$i.')'; + } else { + $num=''; + } + if ($key=~/\'/) { + $dlm='"'; + } else { + $dlm="'"; + } + if ($helper) { + print OUT (< $dlm$key$num$dlm, +$comment + +ENDNEW + } else { + print OUT (< $dlm$key$num$dlm, + +ENDNEW + } + } + } print OUT "\n\#SYNCMARKER\n"; foreach () {