[ Index ] |
|
Code source de WebCalendar 1.0.5 |
1 #!/usr/bin/perl 2 # 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. 11 # 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). 14 # 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. 17 # 18 # Usage: 19 # update_translation.pl [-p plugin] languagefile 20 # 21 # Example for main WebCalendar translation: 22 # update_translation.pl French.txt 23 # or 24 # update_translation.pl French 25 # 26 # Example for plugin "tnn" translation: 27 # update_translation.pl -p tnn French.txt 28 # or 29 # update_translation.pl -p tnn French 30 # 31 # Note: this utility should be run from this directory (tools). 32 # 33 ########################################################################### 34 35 $base_dir = ".."; 36 $trans_dir = "../translations"; 37 38 $base_trans_file = "$trans_dir/English-US.txt"; 39 $plugin = ""; 40 41 $show_missing = 1; # set to 0 to minimize translation file. 42 $show_dups = 0; # set to 0 to minimize translation file. 43 $verbose = 0; 44 45 ( $this ) = reverse split ( /\//, $0 ); 46 47 $save_backup = 0; 48 49 for ( $i = 0; $i < @ARGV; $i++ ) { 50 if ( $ARGV[$i] eq "-p" ) { 51 $plugin = $ARGV[++$i]; 52 } elsif ( $ARGV[$i] eq "-v" ) { 53 $verbose++; 54 } else { 55 $infile = $ARGV[$i]; 56 } 57 } 58 59 die "Usage: $this [-p plugin] language\n" if ( $infile eq "" ); 60 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"; 65 } else { 66 $p_trans_dir = $trans_dir; 67 $p_base_trans_file = $base_trans_file; 68 $p_base_dir = $base_dir; 69 } 70 71 if ( $infile !~ /txt$/ ) { 72 $infile .= ".txt"; 73 } 74 if ( -f "$trans_dir/$infile" || -f "$p_trans_dir/$infile" ) { 75 $b_infile = "$trans_dir/$infile"; 76 $infile = "$p_trans_dir/$infile"; 77 } 78 #print "infile: $infile\nb_infile: $b_infile\ntrans_dir: $trans_dir\n"; 79 80 die "Usage: $this [-p plugin] language\n" if ( ! -f $infile ); 81 82 print "Translation file: $infile\n" if ( $verbose ); 83 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 ); 88 while ( <F> ) { 89 chop; 90 s/\r*$//g; # remove annoying CR 91 next if ( /^#/ ); 92 if ( /\s*:\s*/ ) { 93 $abbrev = $`; 94 $base_trans{$abbrev} = $' if ( $abbrev ne 'charset' ); 95 } 96 } 97 close ( F ); 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"; 102 while ( <F> ) { 103 chop; 104 s/\r*$//g; # remove annoying CR 105 next if ( /^#/ ); 106 if ( /\s*:\s*/ ) { 107 $abbrev = $`; 108 $base_trans{$abbrev} = $'; 109 } 110 } 111 close ( F ); 112 } 113 114 115 # 116 # Now load the translation file we are going to update. 117 # 118 $old = ""; 119 if ( -f $infile ) { 120 print "Reading current translations from $infile\n" if ( $verbose ); 121 open ( F, $infile ) || die "Error opening $infile"; 122 $in_header = 1; 123 while ( <F> ) { 124 $old .= $_; 125 chop; 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 130 } else { 131 $header .= $_ . "\n"; 132 } 133 } 134 next if ( /^#/ ); 135 $in_header = 0; 136 if ( /\s*:\s*/ ) { 137 $abbrev = $`; 138 $trans{$abbrev} = $'; 139 } 140 } 141 } 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"; 145 $in_header = 1; 146 while ( <F> ) { 147 chop; 148 s/\r*$//g; # remove annoying CR 149 if ( /\s*:\s*/ ) { 150 $abbrev = $`; 151 $webcaltrans{$abbrev} = $'; 152 } 153 } 154 } 155 156 # 157 # Save a backup copy of old translation file. 158 # 159 if ( $save_backup ) { 160 open ( F, ">$infile.bak" ) || die "Error writing $infile.bak"; 161 print F $old; 162 close ( F ); 163 print "Backup of translation saved in $infile.bak\n"; 164 } 165 166 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"; 171 } 172 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 ) ); 177 closedir ( DIR ); 178 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 ) ); 184 closedir ( DIR ); 185 foreach $f ( @incfiles ) { 186 push ( @files, "includes/$f" ); 187 } 188 } 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 ) ); 194 closedir ( DIR ); 195 foreach $f ( @incfiles ) { 196 push ( @files, "includes/js/$f" ); 197 } 198 } 199 if ( $plugin eq "" ) { 200 push ( @files, "tools/send_reminders.php" ); 201 } 202 203 # 204 # Write new translation file. 205 # 206 $notfound = 0; 207 open ( OUT, ">$infile" ) || die "Error writing $infile: "; 208 print OUT $header; 209 if ( $plugin eq '' ) { 210 if ( defined ( $trans{'charset'} ) ) { 211 print OUT 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"; 217 } else { 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" . 221 "# charset:\n\n"; 222 } 223 } 224 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 ); 230 %thispage = (); 231 while ( <F> ) { 232 $data = $_; 233 while ( $data =~ /(translate|tooltip)\s*\(\s*"/ ) { 234 $data = $'; 235 if ( $data =~ /"\s*\)/ ) { 236 $text = $`; 237 if ( defined ( $thispage{$text} ) ) { 238 # text already found within this page... 239 } elsif ( $text eq 'charset' ) { 240 # ignore... 241 } elsif ( defined ( $text{$text} ) ) { 242 if ( ! show_dups ) { 243 if ( $pageHeader ne '' ) { 244 print OUT $pageHeader; $pageHeader = ''; 245 } 246 print OUT "# \"$text\" previously defined (in $foundin{$text})\n" 247 } 248 $thispage{$text} = 1; 249 } else { 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"; 255 } else { 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 ); 261 } 262 } 263 $text{$text} = 1; 264 $thispage{$text} = 1; 265 $foundin{$text} = $f; 266 $notfound++ if ( ! length ( $webcaltrans{$text} ) ); 267 } else { 268 $text{$text} = 1; 269 $foundin{$text} = $f; 270 $thispage{$text} = 1; 271 if ( $pageHeader ne '' ) { 272 print OUT $pageHeader; $pageHeader = ''; 273 } 274 printf OUT ( "%s: %s\n", $text, $trans{$text} ); 275 } 276 } 277 $data = $'; 278 } 279 } 280 } 281 close ( F ); 282 } 283 284 if ( ! $notfound ) { 285 print STDERR "All text was found in $infile. Good job :-)\n"; 286 } else { 287 print STDERR "$notfound translation(s) missing.\n"; 288 } 289 290 exit 0;
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Nov 30 19:09:19 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |