[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 <?php 2 /** 3 * File to handle the maintaining of the static html files of this package 4 * 5 * b2evolution - {@link http://b2evolution.net/} 6 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html} 7 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/} 8 * Parts of this file are copyright (c)2004 by Daniel HAHLER - {@link http://thequod.de/contact}. 9 * 10 * {@internal Open Source relicensing agreement: 11 * Daniel HAHLER grants Francois PLANQUE the right to license 12 * Daniel HAHLER's contributions to this file and the b2evolution project 13 * under any OSI approved OSS license (http://www.opensource.org/licenses/). 14 * }} 15 * 16 * @package internal 17 * @author blueyed: Daniel HAHLER 18 */ 19 20 /** 21 * Load config 22 */ 23 require ( '../blogs/conf/_config.php' ); 24 25 define( 'EVO_MAIN_INIT', true ); 26 27 /**#@+ 28 * Load required functions 29 */ 30 require_once $inc_path.'_core/_class4.funcs.php'; 31 load_class('_core/model/_log.class.php'); 32 load_class('_core/_param.funcs.php'); 33 load_funcs('_core/_misc.funcs.php'); 34 load_funcs('_core/ui/forms/_form.funcs.php'); 35 require dirname(__FILE__).'/pofile.class.php'; 36 /**#@-*/ 37 38 $Debuglog = new Log(); 39 40 41 $pofilepath = dirname(__FILE__).'/langfiles'; 42 43 // ------------------- CONFIG ------------------------ 44 // TODO: use other markers and do not use it for replacement tags like 45 // {{{trans_current}}}, which only gets replaced by the current locale 46 define( 'TRANSTAG_OPEN', '{{{' ); 47 define( 'TRANSTAG_CLOSE', '}}}' ); 48 define( 'CHDIR_TO_BLOGS', '..' ); 49 define( 'STATIC_POT', $pofilepath.'\static.POT' ); 50 define( 'DEFAULT_TARGET', 'en-US' ); 51 define( 'DEFAULT_CHARSET', 'iso-8859-1' ); 52 53 param('highlight_untranslated', 'integer', 0 ); 54 param('action', 'string', '' ); 55 56 57 // look what translations we have 58 $pofiles = glob( $pofilepath.'/*.static.po' ); 59 $targets[ DEFAULT_TARGET ] = ''; 60 61 // add targets that use same message file 62 foreach( $locales as $key => $value ) 63 { 64 if( $value['enabled'] && substr($value['messages'], 0, 2 ) == substr( $locales[ DEFAULT_TARGET ]['messages'], 0, 2 ) ) 65 { 66 $targets[ $key ] = ''; 67 } 68 } 69 70 // Discover targets from *.static.po files 71 foreach( $pofiles as $po ) 72 { 73 $target = basename( $po, '.static.po' ); 74 $targets[ $target ] = $po; 75 76 // add targets that use same message file 77 foreach( $locales as $key => $value ) if( $key != $target ) 78 { 79 if( $value['enabled'] && $value['messages'] == $locales[ $target ]['messages'] ) 80 { 81 $targets[ $key ] = $po; 82 } 83 } 84 } 85 86 87 if( !isset($argv) ) 88 { // html head 89 ?> 90 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 91 <html xml:lang="en" lang="en"> 92 <head> 93 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 94 <title>b2evo :: static page generation</title> 95 <link href="../blogs/skins_adm/legacy/rsc/css/desert.css" rel="stylesheet" type="text/css" title="Desert" /> 96 </head> 97 <body> 98 <div class="center" style="margin:auto;width:75%"> 99 100 <img src="<?php echo $rsc_url ?>img/b2evolution_logo_360.gif" /><br /> 101 <?php 102 } 103 104 log_('<hr />'); 105 log_('This script maintains the static html files of the b2evolution project.'); 106 log_('written by <a href="http://thequod.de/contact">daniel hahler</a>, 2004'); 107 108 if( isset($argv) ) 109 { // commandline mode 110 log_('<hr />'); 111 if( isset($argv[1]) && in_array($argv[1], array('extract', 'merge')) ) 112 { 113 $action = $argv[1]; 114 } 115 else 116 { 117 echo " 118 Usage: $argv[0] <extract|merge> 119 extract: extracts all translatable strings into ".STATIC_POT.". 120 merge: creates all static files for which there are .po files in the current directory. 121 \ntargets: ".implode(', ', array_keys( $targets ))." 122 "; 123 exit; 124 } 125 } 126 elseif( $action == '' ) 127 { 128 htmlmenu(); 129 } 130 131 function htmlmenu() 132 { 133 global $targets, $highlight_untranslated; 134 echo ' 135 <hr /> 136 <br /> 137 <div style="width:75%;margin:auto"> 138 <form method="get" class="fform"> 139 <fieldset> 140 <legend>Create translated files</legend> 141 <input type="hidden" name="action" value="merge" /> 142 <input type="checkbox" value="1" name="highlight_untranslated" '.( ($highlight_untranslated) ? 'checked="checked"' : '' ).' /> 143 highlight untranslated strings 144 <br /><br />(targets: '.implode(', ', array_keys( $targets )).') 145 <br /><br /><input type="submit" value="create static files from locales .po files" class="search" /> 146 </fieldset> 147 </form> 148 149 <form method="get" class="fform"> 150 <fieldset> 151 <legend>Extract translatable strings into .POT file</legend> 152 <input type="hidden" name="action" value="extract" /> 153 '; 154 form_info( 'static POT file', str_replace( '\\', '/', STATIC_POT ) ); 155 echo ' 156 <input type="submit" value="extract" class="search" /> 157 </fieldset> 158 </form> 159 </div> 160 <br /><br /> 161 '; 162 }; 163 164 165 /** 166 * output, respects commandline mode 167 */ 168 function log_( $string ) 169 { 170 global $argv; 171 if( isset($argv) ) 172 { // command line mode 173 if( $string == '<hr />' ) 174 echo '------------------------------------------------------------------------------'; 175 else 176 { // remove tags 177 $string = strip_tags($string); 178 echo $string; 179 } 180 echo "\n"; 181 } 182 else 183 { 184 echo $string."<br />\n"; 185 } 186 } 187 188 189 // HERE WE GO ------------------------------- 190 191 if( $action ) 192 { 193 log_('<div class="panelinfo"><p><strong>action: '.$action.'</strong></p>' 194 .( ( $highlight_untranslated && $action == 'merge' ) ? 'note: untranslated strings will get highlighted!' : '' ) 195 .'</div>' 196 ); 197 } 198 199 // change to /blogs folder 200 chdir( CHDIR_TO_BLOGS ); 201 #pre_dump( getcwd(), 'cwd' ); 202 203 // get the source files 204 $srcfiles = array(); 205 206 foreach( array( '.', 'doc' ) as $dir ) 207 { 208 if( $fp = opendir($dir) ) 209 { 210 while( ($file = readdir($fp) ) !== false ) 211 { 212 if( $dir != '.' ) 213 { 214 $file = $dir.'/'.$file; 215 } 216 if( is_file($file) && preg_match('/\.src\.html$/', $file)) 217 { 218 $srcfiles[] = $file; 219 } 220 } 221 closedir($fp); 222 } 223 else log( 'could not open directory '.$dir ); 224 } 225 226 // echo '<hr />'; pre_dump( $srcfiles, 'source files' ); echo '<hr />'; 227 228 229 switch( $action ) 230 { 231 case 'extract': 232 233 $POTFile = new POTFile( STATIC_POT ); 234 235 foreach( $srcfiles as $srcfile ) 236 { 237 log_( 'Extracting '.$srcfile.'..' ); 238 239 // get source file content 240 $text = implode( '', file( $srcfile ) ); 241 // get all strings to translate 242 preg_match_all('/'.TRANSTAG_OPEN.'(.*?)'.TRANSTAG_CLOSE.'/s', $text, $matches_msgids, PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE); 243 // get all newlines (to assign source file line numbers to msgids later) 244 preg_match_all('/\n/', $text, $matches_line, PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE); 245 246 $lm = 0; // represents line numer - 1 247 foreach( $matches_msgids[1] as $match ) 248 { 249 while( isset($matches_line[0][$lm]) && ($match[1] > $matches_line[0][ $lm ][1]) ) 250 { // assign line numbers 251 $lm++; 252 } 253 $POTFile->addmsgid( $match[0], $srcfile.':'.($lm + 1) ); 254 #log_(' ['.$srcfile.':'.($lm + 1).']<br />'); 255 } 256 } 257 258 // write POT file 259 $POTFile->write(); 260 break; 261 262 case 'merge': 263 foreach( $targets as $target => $targetmessagefile ) 264 { // loop targets/locales 265 log_('<h2 style="margin-bottom:0">TARGET: '.$target.'</h2>'); 266 if( $targetmessagefile != '' ) 267 { // only translate when not DEFAULT_TARGET 268 log_( 'reading .po file: '.basename( $targetmessagefile ) ); 269 270 $POFile = new POFile( $targetmessagefile ); 271 $POFile->read(); 272 273 // get charset out of .PO file header 274 if( preg_match( '/; charset=(.*?)\n/', $POFile->translate(''), $matches ) ) 275 { 276 $charset = $matches[1]; 277 if( $charset == 'CHARSET' ) 278 { 279 log_('Invalid charset "'.$charset.'". Will use default, '.DEFAULT_CHARSET); 280 $charset = DEFAULT_CHARSET; 281 } 282 log_('Charset: '.$charset); 283 } 284 else 285 { 286 log_('<span style="color:red">WARNING: no charset found. Will use '.DEFAULT_CHARSET.'.</span>'); 287 $charset = DEFAULT_CHARSET; 288 } 289 } 290 else 291 { // no $targetmessagefile, so we don't translate 292 $charset = DEFAULT_CHARSET; 293 log_( 'building default files'); 294 $POFile = new POFile(''); 295 } 296 $replacesrc = $target != DEFAULT_TARGET ? 297 '.'.$target.'.' : 298 '.'; 299 300 301 foreach( $srcfiles as $srcfile ) 302 { // loop through sourcefiles 303 // the file to create 304 $newfilename = str_replace('.src.', $replacesrc, $srcfile); 305 306 log_( 'Merging '.$srcfile.' into '.$newfilename ); 307 $text = implode( '', file( $srcfile ) ); 308 309 $path_to_root = ''; 310 for($i = 1; $i < count(split('/', $srcfile)); $i++) 311 { 312 $path_to_root = '../'.$path_to_root; 313 } 314 315 316 // --- build "available translations" list ------------- 317 locale_activate( $target ); // activate locale to translate locale names 318 319 // Sort the targets by their translated name 320 $sortedTargets = $targets; 321 ksort( $sortedTargets ); 322 323 $trans_available = "\n"; 324 foreach( $sortedTargets as $ttarget => $ttargetmessagefile ) 325 { // the link to the static html file for that target message file 326 $linkto = str_replace('.src.', ( $ttarget != DEFAULT_TARGET ) ? ".$ttarget." : '.', basename($srcfile) ); 327 328 $trans_available .= 329 '<a href="'.$linkto.'" title="'.T_( $locales[$ttarget]['name'] ).'">' 330 .locale_flag($ttarget, 'h10px', 'flag', '', false, $path_to_root.'blogs/rsc/flags') 331 .'</a>'."\n"; 332 } 333 $trans_available .= "\n"; 334 335 $text = str_replace( TRANSTAG_OPEN.'trans_available'.TRANSTAG_CLOSE, $trans_available, $text ); 336 337 // Current lang: 338 $text = str_replace( TRANSTAG_OPEN.'trans_current'.TRANSTAG_CLOSE, T_( $locales[$target]['name'] ), $text ); 339 340 // standard replacements 341 $search = array( 342 // internal replacements 343 TRANSTAG_OPEN.'trans_locale'.TRANSTAG_CLOSE, 344 urlencode( TRANSTAG_OPEN.'trans_locale'.TRANSTAG_CLOSE ), // DW fix 345 TRANSTAG_OPEN.'trans_charset'.TRANSTAG_CLOSE, 346 '<html', // add note about generator 347 ); 348 $replace = array( 349 $target, 350 $target, 351 $charset, 352 '<!-- This file was generated automatically by /gettext/staticfiles.php - Do not edit this file manually -->'."\n".'<html' 353 ); 354 $text = str_replace( $search, $replace, $text); 355 356 357 // emphasize links to start page (small-caps) 358 #pre_dump('/index.([a-z]{2}-[A-Z]{2}(-.{1,14})?.)?html/', $newfilename ); 359 /*if( preg_match( '/index.([a-z]{2}-[A-Z]{2}(-.{1,14})?.)?html/', $newfilename ) ) 360 { // start page is current 361 $text = preg_replace( '/(<a .*?>)({{{Start page}}})(<\/a>)/s', '$1<span style="font-variant:small-caps">$2</span>$3', $text ); 362 }*/ 363 364 // emphasize current flag link (<strong>) 365 $text = preg_replace( '/(<a[^>]+href="(..\/)?'.basename($newfilename).'(\?.*?)?"[^>]*><img .*?>)(.+?)(<\/a>)/s', 366 '$1<strong>$4</strong>$5', 367 $text); 368 369 370 if( $targetmessagefile != '' ) 371 { // translate everything 372 $text = preg_replace( '/'.TRANSTAG_OPEN.'(.*?)'.TRANSTAG_CLOSE.'/es', '$POFile->translate(stripslashes(\'$1\'))', $text ); 373 374 if( strpos( $text, TRANSTAG_OPEN ) !== false ) 375 { // there are still tags. 376 #pre_dump( $text, substr( $text, strpos( $text, TRANSTAG_OPEN ), 30 ) ); 377 log_('<span style="color:blue">WARNING: some strings have not been translated!</span>'); 378 } 379 } 380 381 382 // handle left TRANSTAGs 383 if( $highlight_untranslated && $targetmessagefile != '' ) 384 { // we want to highlight untranslated strings 385 $text = str_replace( array(TRANSTAG_OPEN, TRANSTAG_CLOSE), array('<span style="color:red" title="not translated">', '</span>'), $text ); 386 } 387 else 388 { // just remove tags 389 $text = str_replace( array(TRANSTAG_OPEN, TRANSTAG_CLOSE), '', $text ); 390 } 391 392 393 // replace links 394 $text = preg_replace( '/\.src\.(html)/', $replacesrc."$1", $text ); 395 396 397 // remove DW tags 398 $text = preg_replace( '/<!-- Instance(Begin|End|Param).*? -->/', '', $text ); 399 400 $fh = @fopen( $newfilename, 'w' ); 401 if( ! $fh ) 402 { 403 log_( sprintf('<p class="error">Could not open %s for writing!</p>', $newfilename) ); 404 } 405 else 406 { 407 fwrite( $fh, $text ); 408 fclose( $fh ); 409 } 410 } 411 log_(''); 412 } 413 414 break; 415 } 416 417 log_(''); 418 log_('Finito.'); 419 420 if( !isset($argv) ) 421 { 422 if( !empty($action) ) 423 htmlmenu(); 424 echo '</div></body></html>'; 425 } 426 427 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |