#!/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 { my $fn=shift; open(IN,$fn); my %lexicon=(); my $contents=join('',); 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=) { chomp($line); $lexicon{$line}=$line; print " New entry: $line\n" if $debug; } close(IN); return %lexicon; } # ---------------------------------------------------------------- # ----- 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>) { print " Reading ".$_." ... "; %master=(%master,&readlexicon($_)); print "ok.\n"; } # 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'); while (my $line=) { chomp($line); delete $master{$line}; } close(IN); print "ok.\n"; print "Synchronization:\n"; foreach my $fn (<*.pm>) { print " Synching ".$fn." ... "; # Build hash with all translations of current translation file 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"); open(IN,$fn.'.original'); # Rebuild current translation file # by writing all exisiting entries until SYNCMARKER open(OUT,'>'.$fn); my $found=0; while () { if ($_=~/\#\s*SYNCMARKER/) { $found=1; last; } 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) { $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}) { print " Found to be added: $key\n" if $debug; 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 () { print OUT $_; } } close (IN); close (OUT); print"ok.\n"; } print "Synchronization completed.\n";