3 # This tool will update a translation file by doing the following:
4 # - Insert an empty translation when a missing translation is found.
5 # The translation will have "<< MISSING >>" right above it to
6 # make it easy to find.
7 # - Show the English translation (in a comment) before a missing translation,
8 # making it easier to add the translation.
9 # - Translations will be reorganized so that they are divided up into
10 # the pages that they appear on.
12 # Note: you will lose any comments you put in the translation file
13 # when using this tool (except for the comments at the very beginning).
15 # Note #2: This will overwrite the existing translation file, so a save
16 # backup of the translation will be saved with a .bak file extension.
19 # update_translation.pl [-p plugin] languagefile
21 # Example for main WebCalendar translation:
22 # update_translation.pl French.txt
24 # update_translation.pl French
26 # Example for plugin "tnn" translation:
27 # update_translation.pl -p tnn French.txt
29 # update_translation.pl -p tnn French
31 # Note: this utility should be run from this directory (tools).
33 ###########################################################################
36 $trans_dir = "../translations";
38 $base_trans_file = "$trans_dir/English-US.txt";
41 $show_missing = 1; # set to 0 to minimize translation file.
42 $show_dups = 0; # set to 0 to minimize translation file.
45 ( $this ) = reverse split ( /\//, $0 );
49 for ( $i = 0; $i < @ARGV; $i++ ) {
50 if ( $ARGV[$i] eq "-p" ) {
51 $plugin = $ARGV[++$i];
52 } elsif ( $ARGV[$i] eq "-v" ) {
59 die "Usage: $this [-p plugin] language\n" if ( $infile eq "" );
61 if ( $plugin ne "" ) {
62 $p_trans_dir = "$base_dir/$plugin/translations";
63 $p_base_trans_file = "$p_trans_dir/English-US.txt";
64 $p_base_dir = "$base_dir/$plugin";
66 $p_trans_dir = $trans_dir;
67 $p_base_trans_file = $base_trans_file;
68 $p_base_dir = $base_dir;
71 if ( $infile !~ /txt$/ ) {
74 if ( -f "$trans_dir/$infile" || -f "$p_trans_dir/$infile" ) {
75 $b_infile = "$trans_dir/$infile";
76 $infile = "$p_trans_dir/$infile";
78 #print "infile: $infile\nb_infile: $b_infile\ntrans_dir: $trans_dir\n";
80 die "Usage: $this [-p plugin] language\n" if ( ! -f $infile );
82 print "Translation file: $infile\n" if ( $verbose );
84 # Now load the base translation(s) file (English), so that we can include
85 # the English translation text above a missing translation in a comment.
86 open ( F, $base_trans_file ) || die "Error opening $base_trans_file";
87 print "Reading base translation file: $base_trans_file\n" if ( $verbose );
90 s/\r*$//g; # remove annoying CR
94 $base_trans{$abbrev} = $' if ( $abbrev ne 'charset' );
98 # read in the plugin base translation file
99 if ( $plugin ne "" ) {
100 print "Reading plugin base translation file: $p_base_trans_file\n" if ( $verbose );
101 open ( F, $p_base_trans_file ) || die "Error opening $p_base_trans_file";
104 s/\r*$//g; # remove annoying CR
108 $base_trans{$abbrev} = $';
116 # Now load the translation file we are going to update.
120 print "Reading current translations from $infile\n" if ( $verbose );
121 open ( F, $infile ) || die "Error opening $infile";
126 s/\r*$//g; # remove annoying CR
127 if ( $in_header && /^#/ ) {
128 if ( /Translation last (pagified|updated)/ ) {
129 # ignore since we will replace this with current date below
131 $header .= $_ . "\n";
138 $trans{$abbrev} = $';
142 if ( $plugin ne "" ) {
143 print "Reading current WebCalendar translations from $b_infile\n" if ( $verbose );
144 open ( F, $b_infile ) || die "Error opening $b_infile";
148 s/\r*$//g; # remove annoying CR
151 $webcaltrans{$abbrev} = $';
157 # Save a backup copy of old translation file.
159 if ( $save_backup ) {
160 open ( F, ">$infile.bak" ) || die "Error writing $infile.bak";
163 print "Backup of translation saved in $infile.bak\n";
167 if ( $header !~ /Translation last updated/ ) {
168 ( $day, $mon, $year ) = ( localtime ( time() ) )[3,4,5];
169 $header .= "# Translation last updated on " .
170 sprintf ( "%02d-%02d-%04d", $mon + 1, $day, $year + 1900 ) . "\n";
173 # First get the list of .php files
174 print "Searching for PHP files in $p_base_dir\n" if ( $verbose );
175 opendir ( DIR, $p_base_dir ) || die "Error opening $p_base_dir";
176 @files = grep ( /\.php$/, readdir ( DIR ) );
179 if ( -d "$p_base_dir/includes" ) {
180 print "Searching for PHP files in $p_base_dir/includes\n" if ( $verbose );
181 opendir ( DIR, "$p_base_dir/includes" ) ||
182 die "Error opening $p_base_dir/includes";
183 @incfiles = grep ( /\.php$/, readdir ( DIR ) );
185 foreach $f ( @incfiles ) {
186 push ( @files, "includes/$f" );
189 if ( -d "$p_base_dir/includes/js" ) {
190 print "Searching for PHP files in $p_base_dir/includes/js\n" if ( $verbose );
191 opendir ( DIR, "$p_base_dir/includes/js" ) ||
192 die "Error opening $p_base_dir/includes/js";
193 @incfiles = grep ( /\.php$/, readdir ( DIR ) );
195 foreach $f ( @incfiles ) {
196 push ( @files, "includes/js/$f" );
199 if ( $plugin eq "" ) {
200 push ( @files, "tools/send_reminders.php" );
204 # Write new translation file.
207 open ( OUT, ">$infile" ) || die "Error writing $infile: ";
209 if ( $plugin eq '' ) {
210 if ( defined ( $trans{'charset'} ) ) {
212 "\n\n###############################################\n" .
213 "# Specify a charset (will be sent within meta tag for each page)\n#\n" .
214 "charset: $trans{'charset'}\n\n";
215 $text{'charset'} = 1;
216 $foundin{'charset'} = " top of this file";
218 print OUT "\n# No charset specified (not needed for iso-8859-1)\n" .
219 "# \"charset\" is used in a meta tag, " .
220 "do not translate \"charset\" here.\n" .
225 foreach $f ( @files ) {
226 $pageHeader = "\n\n###############################################\n# Page: $f\n#\n";
227 $file = "$p_base_dir/$f";
228 open ( F, $file ) || die "Error reading $file";
229 print "Searching $f\n" if ( $verbose );
233 while ( $data =~ /(translate|tooltip)\s*\(\s*"/ ) {
235 if ( $data =~ /"\s*\)/ ) {
237 if ( defined ( $thispage{$text} ) ) {
238 # text already found within this page...
239 } elsif ( $text eq 'charset' ) {
241 } elsif ( defined ( $text{$text} ) ) {
243 if ( $pageHeader ne '' ) {
244 print OUT $pageHeader; $pageHeader = '';
246 print OUT "# \"$text\" previously defined (in $foundin{$text})\n"
248 $thispage{$text} = 1;
250 if ( ! length ( $trans{$text} ) ) {
251 if ( $show_missing ) {
252 if ( length ( $webcaltrans{$text} ) ) {
253 if ( $pageHeader ne '' ) { print OUT $pageHeader; $pageHeader = ''; }
254 print OUT "# \"$text\" defined in WebCalendar translation\n";
256 if ( $pageHeader ne '' ) { print OUT $pageHeader; $pageHeader = ''; }
257 print OUT "#\n# << MISSING >>\n# $text:\n";
258 print OUT "# English text: $base_trans{$text}\n#\n"
259 if ( length ( $base_trans{$text} ) &&
260 $base_trans{$text} ne $text );
264 $thispage{$text} = 1;
265 $foundin{$text} = $f;
266 $notfound++ if ( ! length ( $webcaltrans{$text} ) );
269 $foundin{$text} = $f;
270 $thispage{$text} = 1;
271 if ( $pageHeader ne '' ) {
272 print OUT $pageHeader; $pageHeader = '';
274 printf OUT ( "%s: %s\n", $text, $trans{$text} );
285 print STDERR "All text was found in $infile. Good job :-)\n";
287 print STDERR "$notfound translation(s) missing.\n";