File:  [LON-CAPA] / loncom / localize / localize / synch.pl
Revision 1.9: download - view: text, annotated - select for diffs
Tue Jan 13 12:31:13 2009 UTC (15 years, 5 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
- Changed to strict mode and added needed declarations
- Added more comments
- Added proper configuration to configure numbering and translation help comments
- Disabled translation help comments by default
- Changed delimiter variable name from del to dlm (del=delete?!?)

Note:
Script seems to add new translations from newphrases.txt even if they already existed in the translation file.

#!/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;
    open(IN,$fn);
    my %lexicon=();
    my $contents=join('',<IN>);
    close(IN);
    $contents=~s/package Apache\:[^\;]+//;
    $contents=~s/use base[^\;]+//;
    eval($contents.'; %lexicon=%Lexicon;');
    delete $lexicon{'_AUTO'};
    delete $lexicon{'char_encoding'};
    delete $lexicon{'language_code'};
    return %lexicon;
}

sub readnew {
    open(IN,'newphrases.txt');
    my %lexicon=();
    while (my $line=<IN>) {
	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($_));
}

# Remove obsolete from synch
# Ignore all phrases in removephrases.txt for current synchronization
open(IN,'removephrases.txt');
while (my $line=<IN>) {
    chomp($line);
    delete $master{$line};
}
close(IN);


foreach my $fn (<*.pm>) {
    print "Synching: ".$fn."\n";
    my %lang=&readlexicon($fn);
    system ("cp $fn $fn.original");
    open(IN,$fn.'.original');
    open(OUT,'>'.$fn);
    my $found=0;
    # Rebuild current translation file until SYNCMARKER:
    while (<IN>) {
	if ($_=~/\#\s*SYNCMARKER/) { $found=1; last; } 
	print OUT $_;
    }
    if ($found) {
	$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 (<<ENDNEW);
   $dlm$key$dlm
=> $dlm$key$num$dlm,
$comment

ENDNEW
		} else {
		    print OUT (<<ENDNEW);
   $dlm$key$dlm
=> $dlm$key$num$dlm,

ENDNEW
		}
	    }
	}

	print OUT "\n\#SYNCMARKER\n";
	foreach (<IN>) {
	    print OUT $_;
	}
    }
    close (IN);
    close (OUT);
}

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