[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 4 lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/locale/locale.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" ); 7 lt_include( PLOG_CLASS_PATH.'class/dao/daocacheconstants.properties.php' ); 8 9 define("DEFAULT_LOCALE", "en_UK"); 10 define( "REGEXP_VALID_LOCALE", "/.*locale_([a-z]{2}_[A-Z]{2}_?([a-zA-Z0-9\-]*)+)\.php$/" ); 11 12 /** 13 * \ingroup Locale 14 * 15 * Class that supports methods such as getAvailableLocales, addLocale 16 * removeLocale and so on, basically to deal with locale files. 17 * 18 * It also provides a singleton-like method called Locales::getLocale to load and cache locale 19 * files from disk, so that we don't have to fetch the same file as many times 20 * as we ask for it but else, we keep a cached copy of it for later use. It is advised to 21 * use this method instead of creating a new Locale object every time we need a Locale. 22 * 23 * @see Locales::getLocale 24 * @see Locale 25 */ 26 class Locales 27 { 28 29 function Locales() 30 { 31 32 } 33 34 /** 35 * @static 36 * Static method that offers some kind of locale factory. Since the Locale object 37 * better not use a Singleton (otherwise we couldn't use more than one locale file 38 * at a time) this function has been included here to provide a system similar to 39 * a singleton: we keep an static array inside the function, that contains all the 40 * locale files that have been loaded so far. Whenever somebody requests a locale 41 * to be fetched from disk, we will first check that we have not loaded it before. If 42 * we have, then we only have to return the same object we were keeping. 43 * If the locale wasn't there, we will then load it from disk and store/cache the 44 * resulting object for future use. 45 * It is recommended to use this method over creating new Locale objects every time 46 * we need one. 47 * 48 * @param localeCode The code (eg. en_UK, es_ES) of the locale we want to get. 49 * @return Returns a Locale object corresponding to the requested locale. 50 * @see Locale 51 */ 52 function &getBlogLocale( $localeCode = null ) 53 { 54 lt_include( PLOG_CLASS_PATH."class/locale/bloglocale.class.php" ); 55 56 // array to keep track of the locales that we have already loaded, so that 57 // we don't have to fetch them from disk 58 static $loadedLocales; 59 60 // if there is no locale parameter, we use the default one 61 if( $localeCode == null ) { 62 $config =& Config::getConfig(); 63 $localeCode = $config->getValue( "default_locale" ); 64 } 65 66 // check if we have already loaded that locale or else, load it from 67 // disk and keep it for later, just in case anybody asks again 68 if( isset($loadedLocales[$localeCode]) ) { 69 $locale = $loadedLocales[$localeCode]; 70 } 71 else { 72 lt_include( PLOG_CLASS_PATH . "class/cache/cachemanager.class.php" ); 73 $cache =& CacheManager::getCache(); 74 $locale = $cache->getData( $localeCode, CACHE_BLOG_LOCALES ); 75 if ( !$locale ) { 76 $locale = new BlogLocale( $localeCode ); 77 $cache->setData( $localeCode, CACHE_BLOG_LOCALES, $locale ); 78 } 79 80 Locales::_loadPluginLocales( $locale ); 81 } 82 83 $loadedLocales[$localeCode] = $locale; 84 85 return $locale; 86 } 87 88 function _loadPluginLocales( &$locale ) 89 { 90 $localeCode = $locale->getLocaleCode(); 91 92 $pm =& PluginManager::getPluginManager(); 93 foreach( $pm->_pluginList as $pluginId ) { 94 if( $pm->pluginHasLocale( $pluginId, $localeCode )) { 95 // if the plugin provides the locale that we need, continue 96 $pluginLocale = Locales::getPluginLocale( $pluginId, $localeCode ); 97 } 98 else { 99 // if not, try to load en_UK by default 100 if( $pm->pluginHasLocale( $pluginId, "en_UK" )) { 101 $pluginLocale = Locales::getPluginLocale( $pluginId, "en_UK" ); 102 } 103 // if not en_UK locale available, forget about it... 104 } 105 106 // merge the plugin locale with the big locale 107 if ( isset( $pluginLocale ) ) { 108 $locale->mergeLocale( $pluginLocale ); 109 } 110 } 111 112 return( true ); 113 } 114 115 /** 116 * @static 117 * Static method that offers some kind of locale factory. Since the Locale object 118 * better not use a Singleton (otherwise we couldn't use more than one locale file 119 * at a time) this function has been included here to provide a system similar to 120 * a singleton: we keep an static array inside the function, that contains all the 121 * locale files that have been loaded so far. Whenever somebody requests a locale 122 * to be fetched from disk, we will first check that we have not loaded it before. If 123 * we have, then we only have to return the same object we were keeping. 124 * If the locale wasn't there, we will then load it from disk and store/cache the 125 * resulting object for future use. 126 * It is recommended to use this method over creating new Locale objects every time 127 * we need one. 128 * 129 * @param localeCode The code (eg. en_UK, es_ES) of the locale we want to get. 130 * @return Returns a Locale object corresponding to the requested locale. 131 * @see Locale 132 */ 133 function &getLocale( $localeCode = null ) 134 { 135 // array to keep track of the locales that we have already loaded, so that 136 // we don't have to fetch them from disk 137 static $loadedLocales; 138 139 // if there is no locale parameter, we use the default one 140 if( $localeCode == null ) { 141 $config =& Config::getConfig(); 142 $localeCode = $config->getValue( "default_locale" ); 143 } 144 145 // check if we have already loaded that locale or else, load it from 146 // disk and keep it for later, just in case anybody asks again 147 if( isset($loadedLocales[$localeCode]) ) { 148 $locale = $loadedLocales[$localeCode]; 149 } 150 else { 151 lt_include( PLOG_CLASS_PATH . "class/cache/cachemanager.class.php" ); 152 $cache =& CacheManager::getCache(); 153 $locale = $cache->getData( $localeCode, CACHE_LOCALES ); 154 if ( !$locale ) { 155 $locale = new Locale( $localeCode ); 156 $cache->setData( $localeCode, CACHE_LOCALES, $locale ); 157 } 158 159 Locales::_loadPluginLocales( $locale ); 160 } 161 162 $loadedLocales[$localeCode] = $locale; 163 164 return $locale; 165 } 166 167 /** 168 * loads the locale file provided by a plugin. First it will try to load the locale specified in 169 * the $pluginId parameter and if not available, it will load the current default locale. 170 * 171 * @param localeCode The locale code we would like to load 172 * @return A PluginLocale object with the translated strings loaded from the plugin's own folder 173 */ 174 function getPluginLocale( $pluginId, $localeCode = null ) 175 { 176 global $_plugins_loadedLocales; 177 178 if( $localeCode == null ) { 179 $config =& Config::getConfig(); 180 $localeCode = $config->getValue( "default_locale" ); 181 } 182 183 //$pluginLocaleKey = "plugin_".$pluginId."_".$localeCode; 184 $pluginLocaleKey = $pluginId; 185 186 // check if we have already loaded that locale or else, load it from 187 // disk and keep it for later, just in case anybody asks again 188 if( isset($_plugins_loadedLocales[$pluginLocaleKey][$localeCode] )) { 189 $locale = $_plugins_loadedLocales[$pluginLocaleKey][$localeCode]; 190 } 191 else { 192 lt_include( PLOG_CLASS_PATH."class/locale/pluginlocale.class.php" ); 193 194 $locale = new PluginLocale( $pluginId, $localeCode ); 195 $_plugins_loadedLocales[$pluginLocaleKey][$localeCode] = $locale; 196 } 197 198 return $locale; 199 } 200 201 /** 202 * Returns an array with the codes of the locale files available. 203 * 204 * @return An array containing all the locale codes available in the system. 205 * @static 206 */ 207 function getAvailableLocales() 208 { 209 $config =& Config::getConfig(); 210 211 $locales = $config->getValue( "locales" ); 212 213 // in order to prevent some ugly error messages 214 if( !is_array( $locales )) 215 $locales = Array(); 216 217 return( $locales ); 218 } 219 220 /** 221 * Returns default locale code 222 * 223 * @return default locale code 224 */ 225 226 function getDefaultLocale() 227 { 228 return DEFAULT_LOCALE; 229 } 230 231 /** 232 * Returns true if the given locale code is a valid one (i.e. if it is amongst 233 * the available ones 234 * 235 * @param localeCode The code of the locale we'd like to check 236 * @return Returns 'true' if the locale file is already available in the system 237 * or false otherwise. 238 * @static 239 */ 240 function isValidLocale( $localeCode ) 241 { 242 $availableLocales = Locales::getAvailableLocales(); 243 244 return in_array( $localeCode, $availableLocales ); 245 } 246 247 /** 248 * returns whether a file has the corect name format 249 * 250 * @param fileName 251 * @return true if format is correct or false otherwise 252 */ 253 function isValidLocaleFileName( $fileName ) 254 { 255 return( preg_match( REGEXP_VALID_LOCALE, $fileName )); 256 } 257 258 /** 259 * Returns an array with all the locales available in the system. 260 * 261 * This is quite memory and disk-intensive since we are loading a lot of lines 262 * from possibly a lot of files!!! 263 * 264 * @return Returns an array of Locale objects, which represent *all* the locales 265 * that have been installed in this system. 266 * @static 267 */ 268 function getLocales() 269 { 270 $localeCodes = Locales::getAvailableLocales(); 271 272 $locales = Array(); 273 foreach( $localeCodes as $code ) { 274 array_push( $locales, Locales::getLocale( $code )); 275 } 276 277 return $locales; 278 } 279 280 /** 281 * Given a locale code, returns the path to the file that contains it. 282 * 283 * @param localeCode The code representing the locale 284 * @return A string containing the path (may be absolute or relative, it depends) 285 * to the file containing the given locale. It does *not* check if the file exists 286 * or not, simply returns its path. 287 */ 288 function getLocaleFilename( $localeCode ) 289 { 290 return (Locale::getLocaleFolder()."/locale_".$localeCode.".php"); 291 } 292 293 /** 294 * Removes a locale from the system. First the file containing it is deleted 295 * and then we also remove its entry from the configuration. 296 * 297 * @param localeCode The code of the locale that we would like to delete. 298 * @return Returns true if the locale was removed succesfully or false otherwise. 299 */ 300 function removeLocale( $localeCode ) 301 { 302 $config =& Config::getConfig(); 303 304 // if we don't have permissions on the folder where the locale files 305 // are stored, we don't even have to bother about it... 306 if(!File::isWritable(Locale::getLocaleFolder())) 307 return false; 308 309 // does the locale really exist? 310 if( !$this->isValidLocale( $localeCode )) 311 return false; 312 313 $fileName = $this->getLocaleFilename( $localeCode ); 314 315 if( File::exists( $fileName )) { 316 if( !unlink( $fileName )) 317 return false; 318 } 319 320 $availableLocales = $config->getValue( "locales" ); 321 322 $newLocaleList = Array(); 323 foreach( $availableLocales as $locale ) { 324 if( $locale != $localeCode ) 325 array_push( $newLocaleList, $locale ); 326 } 327 328 $config->saveValue( "locales", $newLocaleList ); 329 330 return true; 331 } 332 333 /** 334 * Adds a new locale to the list of available/supported locale. It checks to make 335 * sure that the file exists and that has proper access permissions. 336 * 337 * @param localeCode the code of the locale we'd like to add. 338 * @return Returns true if the file was successfully added or false otherwise. 339 */ 340 function addLocale( $localeCode ) 341 { 342 $config =& Config::getConfig(); 343 344 // if the locale is already there, there is no need to add it, 345 // since the user is *maybe* just updating the file. 346 if( $this->isValidLocale( $localeCode )) 347 return true; 348 349 $fileName = $this->getLocaleFilename( $localeCode ); 350 351 // make sure that the file exists and can be read and 352 // give up if not so 353 if( !File::isReadable( $fileName )) 354 return false; 355 356 // otherwise, we can happily add it to the list of supported locales 357 $availableLocales = $this->getAvailableLocales(); 358 array_push( $availableLocales, $localeCode ); 359 $config->saveValue( "locales", $availableLocales ); 360 361 return true; 362 } 363 } 364 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |