00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 include_once( "lib/ezi18n/classes/eztranslatorhandler.php" );
00042 include_once( "lib/ezi18n/classes/eztextcodec.php" );
00043 include_once( "lib/ezi18n/classes/eztranslationcache.php" );
00044
00045 class eZTSTranslator extends eZTranslatorHandler
00046 {
00047
00048
00049
00050 function eZTSTranslator( $locale, $filename = null, $useCache = true )
00051 {
00052 $this->UseCache = $useCache;
00053 if ( isset( $GLOBALS['eZSiteBasics'] ) )
00054 {
00055 $siteBasics = $GLOBALS['eZSiteBasics'];
00056 if ( isset( $siteBasics['no-cache-adviced'] ) && $siteBasics['no-cache-adviced'] )
00057 $this->UseCache = false;
00058 }
00059 $this->BuildCache = false;
00060 $this->eZTranslatorHandler( true );
00061
00062 $this->Locale = $locale;
00063 $this->File = $filename;
00064 $this->Messages = array();
00065 $this->CachedMessages = array();
00066 $this->HasRestoredCache = false;
00067 $this->RootCache = false;
00068 }
00069
00070
00071
00072
00073
00074 function &initialize( $context, $locale, $filename, $useCache = true )
00075 {
00076 $tables =& $GLOBALS["eZTSTranslationTables"];
00077 $instance = false;
00078 $file = $locale . '/' . $filename;
00079 if ( isset( $tables[$file] ) and
00080 get_class( $tables[$file] ) == "eztstranslator" )
00081 $instance =& $tables[$file];
00082 if ( $instance and
00083 $instance->hasInitializedContext( $context ) )
00084 return $instance;
00085 eZDebug::createAccumulatorGroup( 'tstranslator', 'TS translator' );
00086 eZDebug::accumulatorStart( 'tstranslator_init', 'tstranslator', 'TS init' );
00087 if ( !$instance )
00088 {
00089 $instance = new eZTSTranslator( $locale, $filename, $useCache );
00090 $tables[$file] =& $instance;
00091 $manager =& eZTranslatorManager::instance();
00092 $manager->registerHandler( $instance );
00093 }
00094 $instance->load( $context );
00095 eZDebug::accumulatorStop( 'tstranslator_init' );
00096 return $instance;
00097 }
00098
00099
00100
00101
00102 function hasInitializedContext( $context )
00103 {
00104 return isset( $this->CachedMessages[$context] );
00105 }
00106
00107
00108
00109
00110 function load( $requestedContext )
00111 {
00112 return $this->loadTranslationFile( $this->Locale, $this->File, $requestedContext );
00113 }
00114
00115
00116
00117
00118 function loadTranslationFile( $locale, $filename, $requestedContext )
00119 {
00120 include_once( 'lib/ezfile/classes/ezdir.php' );
00121
00122
00123 $charset = eZTextCodec::internalCharset();
00124 $tsTimeStamp = false;
00125
00126 if ( !$this->RootCache )
00127 {
00128 $ini =& eZINI::instance();
00129 $roots = array( $ini->variable( 'RegionalSettings', 'TranslationRepository' ) );
00130 $extensionBase = eZExtension::baseDirectory();
00131 $translationExtensions = $ini->variable( 'RegionalSettings', 'TranslationExtensions' );
00132 foreach ( $translationExtensions as $translationExtension )
00133 {
00134 $extensionPath = $extensionBase . '/' . $translationExtension . '/translations';
00135 if ( file_exists( $extensionPath ) )
00136 {
00137 $roots[] = $extensionPath;
00138 }
00139 }
00140 $this->RootCache = array( 'roots' => $roots );
00141 }
00142 else
00143 {
00144 $roots = $this->RootCache['roots'];
00145 if ( isset( $this->RootCache['timestamp'] ) )
00146 $tsTimeStamp = $this->RootCache['timestamp'];
00147 }
00148
00149
00150 if ( $this->UseCache == true )
00151 {
00152 if ( !$tsTimeStamp )
00153 {
00154 foreach ( $roots as $root )
00155 {
00156 $path = eZDir::path( array( $root, $locale, $charset, $filename ) );
00157 if ( file_exists( $path ) )
00158 {
00159 $timestamp = filemtime( $path );
00160 if ( $timestamp > $tsTimeStamp )
00161 $tsTimeStamp = $timestamp;
00162 }
00163 else
00164 {
00165 $path = eZDir::path( array( $root, $locale, $filename ) );
00166 if ( file_exists( $path ) )
00167 {
00168 $timestamp = filemtime( $path );
00169 if ( $timestamp > $tsTimeStamp )
00170 $tsTimeStamp = $timestamp;
00171 }
00172 }
00173 }
00174 $this->RootCache['timestamp'] = $tsTimeStamp;
00175 }
00176 $key = 'cachecontexts';
00177 if ( $this->HasRestoredCache or
00178 eZTranslationCache::canRestoreCache( $key, $tsTimeStamp ) )
00179 {
00180 eZDebug::accumulatorStart( 'tstranslator_cache_load', 'tstranslator', 'TS cache load' );
00181 if ( !$this->HasRestoredCache )
00182 {
00183 if ( !eZTranslationCache::restoreCache( $key ) )
00184 {
00185 $this->BuildCache = true;
00186 }
00187 $contexts = eZTranslationCache::contextCache( $key );
00188 if ( !is_array( $contexts ) )
00189 $contexts = array();
00190 $this->HasRestoredCache = $contexts;
00191 }
00192 else
00193 $contexts = $this->HasRestoredCache;
00194 if ( !$this->BuildCache )
00195 {
00196 $contextName = $requestedContext;
00197 if ( !isset( $this->CachedMessages[$contextName] ) )
00198 {
00199 eZDebug::accumulatorStart( 'tstranslator_context_load', 'tstranslator', 'TS context load' );
00200 if ( eZTranslationCache::canRestoreCache( $contextName, $tsTimeStamp ) )
00201 {
00202 if ( !eZTranslationCache::restoreCache( $contextName ) )
00203 {
00204 $this->BuildCache = true;
00205 }
00206 $this->CachedMessages[$contextName] =
00207 eZTranslationCache::contextCache( $contextName );
00208
00209 foreach ( $this->CachedMessages[$contextName] as $key => $msg )
00210 {
00211 $this->Messages[$key] = $msg;
00212 }
00213 }
00214 eZDebug::accumulatorStop( 'tstranslator_context_load' );
00215 }
00216 }
00217 eZDebugSetting::writeNotice( 'i18n-tstranslator', "Loading cached translation", "eZTSTranslator::loadTranslationFile" );
00218 eZDebug::accumulatorStop( 'tstranslator_cache_load' );
00219 if ( !$this->BuildCache )
00220 {
00221 return true;
00222 }
00223 }
00224 eZDebugSetting::writeNotice( 'i18n-tstranslator',
00225 "Translation cache has expired. Will rebuild it from source.",
00226 "eZTSTranslator::loadTranslationFile" );
00227 $this->BuildCache = true;
00228 }
00229
00230 $status = false;
00231 foreach ( $roots as $root )
00232 {
00233 $path = eZDir::path( array( $root, $locale, $charset, $filename ) );
00234 if ( !file_exists( $path ) )
00235 {
00236 $path = eZDir::path( array( $root, $locale, $filename ) );
00237
00238 $ini =& eZINI::instance( "i18n.ini" );
00239 $fallbacks = $ini->variable( 'TranslationSettings', 'FallbackLanguages' );
00240
00241 if ( array_key_exists( $locale, $fallbacks ) and $fallbacks[$locale] )
00242 {
00243 $fallbackpath = eZDir::path( array( $root, $fallbacks[$locale], $filename ) );
00244 if ( !file_exists( $path ) and file_exists( $fallbackpath ) )
00245 $path = $fallbackpath;
00246 }
00247
00248 if ( !file_exists( $path ) )
00249 {
00250 eZDebug::writeError( "Could not load translation file: $path", "eZTSTranslator::loadTranslationFile" );
00251 continue;
00252 }
00253 }
00254
00255 eZDebug::accumulatorStart( 'tstranslator_load', 'tstranslator', 'TS load' );
00256 $fd = fopen( $path, "rb" );
00257 $trans_xml = fread( $fd, filesize( $path ) );
00258 fclose( $fd );
00259
00260 include_once( "lib/ezxml/classes/ezxml.php" );
00261 $xml = new eZXML();
00262
00263 $tree =& $xml->domTree( $trans_xml, array(), true );
00264
00265 if ( !$this->validateDOMTree( $tree ) )
00266 {
00267 eZDebug::writeWarning( "XML text for file $path did not validate", 'eZTSTranslator::loadTranslationFile' );
00268 continue;
00269 }
00270 $status = true;
00271
00272 $treeRoot = $tree->get_root();
00273 $children = $treeRoot->children();
00274 foreach( $children as $child )
00275 {
00276 if ( $child->type == 1 )
00277 {
00278 if ( $child->name() == "context" )
00279 {
00280 $this->handleContextNode( $child );
00281 }
00282 else
00283 eZDebug::writeError( "Unknown element name: " . $child->name(),
00284 "eZTSTranslator::loadTranslationFile" );
00285 }
00286 }
00287 eZDebug::accumulatorStop( 'tstranslator_load' );
00288 }
00289
00290
00291 if ( $this->UseCache == true && $this->BuildCache == true )
00292 {
00293 eZDebug::accumulatorStart( 'tstranslator_store_cache', 'tstranslator', 'TS store cache' );
00294 if ( eZTranslationCache::contextCache( 'cachecontexts' ) == null )
00295 {
00296 $contexts = array_keys( $this->CachedMessages );
00297 eZTranslationCache::setContextCache( 'cachecontexts',
00298 $contexts );
00299 eZTranslationCache::storeCache( 'cachecontexts' );
00300 $this->HasRestoredCache = $contexts;
00301 }
00302
00303 foreach ( $this->CachedMessages as $contextName => $context )
00304 {
00305 if ( eZTranslationCache::contextCache( $contextName ) == null )
00306 eZTranslationCache::setContextCache( $contextName, $context );
00307 eZTranslationCache::storeCache( $contextName );
00308 }
00309 $this->BuildCache = false;
00310 eZDebug::accumulatorStop( 'tstranslator_store_cache' );
00311 }
00312
00313 return $status;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322 function validateDOMTree( &$tree )
00323 {
00324 if ( !is_object( $tree ) )
00325 return false;
00326
00327 return true;
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 }
00376
00377 function handleContextNode( $context )
00378 {
00379 $contextName = null;
00380 $messages = array();
00381 $context_children = $context->children();
00382
00383 foreach( $context_children as $context_child )
00384 {
00385 if ( $context_child->type == 1 )
00386 {
00387 if ( $context_child->name() == "name" )
00388 {
00389 $name_el = $context_child->children();
00390 if ( count( $name_el ) > 0 )
00391 {
00392 $name_el = $name_el[0];
00393 $contextName = $name_el->content;
00394 }
00395 }
00396 break;
00397 }
00398 }
00399 if ( !$contextName )
00400 {
00401 eZDebug::writeError( "No context name found, skipping context",
00402 "eZTSTranslator::handleContextNode" );
00403 return false;
00404 }
00405 foreach( $context_children as $context_child )
00406 {
00407 if ( $context_child->type == 1 )
00408 {
00409 $childName = $context_child->name();
00410 if ( $childName == "message" )
00411 {
00412 $this->handleMessageNode( $contextName, $context_child );
00413 }
00414 else if ( $childName == "name" )
00415 {
00416
00417 }
00418 else
00419 {
00420 eZDebug::writeError( "Unknown element name: $childName",
00421 "eZTSTranslator::handleContextNode" );
00422 }
00423 }
00424 }
00425 if ( $contextName === null )
00426 {
00427 eZDebug::writeError( "No context name found, skipping context",
00428 "eZTSTranslator::handleContextNode" );
00429 return false;
00430 }
00431 if ( !isset( $this->CachedMessages[$contextName] ) )
00432 $this->CachedMessages[$contextName] = array();
00433
00434 return true;
00435 }
00436
00437 function handleMessageNode( $contextName, &$message )
00438 {
00439 $source = null;
00440 $translation = null;
00441 $comment = null;
00442 $message_children = $message->children();
00443 foreach( $message_children as $message_child )
00444 {
00445 if ( $message_child->type == 1 )
00446 {
00447 if ( $message_child->name() == "source" )
00448 {
00449 $source_el = $message_child->children();
00450 $source_el = $source_el[0];
00451 $source = $source_el->content;
00452 }
00453 else if ( $message_child->name() == "translation" )
00454 {
00455 $translation_el = $message_child->children();
00456 if ( count( $translation_el ) > 0 )
00457 {
00458 $translation_el = $translation_el[0];
00459 $translation = $translation_el->content;
00460 }
00461 }
00462 else if ( $message_child->name() == "comment" )
00463 {
00464 $comment_el = $message_child->children();
00465 $comment_el = $comment_el[0];
00466 $comment = $comment_el->content;
00467 }
00468 else if ( $message_child->name() == "location" )
00469 {
00470
00471 }
00472 else
00473 eZDebug::writeError( "Unknown element name: " . $message_child->name(),
00474 "eZTSTranslator::handleMessageNode" );
00475 }
00476 }
00477 if ( $source === null )
00478 {
00479 eZDebug::writeError( "No source name found, skipping message",
00480 "eZTSTranslator::handleMessageNode" );
00481 return false;
00482 }
00483 if ( $translation === null )
00484 {
00485
00486 return false;
00487 }
00488
00489 if ( get_class( $message ) == 'domelement' )
00490 {
00491 $codec =& eZTextCodec::instance( "utf8" );
00492 $source = $codec->convertString( $source );
00493 $translation = $codec->convertString( $translation );
00494 $comment = $codec->convertString( $comment );
00495 }
00496
00497 $this->insert( $contextName, $source, $translation, $comment );
00498 return true;
00499 }
00500
00501
00502
00503
00504 function findKey( $key )
00505 {
00506 $msg = null;
00507 if ( isset( $this->Messages[$key] ) )
00508 {
00509 $msg = $this->Messages[$key];
00510 }
00511 return $msg;
00512 }
00513
00514
00515
00516
00517 function findMessage( $context, $source, $comment = null )
00518 {
00519
00520 $man = eZTranslatorManager::instance();
00521 $key = $man->createKey( $context, $source, $comment );
00522
00523 if ( !isset( $this->Messages[$key] ) )
00524 {
00525
00526 $key = $man->createKey( $context, $source );
00527 }
00528
00529 return $this->findKey( $key );
00530 }
00531
00532
00533
00534
00535 function keyTranslate( $key )
00536 {
00537 $msg = $this->findKey( $key );
00538 if ( $msg !== null )
00539 return $msg["translation"];
00540 else
00541 {
00542 return null;
00543 }
00544 }
00545
00546
00547
00548
00549 function translate( $context, $source, $comment = null )
00550 {
00551 $msg = $this->findMessage( $context, $source, $comment );
00552 if ( $msg !== null )
00553 {
00554 return $msg["translation"];
00555 }
00556
00557 return null;
00558 }
00559
00560
00561
00562
00563
00564
00565
00566 function insert( $context, $source, $translation, $comment = null )
00567 {
00568
00569 if ( $context == "" )
00570 $context = "default";
00571 $man =& eZTranslatorManager::instance();
00572 $key = $man->createKey( $context, $source, $comment );
00573
00574
00575 $msg = $man->createMessage( $context, $source, $comment, $translation );
00576 $msg["key"] = $key;
00577 $this->Messages[$key] = $msg;
00578
00579 if ( $this->UseCache == true && $this->BuildCache == true )
00580 {
00581 if ( !isset( $this->CachedMessages[$context] ) )
00582 $this->CachedMessages[$context] = array();
00583 $this->CachedMessages[$context][$key] = $msg;
00584 }
00585 return $key;
00586 }
00587
00588
00589
00590
00591
00592
00593
00594 function remove( $context, $source, $message = null )
00595 {
00596 if ( $context == "" )
00597 $context = "default";
00598 $man =& eZTranslatorManager::instance();
00599 $key = $man->createKey( $context, $source, $message );
00600 if ( isset( $this->Messages[$key] ) )
00601 unset( $this->Messages[$key] );
00602 }
00603
00604
00605
00606
00607
00608 function removeKey( $key )
00609 {
00610 if ( isset( $this->Messages[$key] ) )
00611 unset( $this->Messages[$key] );
00612 }
00613
00614
00615
00616
00617
00618
00619 function fetchList( $localeList = array() )
00620 {
00621 include_once( 'lib/ezutils/classes/ezini.php' );
00622 $ini =& eZINI::instance();
00623
00624 $dir = $ini->variable( 'RegionalSettings', 'TranslationRepository' );
00625
00626 $fileInfoList = array();
00627 $translationList = array();
00628 $locale = '';
00629
00630 include_once( 'lib/ezfile/classes/ezfile.php' );
00631
00632 if ( count( $localeList ) == 0 )
00633 {
00634 $localeList = eZDir::findSubdirs( $dir );
00635 }
00636
00637 foreach( $localeList as $locale )
00638 {
00639 if ( $locale != 'untranslated' )
00640 {
00641 $translationFiles = eZDir::findSubitems( $dir . '/' . $locale, 'f' );
00642
00643 foreach( $translationFiles as $translationFile )
00644 {
00645 if ( eZFile::suffix( $translationFile ) == 'ts' )
00646 {
00647 $translationList[] = new eZTSTranslator( $locale, $translationFile );
00648 }
00649 }
00650 }
00651 }
00652
00653 return $translationList;
00654 }
00655
00656
00657
00658
00659 function resetGlobals()
00660 {
00661 unset( $GLOBALS["eZTSTranslationTables"] );
00662 }
00663
00664
00665
00666 var $Messages;
00667 var $File;
00668 var $UseCache;
00669 var $BuildCache;
00670 var $CachedMessages;
00671 }
00672
00673 ?>