|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZTranslatorManager class 00004 // 00005 // Created on: <10-Jun-2002 11:16:48 amos> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! \file eztranslatormanager.php 00032 */ 00033 00034 /*! \defgroup eZTranslation Translation 00035 \ingroup eZI18N 00036 */ 00037 00038 00039 /*! 00040 \class eZTranslatorManager eztranslatormanager.php 00041 \ingroup eZTranslation 00042 \brief This provides internationalization support for text output 00043 00044 Each message consists of: 00045 - context - the context of the translation 00046 - source - the source string 00047 - comment - a variation of the context/source 00048 - key - the uniquely generated key taken from context, source and eventually comment 00049 00050 00051 */ 00052 00053 //include_once( "lib/ezi18n/classes/eztranslatorhandler.php" ); 00054 00055 class eZTranslatorManager 00056 { 00057 const DYNAMIC_TRANSLATIONS_ENABLED = 'eZTMDynamicTranslationsEnabled'; 00058 00059 /*! 00060 */ 00061 function eZTranslatorManager() 00062 { 00063 $this->Handlers = array(); 00064 } 00065 00066 /*! 00067 Tries to find the translation message that matches \a $key in all it's handlers 00068 and returns it. If no message could be found it either means that none of the 00069 handlers have a translation for the key or that some of the handlers are not key based, 00070 for instance realtime translators. 00071 In the latter case an extra call to findMessage() or translate() is required. 00072 00073 Use keyTranslate if you only want to translate a message. 00074 00075 \sa findMessage, keyTranslate 00076 */ 00077 function findKey( $key ) 00078 { 00079 $msg = null; 00080 for ( $i = 0; $i < count( $this->Handlers ) and $msg === null; ++$i ) 00081 { 00082 $handler = $this->Handlers[$i]; 00083 if ( $handler->isKeyBased() ) 00084 $msg = $handler->findKey( $key ); 00085 } 00086 return $msg; 00087 } 00088 00089 /*! 00090 Tries to find the translation message that matches \a $context, \a $source and 00091 \a $comment. If that fails it tries \a $context and \a $source only. 00092 The message is then returned or null if no translation message could be found/generated for it. 00093 00094 Use translate if you only want to translate a message. 00095 00096 \sa findKey, translate 00097 */ 00098 function findMessage( $context, $source, $comment = null ) 00099 { 00100 if ( !is_string( $context ) or $context == "" ) 00101 $context = "default"; 00102 $msg = null; 00103 for ( $i = 0; $i < count( $this->Handlers ) and $msg === null; ++$i ) 00104 { 00105 $handler = $this->Handlers[$i]; 00106 $msg = $handler->findMessage( $context, $source, $comment ); 00107 } 00108 return $msg; 00109 } 00110 00111 /*! 00112 \return the translation string for \a $key. 00113 00114 Note this returns the exact translation for the given key, use translate() 00115 instead if you want to have variable comment support. 00116 00117 \sa findKey, translate 00118 */ 00119 function keyTranslate( $key ) 00120 { 00121 $trans = null; 00122 for ( $i = 0; $i < count( $this->Handlers ) and $trans === null; ++$i ) 00123 { 00124 $handler = $this->Handlers[$i]; 00125 if ( $handler->isKeyBased() ) 00126 $trans = $handler->keyTranslate( $key ); 00127 } 00128 return $trans; 00129 } 00130 00131 /*! 00132 \return the translation string for \a $source and \a $context or null if the key does not exist. 00133 00134 \sa findMessage, findKey 00135 */ 00136 function translate( $context, $source, $comment = null ) 00137 { 00138 if ( !is_string( $context ) or $context == "" ) 00139 $context = "default"; 00140 $trans = null; 00141 for ( $i = 0; $i < count( $this->Handlers ) and $trans === null; ++$i ) 00142 { 00143 $handler = $this->Handlers[$i]; 00144 $trans = $handler->translate( $context, $source, $comment ); 00145 } 00146 return $trans; 00147 } 00148 00149 /*! 00150 \static 00151 \return the unique instance of the translator system. 00152 */ 00153 static function instance() 00154 { 00155 if ( empty( $GLOBALS['eZTranslatorManagerInstance'] ) ) 00156 { 00157 $GLOBALS['eZTranslatorManagerInstance'] = new eZTranslatorManager(); 00158 } 00159 return $GLOBALS['eZTranslatorManagerInstance']; 00160 } 00161 00162 /*! 00163 \static 00164 Registers the handler object \a $handler. 00165 */ 00166 static function registerHandler( $handler ) 00167 { 00168 $instance = eZTranslatorManager::instance(); 00169 $instance->Handlers[] = $handler; 00170 } 00171 00172 /*! 00173 \static 00174 Creates an md5 key based on the \a $context, \a $source and \a $comment and returns it. 00175 */ 00176 static function createKey( $context, $source, $comment = null ) 00177 { 00178 if ( $comment === null ) 00179 $comment = ""; 00180 return md5( "$context\n$source\n$comment" ); 00181 } 00182 00183 /*! 00184 \static 00185 Creates a message structure out of \a $context, \a $source and \a $comment 00186 and returns it. 00187 */ 00188 static function createMessage( $context, $source, $comment = null, $translation = null ) 00189 { 00190 $msg = array( "context" => $context, 00191 "source" => $source, 00192 "comment" => $comment, 00193 "translation" => $translation ); 00194 return $msg; 00195 } 00196 00197 /*! 00198 \static 00199 */ 00200 static function resetGlobals() 00201 { 00202 unset( $GLOBALS["eZTranslatorManagerInstance"] ); 00203 } 00204 00205 /*! 00206 \static 00207 */ 00208 static function resetTranslations() 00209 { 00210 //include_once( 'lib/ezi18n/classes/eztstranslator.php' ); 00211 eZTranslatorManager::resetGlobals(); 00212 eZTSTranslator::resetGlobals(); 00213 eZLocale::resetGlobals(); 00214 eZTranslationCache::resetGlobals(); 00215 } 00216 00217 /*! 00218 \static 00219 */ 00220 static function dynamicTranslationsEnabled() 00221 { 00222 return isset( $GLOBALS[self::DYNAMIC_TRANSLATIONS_ENABLED] ); 00223 } 00224 00225 /*! 00226 \static 00227 */ 00228 static function enableDynamicTranslations( $enable = true ) 00229 { 00230 if ( $enable ) 00231 { 00232 $GLOBALS[self::DYNAMIC_TRANSLATIONS_ENABLED] = true; 00233 } 00234 else 00235 { 00236 unset( $GLOBALS[self::DYNAMIC_TRANSLATIONS_ENABLED] ); 00237 } 00238 } 00239 00240 /*! 00241 \static 00242 */ 00243 static function setActiveTranslation( $locale, $permanently = true ) 00244 { 00245 if( !eZTranslatorManager::dynamicTranslationsEnabled() ) 00246 return; 00247 00248 if ( $permanently ) 00249 $siteINI = eZINI::instance( 'site.ini.append', 'settings/override', null, null, false, true ); 00250 else 00251 $siteINI = eZINI::instance(); 00252 00253 $siteINI->setVariable( 'RegionalSettings', 'Locale', $locale ); 00254 $siteINI->setVariable( 'RegionalSettings', 'TextTranslation', 'enabled' ); 00255 00256 if ( $permanently ) 00257 { 00258 $siteINI->save( 'site.ini.append', '.php', false, false ); 00259 eZINI::resetGlobals( "site.ini" ); 00260 } 00261 00262 eZTranslatorManager::resetTranslations(); 00263 } 00264 00265 00266 00267 /// \privatesection 00268 /// The array of handler objects 00269 public $Handlers; 00270 } 00271 00272 ?>