| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 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 |
$this->globalMessageFormat = $this->createMessageFormat($this->globalMessageSource); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
public function setMessageSourceDir($dir, $culture) |
|---|
| 49 |
{ |
|---|
| 50 |
$this->messageSource = $this->createMessageSource($dir); |
|---|
| 51 |
$this->messageSource->setCulture($culture); |
|---|
| 52 |
$this->messageFormat = $this->createMessageFormat($this->messageSource); |
|---|
| 53 |
|
|---|
| 54 |
$this->globalMessageSource->setCulture($culture); |
|---|
| 55 |
$this->globalMessageFormat = $this->createMessageFormat($this->globalMessageSource); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
public function createMessageSource($dir) |
|---|
| 59 |
{ |
|---|
| 60 |
if (in_array(sfConfig::get('sf_i18n_source'), array('Creole', 'MySQL', 'SQLite'))) |
|---|
| 61 |
{ |
|---|
| 62 |
$messageSource = sfMessageSource::factory(sfConfig::get('sf_i18n_source'), sfConfig::get('sf_i18n_database', 'default')); |
|---|
| 63 |
} |
|---|
| 64 |
else |
|---|
| 65 |
{ |
|---|
| 66 |
$messageSource = sfMessageSource::factory(sfConfig::get('sf_i18n_source'), $dir); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
if (sfConfig::get('sf_i18n_cache')) |
|---|
| 70 |
{ |
|---|
| 71 |
$subdir = str_replace(str_replace('/', DIRECTORY_SEPARATOR, sfConfig::get('sf_root_dir')), '', $dir); |
|---|
| 72 |
$cacheDir = str_replace('/', DIRECTORY_SEPARATOR, sfConfig::get('sf_i18n_cache_dir').$subdir); |
|---|
| 73 |
|
|---|
| 74 |
$cache = new sfMessageCache(); |
|---|
| 75 |
$cache->initialize(array( |
|---|
| 76 |
'cacheDir' => $cacheDir, |
|---|
| 77 |
'lifeTime' => 86400, |
|---|
| 78 |
)); |
|---|
| 79 |
|
|---|
| 80 |
$messageSource->setCache($cache); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
return $messageSource; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
public function createMessageFormat($source) |
|---|
| 87 |
{ |
|---|
| 88 |
$messageFormat = new sfMessageFormat($source, sfConfig::get('sf_charset')); |
|---|
| 89 |
|
|---|
| 90 |
if (sfConfig::get('sf_debug') && sfConfig::get('sf_i18n_debug')) |
|---|
| 91 |
{ |
|---|
| 92 |
$messageFormat->setUntranslatedPS(array(sfConfig::get('sf_i18n_untranslated_prefix'), sfConfig::get('sf_i18n_untranslated_suffix'))); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
return $messageFormat; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
public function setCulture($culture) |
|---|
| 99 |
{ |
|---|
| 100 |
if ($this->messageSource) |
|---|
| 101 |
{ |
|---|
| 102 |
$this->messageSource->setCulture($culture); |
|---|
| 103 |
$this->messageFormat = $this->createMessageFormat($this->messageSource); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
$this->globalMessageSource->setCulture($culture); |
|---|
| 107 |
$this->globalMessageFormat = $this->createMessageFormat($this->globalMessageSource); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
public function getMessageSource() |
|---|
| 111 |
{ |
|---|
| 112 |
return $this->messageSource; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
public function getGlobalMessageSource() |
|---|
| 116 |
{ |
|---|
| 117 |
return $this->globalMessageSource; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
public function getMessageFormat() |
|---|
| 121 |
{ |
|---|
| 122 |
return $this->messageFormat; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
public function getGlobalMessageFormat() |
|---|
| 126 |
{ |
|---|
| 127 |
return $this->globalMessageFormat; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
public function __($string, $args = array(), $catalogue = 'messages') |
|---|
| 131 |
{ |
|---|
| 132 |
$retval = $this->messageFormat->formatExists($string, $args, $catalogue); |
|---|
| 133 |
|
|---|
| 134 |
if (!$retval) |
|---|
| 135 |
{ |
|---|
| 136 |
$retval = $this->globalMessageFormat->format($string, $args, $catalogue); |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
return $retval; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
public static function getCountry($iso, $culture) |
|---|
| 143 |
{ |
|---|
| 144 |
$c = new sfCultureInfo($culture); |
|---|
| 145 |
$countries = $c->getCountries(); |
|---|
| 146 |
|
|---|
| 147 |
return (array_key_exists($iso, $countries)) ? $countries[$iso] : ''; |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
public static function getNativeName($culture) |
|---|
| 151 |
{ |
|---|
| 152 |
$cult = new sfCultureInfo($culture); |
|---|
| 153 |
return $cult->getNativeName(); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
public static function getTimestampForCulture($date, $culture) |
|---|
| 158 |
{ |
|---|
| 159 |
list($d, $m, $y) = self::getDateForCulture($date, $culture); |
|---|
| 160 |
list($hour, $minute) = self::getTimeForCulture($date, $culture); |
|---|
| 161 |
|
|---|
| 162 |
return mktime($hour, $minute, 0, $m, $d, $y); |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
public static function getDateForCulture($date, $culture) |
|---|
| 167 |
{ |
|---|
| 168 |
if (!$date) return 0; |
|---|
| 169 |
|
|---|
| 170 |
$dateFormatInfo = @sfDateTimeFormatInfo::getInstance($culture); |
|---|
| 171 |
$dateFormat = $dateFormatInfo->getShortDatePattern(); |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
$dateRegexp = preg_replace('/[dmy]+/i', '(\d+)', $dateFormat); |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
$a = array( |
|---|
| 178 |
'd' => strpos($dateFormat, 'd'), |
|---|
| 179 |
'm' => strpos($dateFormat, 'M'), |
|---|
| 180 |
'y' => strpos($dateFormat, 'y'), |
|---|
| 181 |
); |
|---|
| 182 |
$tmp = array_flip($a); |
|---|
| 183 |
ksort($tmp); |
|---|
| 184 |
$i = 0; |
|---|
| 185 |
$c = array(); |
|---|
| 186 |
foreach ($tmp as $value) $c[++$i] = $value; |
|---|
| 187 |
$datePositions = array_flip($c); |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
if (preg_match("~$dateRegexp~", $date, $matches)) |
|---|
| 191 |
{ |
|---|
| 192 |
|
|---|
| 193 |
return array($matches[$datePositions['d']], $matches[$datePositions['m']], $matches[$datePositions['y']]); |
|---|
| 194 |
} |
|---|
| 195 |
else |
|---|
| 196 |
{ |
|---|
| 197 |
return null; |
|---|
| 198 |
} |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
* Returns the hour, minute from a date formatted with a given culture. |
|---|
| 203 |
* |
|---|
| 204 |
* @param string $date The formatted date as string |
|---|
| 205 |
* @param string $culture The culture |
|---|
| 206 |
* |
|---|
| 207 |
* @return array An array with the hour and minute |
|---|
| 208 |
*/ |
|---|
| 209 |
protected static function getTimeForCulture($time, $culture) |
|---|
| 210 |
{ |
|---|
| 211 |
if (!$time) return 0; |
|---|
| 212 |
|
|---|
| 213 |
$culture = is_null($culture) ? $this->culture : $culture; |
|---|
| 214 |
|
|---|
| 215 |
$timeFormatInfo = @sfDateTimeFormatInfo::getInstance($culture); |
|---|
| 216 |
$timeFormat = $timeFormatInfo->getShortTimePattern(); |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
$timeRegexp = preg_replace(array('/[^hm:]+/i', '/[hm]+/i'), array('', '(\d+)'), $timeFormat); |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
$a = array( |
|---|
| 223 |
'h' => strpos($timeFormat, 'H') !== false ? strpos($timeFormat, 'H') : strpos($timeFormat, 'h'), |
|---|
| 224 |
'm' => strpos($timeFormat, 'm') |
|---|
| 225 |
); |
|---|
| 226 |
$tmp = array_flip($a); |
|---|
| 227 |
ksort($tmp); |
|---|
| 228 |
$i = 0; |
|---|
| 229 |
$c = array(); |
|---|
| 230 |
|
|---|
| 231 |
foreach ($tmp as $value) |
|---|
| 232 |
{ |
|---|
| 233 |
$c[++$i] = $value; |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
$timePositions = array_flip($c); |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
if (preg_match("~$timeRegexp~", $time, $matches)) |
|---|
| 240 |
{ |
|---|
| 241 |
|
|---|
| 242 |
return array($matches[$timePositions['h']], $matches[$timePositions['m']]); |
|---|
| 243 |
} |
|---|
| 244 |
else |
|---|
| 245 |
{ |
|---|
| 246 |
return null; |
|---|
| 247 |
} |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
} |
|---|
| 251 |
|
|---|