File:
[LON-CAPA] /
loncom /
localize /
localize /
synch.pl
Revision
1.14:
download - view:
text,
annotated -
select for diffs
Wed Dec 9 10:30:45 2009 UTC (14 years, 5 months ago) by
bisitz
Branches:
MAIN
CVS tags:
version_2_9_99_0,
version_2_11_0_RC2,
version_2_11_0_RC1,
version_2_10_X,
version_2_10_1,
version_2_10_0_RC2,
version_2_10_0_RC1,
version_2_10_0,
loncapaMITrelate_1,
language_hyphenation_merge,
language_hyphenation,
bz6209-base,
bz6209,
PRINT_INCOMPLETE_base,
PRINT_INCOMPLETE,
HEAD,
BZ4492-merge,
BZ4492-feature_horizontal_radioresponse
- Ignore emtpy rows in newphrases.txt and removephrases.txt
- Added some comments
- Optimized quotation warning message
1: #!/usr/bin/perl
2:
3: use strict;
4: use warnings;
5:
6: # ----------------------------------------------------------------
7: # Configuration
8:
9: # Add an ascending number after each new translation
10: # 1: add, 0: don't add
11: my $numbered=0;
12:
13: # Add a translation help comment after each new translation.
14: # This comment contains a combination of translations which are built by using already existing translations.
15: # 1: add, 0: don't add
16: my $helper=0;
17:
18: # Debug Mode
19: # Displays additional output for debugging purposes
20: # WARNING: Creates a huge amount of output. Recommended to be used only for small test files.
21: # 1: display, 0: don't display
22: my $debug=0;
23:
24: # List of files to be accepted as translation files for synching
25: # These files contain actual translation phrases.
26: # If you want to exclude translation files from being included
27: # in the synching process, remove their file names from this list.
28: my @listoffiles=(
29: # 'test.pm'
30: ,'ar.pm'
31: ,'de.pm'
32: ,'es.pm'
33: ,'fa.pm'
34: ,'fr.pm'
35: ,'he.pm'
36: ,'ja.pm'
37: ,'pt.pm'
38: ,'ru.pm'
39: ,'tr.pm'
40: ,'zh.pm'
41: );
42:
43: # ----------------------------------------------------------------
44: # ----- Sub Routines -----
45:
46: sub readlexicon {
47: # Read translation file into memory
48: my $fn=shift;
49: open(IN,$fn) or die;
50: my %lexicon=();
51: my $contents=join('',<IN>);
52: close(IN);
53: # Tidy up: remove header data
54: $contents=~s/package Apache\:[^\;]+//;
55: $contents=~s/use base[^\;]+//;
56: # Build hash with hash from file
57: my %Lexicon=();
58: eval($contents.'; %lexicon=%Lexicon;');
59: if ($@ ne "") {
60: print "\nAn error occurred during the attempt to retrieve the translation hash for the file '$fn'.\n"
61: ."Error: ".$@."\n";
62: die;
63: }
64: # Remove entries which are not needed for synch
65: delete $lexicon{'_AUTO'};
66: delete $lexicon{'char_encoding'};
67: delete $lexicon{'language_code'};
68: # Hash is expected not to be empty
69: print scalar(keys(%lexicon))." found... ";
70: if (!scalar(keys(%lexicon))) {
71: print "\nWarning: No translation phrases from '$fn'.\n";
72: }
73: return %lexicon;
74: }
75:
76: sub readnew {
77: print "\n" if $debug;
78: open(IN,'newphrases.txt') or die;
79: my %lexicon=();
80: while (my $line=<IN>) {
81: chomp($line);
82: next if ($line eq '');
83: $lexicon{$line}=$line;
84: print " New entry: '$line'\n" if $debug;
85: }
86: close(IN);
87: return %lexicon;
88: }
89:
90: sub takethisfile {
91: my $file = shift;
92: foreach my $listfile (@listoffiles) {
93: if ($listfile eq $file) { return 1 }
94: }
95: return 0;
96: }
97:
98:
99:
100: # ----------------------------------------------------------------
101: # ----- Main Program -----
102: my $i; # Count new phrases
103: my $num;
104: my $dlm; # Delimiter character
105: my $comment;
106:
107: print "*** Synching Translation Files ***\n";
108:
109: # Create master hash for the entire set of all translations
110: print "Building master hash:\n";
111:
112: # Initially fill master hash with phrases which are additionally needed/wanted.
113: print " Adding new phrases... ";
114: my %master=&readnew();
115: print scalar(keys(%master))." added... ";
116: print "ok.\n";
117:
118: # Add all the different phrases of all translation files to master hash
119: foreach (<*.pm>) {
120: if (&takethisfile($_)) {
121: print " Reading '".$_."'... ";
122: %master=(%master,&readlexicon($_));
123: print "ok.\n";
124: }
125: }
126:
127: # Ignore all phrases found in removephrases.txt for current synchronization.
128: # These phrases would not be removed from a translation file, if they existed in the file.
129: # But the phrases will not be added to any translation file even if they were missing in it.
130: # Remove these obsolete phrases from master hash
131: print " Removing obsolete phrases... ";
132: open(IN,'removephrases.txt') or die;
133: my $rm=0;
134: while (my $line=<IN>) {
135: chomp($line);
136: next if ($line eq '');
137: delete $master{$line};
138: $rm++;
139: }
140: close(IN);
141: print "$rm removed... ok.\n";
142:
143:
144: print "Synchronization:\n";
145: my $quotwarn=0;
146: foreach my $fn (<*.pm>) {
147: if (!&takethisfile($fn)) { next }
148: print " Synching '".$fn."'... ";
149: # Build hash with all translations of current translation file
150: my %lang=&readlexicon($fn);
151: # Copy current translation file so that the old file could be overwritten with the new content
152: # while the copy is used to read from.
153: system ("cp $fn $fn.original");
154: open(IN,$fn.'.original') or die;
155: # Rebuild current translation file
156: # by writing all exisiting entries until SYNCMARKER
157: open(OUT,'>'.$fn) or die;
158: my $found=0;
159: while (<IN>) {
160: if ($_=~/\#\s*SYNCMARKER/) { $found=1; last; }
161: print OUT $_;
162: }
163: # Append missing phrases to new version of current translation file
164: # by synching old version of current translation file with master hash
165: if ($found) { # Only change files where SYNCMARKER was found
166: $i=0;
167: print OUT "\n\#SYNC ".localtime()."\n";
168: # Sync master with current translation file:
169: foreach my $key (sort keys %master) {
170: print "\n Checking key: '$key'" if $debug;
171: next unless ($key);
172: unless ($lang{$key}) {
173: # Translation helper?
174: if ($helper) {
175: $comment='';
176: my $copytrans=$key;
177: # Create comment based on already existing translations:
178: foreach (reverse sort keys %lang) {
179: $copytrans=~s/\Q$_\E/$lang{$_}/gsi; # \Q \E: escape meta characters
180: }
181: if (lc($copytrans) ne lc($key)) {
182: $comment='# '.$copytrans;
183: }
184: }
185: # Numbered?
186: $i++;
187: if ($numbered) {
188: $num=' ('.$i.')';
189: } else {
190: $num='';
191: }
192: # Find delimiter for key and value
193: if (($key=~/\'/) & ($key=~/\"/)) {
194: $quotwarn++;
195: print " (Warning: Both, ' and \", occur!)" if $debug;
196: }
197: # if (($key=~/[^\\]\'/) | ($key=~/\\\"/)) {
198: if ($key=~/\'/) {
199: $dlm='"';
200: } else {
201: $dlm="'";
202: }
203: # Write new entry to translation file
204: print OUT (<<ENDNEW);
205: $dlm$key$dlm
206: => $dlm$key$num$dlm,
207: ENDNEW
208: if ($helper) {
209: print OUT $comment
210: }
211: print OUT "\n";
212: print " > added" if $debug;
213: }
214: }
215: # Add SYNCMARKER at end of file
216: print OUT "\n\#SYNCMARKER\n";
217: foreach (<IN>) {
218: print OUT $_;
219: }
220: }
221: close (IN);
222: close (OUT);
223: print "\n" if $debug;
224: print"$i added... ok.\n";
225: }
226: if ($quotwarn) {
227: print "Warning: Issues expected due to occurrence of ' and \" in $quotwarn new key(s).\n";
228: }
229: print "Synchronization completed.\n";
230:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>