[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 /** 12 * 13 * @package symfony 14 * @subpackage i18n 15 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 16 * @version SVN: $Id: sfI18N.class.php 2769 2006-11-20 14:47:55Z fabien $ 17 */ 18 class sfI18N 19 { 20 protected 21 $context = null, 22 $globalMessageSource = null, 23 $messageSource = null, 24 $messageFormat = null; 25 26 static protected 27 $instance = null; 28 29 static public function getInstance() 30 { 31 if (!isset(self::$instance)) 32 { 33 $class = __CLASS__; 34 self::$instance = new $class(); 35 } 36 37 return self::$instance; 38 } 39 40 public function initialize($context) 41 { 42 $this->context = $context; 43 44 $this->globalMessageSource = $this->createMessageSource(sfConfig::get('sf_app_i18n_dir')); 45 46 $this->globalMessageFormat = $this->createMessageFormat($this->globalMessageSource); 47 } 48 49 public function setMessageSourceDir($dir, $culture) 50 { 51 $this->messageSource = $this->createMessageSource($dir); 52 $this->messageSource->setCulture($culture); 53 54 $this->messageFormat = $this->createMessageFormat($this->messageSource); 55 } 56 57 public function createMessageSource($dir) 58 { 59 if (in_array(sfConfig::get('sf_i18n_source'), array('Creole', 'MySQL', 'SQLite'))) 60 { 61 $messageSource = sfMessageSource::factory(sfConfig::get('sf_i18n_source'), sfConfig::get('sf_i18n_database', 'default')); 62 } 63 else 64 { 65 $messageSource = sfMessageSource::factory(sfConfig::get('sf_i18n_source'), $dir); 66 } 67 68 if (sfConfig::get('sf_i18n_cache')) 69 { 70 $subdir = str_replace(str_replace('/', DIRECTORY_SEPARATOR, sfConfig::get('sf_root_dir')), '', $dir); 71 $cacheDir = str_replace('/', DIRECTORY_SEPARATOR, sfConfig::get('sf_i18n_cache_dir').$subdir); 72 73 $cache = new sfMessageCache(); 74 $cache->initialize(array( 75 'cacheDir' => $cacheDir, 76 'lifeTime' => 86400, 77 )); 78 79 $messageSource->setCache($cache); 80 } 81 82 return $messageSource; 83 } 84 85 public function createMessageFormat($source) 86 { 87 $messageFormat = new sfMessageFormat($source, sfConfig::get('sf_charset')); 88 89 if (sfConfig::get('sf_debug') && sfConfig::get('sf_i18n_debug')) 90 { 91 $messageFormat->setUntranslatedPS(array(sfConfig::get('sf_i18n_untranslated_prefix'), sfConfig::get('sf_i18n_untranslated_suffix'))); 92 } 93 94 return $messageFormat; 95 } 96 97 public function setCulture($culture) 98 { 99 if ($this->messageSource) 100 { 101 $this->messageSource->setCulture($culture); 102 } 103 104 $this->globalMessageSource->setCulture($culture); 105 } 106 107 public function getMessageSource() 108 { 109 return $this->messageSource; 110 } 111 112 public function getGlobalMessageSource() 113 { 114 return $this->globalMessageSource; 115 } 116 117 public function getMessageFormat() 118 { 119 return $this->messageFormat; 120 } 121 122 public function getGlobalMessageFormat() 123 { 124 return $this->globalMessageFormat; 125 } 126 127 public function __($string, $args = array(), $catalogue = 'messages') 128 { 129 $retval = $this->messageFormat->formatExists($string, $args, $catalogue); 130 131 if (!$retval) 132 { 133 $retval = $this->globalMessageFormat->format($string, $args, $catalogue); 134 } 135 136 return $retval; 137 } 138 139 public static function getCountry($iso, $culture) 140 { 141 $c = new sfCultureInfo($culture); 142 $countries = $c->getCountries(); 143 144 return (array_key_exists($iso, $countries)) ? $countries[$iso] : ''; 145 } 146 147 public static function getNativeName($culture) 148 { 149 $cult = new sfCultureInfo($culture); 150 return $cult->getNativeName(); 151 } 152 153 // Return timestamp from a date formatted with a given culture 154 public static function getTimestampForCulture($date, $culture) 155 { 156 list($d, $m, $y) = self::getDateForCulture($date, $culture); 157 return mktime(0, 0, 0, $m, $d, $y); 158 } 159 160 // Return a d, m and y from a date formatted with a given culture 161 public static function getDateForCulture($date, $culture) 162 { 163 if (!$date) return 0; 164 165 $dateFormatInfo = @sfDateTimeFormatInfo::getInstance($culture); 166 $dateFormat = $dateFormatInfo->getShortDatePattern(); 167 168 // We construct the regexp based on date format 169 $dateRegexp = preg_replace('/[dmy]+/i', '(\d+)', $dateFormat); 170 171 // We parse date format to see where things are (m, d, y) 172 $a = array( 173 'd' => strpos($dateFormat, 'd'), 174 'm' => strpos($dateFormat, 'M'), 175 'y' => strpos($dateFormat, 'y'), 176 ); 177 $tmp = array_flip($a); 178 ksort($tmp); 179 $i = 0; 180 $c = array(); 181 foreach ($tmp as $value) $c[++$i] = $value; 182 $datePositions = array_flip($c); 183 184 // We find all elements 185 if (preg_match("~$dateRegexp~", $date, $matches)) 186 { 187 // We get matching timestamp 188 return array($matches[$datePositions['d']], $matches[$datePositions['m']], $matches[$datePositions['y']]); 189 } 190 else 191 { 192 return null; 193 } 194 } 195 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |