Changeset 6684
- Timestamp:
- 12/23/07 12:34:28 (10 months ago)
- Files:
-
- branches/1.1/lib/config/sfFactoryConfigHandler.class.php (modified) (1 diff)
- branches/1.1/lib/i18n/sfI18N.class.php (modified) (12 diffs)
- branches/1.1/test/unit/i18n/fixtures/messages.fr.xml (modified) (1 diff)
- branches/1.1/test/unit/i18n/fixtures/messages.fr_BE.xml (added)
- branches/1.1/test/unit/i18n/sfI18NTest.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/config/sfFactoryConfigHandler.class.php
r6662 r6684 160 160 " \$class = sfConfig::get('sf_factory_i18n', '%s');\n". 161 161 "%s". 162 " \$this->factories['i18n'] = new \$class(\$this , \$cache);\n".162 " \$this->factories['i18n'] = new \$class(\$this->dispatcher, \$cache);\n". 163 163 $configuration. 164 164 " }\n" branches/1.1/lib/i18n/sfI18N.class.php
r6497 r6684 20 20 { 21 21 protected 22 $ context= null,22 $dispatcher = null, 23 23 $cache = null, 24 $culture = null, 24 $options = array(), 25 $culture = 'en', 25 26 $messageSource = null, 26 27 $messageFormat = null; … … 31 32 * @see initialize() 32 33 */ 33 public function __construct( $context, sfCache $cache = null)34 { 35 $this->initialize($ context, $cache);34 public function __construct(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array()) 35 { 36 $this->initialize($dispatcher, $cache, $options); 36 37 } 37 38 … … 39 40 * Initializes this class. 40 41 * 41 * @param sfContext A sfContext implementation instance 42 */ 43 public function initialize($context, sfCache $cache = null) 44 { 45 $this->context = $context; 46 $this->cache = $cache; 47 48 $context->getEventDispatcher()->connect('user.change_culture', array($this, 'listenToChangeCultureEvent')); 49 $context->getEventDispatcher()->connect('controller.change_action', array($this, 'listenToChangeActionEvent')); 42 * @param sfEventDispatcher A sfEventDispatcher implementation instance 43 * @param sfCache A sfCache instance 44 * @param array An array of options 45 */ 46 public function initialize(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array()) 47 { 48 $this->dispatcher = $dispatcher; 49 $this->cache = $cache; 50 51 if (isset($options['culture'])) 52 { 53 $this->culture = $options['culture']; 54 unset($options['culture']); 55 } 56 57 $this->options = array_merge(array( 58 'source' => 'XLIFF', 59 'debug' => false, 60 'database' => 'default', 61 'untranslated_prefix' => '[T]', 62 'untranslated_suffix' => '[/T]', 63 ), $options); 64 65 $dispatcher->connect('user.change_culture', array($this, 'listenToChangeCultureEvent')); 66 $dispatcher->connect('controller.change_action', array($this, 'listenToChangeActionEvent')); 50 67 } 51 68 … … 56 73 { 57 74 include(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/i18n.yml')); 75 76 $this->options = array_merge($this->options, array( 77 'source' => sfConfig::get('sf_i18n_source'), 78 'database' => sfConfig::get('sf_i18n_database'), 79 'debug' => sfConfig::get('sf_debug') && sfConfig::get('sf_i18n_debug'), 80 'untranslated_prefix' => sfConfig::get('sf_i18n_untranslated_prefix'), 81 'untranslated_suffix' => sfConfig::get('sf_i18n_untranslated_suffix'), 82 )); 58 83 } 59 84 … … 61 86 * Sets the message source. 62 87 * 63 * @param mixed An array of i18n directories if message source is XLIFF or gettext, null otherwise88 * @param mixed An array of i18n directories if message source is a sfMessageSource_File subclass, null otherwise 64 89 * @param string The culture 65 90 */ … … 101 126 public function createMessageSource($dir = null) 102 127 { 103 if (in_array(sfConfig::get('sf_i18n_source'), array('Creole', 'MySQL', 'SQLite'))) 104 { 105 return sfMessageSource::factory(sfConfig::get('sf_i18n_source'), sfConfig::get('sf_i18n_database', 'default')); 106 } 107 else 108 { 109 return sfMessageSource::factory(sfConfig::get('sf_i18n_source'), $dir); 110 } 128 $class = 'sfMessageSource_'.$this->options['source']; 129 $source = class_exists($class) && is_subclass_of($class, 'sfMessageSource_Database') ? $this->options['database'] : $dir; 130 131 return sfMessageSource::factory($this->options['source'], $source); 132 } 133 134 /** 135 * Gets the current culture for i18n format objects. 136 * 137 * @return string The culture 138 */ 139 public function getCulture() 140 { 141 return $this->culture; 111 142 } 112 143 … … 123 154 { 124 155 $this->messageSource->setCulture($culture); 156 $this->messageFormat = null; 125 157 } 126 158 } … … 135 167 if (!isset($this->messageSource)) 136 168 { 137 $this->setMessageSource(sfLoader::getI18NGlobalDirs(), $this->c ontext->getUser()->getCulture());169 $this->setMessageSource(sfLoader::getI18NGlobalDirs(), $this->culture); 138 170 } 139 171 … … 152 184 $this->messageFormat = new sfMessageFormat($this->getMessageSource(), sfConfig::get('sf_charset')); 153 185 154 if ( sfConfig::get('sf_debug') && sfConfig::get('sf_i18n_debug'))186 if ($this->options['debug']) 155 187 { 156 $this->messageFormat->setUntranslatedPS(array( sfConfig::get('sf_i18n_untranslated_prefix'), sfConfig::get('sf_i18n_untranslated_suffix')));188 $this->messageFormat->setUntranslatedPS(array($this->options['untranslated_prefix'], $this->options['untranslated_suffix'])); 157 189 } 158 190 } … … 179 211 * 180 212 * @param string The ISO code 181 * @param string The culture 213 * @param string The culture for the translation 182 214 * 183 215 * @return string The country name 184 216 */ 185 public function getCountry($iso, $culture )186 { 187 $c = new sfCultureInfo( $culture);217 public function getCountry($iso, $culture = null) 218 { 219 $c = new sfCultureInfo(is_null($culture) ? $this->culture : $culture); 188 220 $countries = $c->getCountries(); 189 221 … … 213 245 * @return integer The timestamp 214 246 */ 215 public function getTimestampForCulture($date, $culture )216 { 217 list($d, $m, $y) = $this->getDateForCulture($date, $culture);218 219 return mktime(0, 0, 0, $m, $d, $y);247 public function getTimestampForCulture($date, $culture = null) 248 { 249 list($d, $m, $y) = $this->getDateForCulture($date, is_null($culture) ? $this->culture : $culture); 250 251 return is_null($d) ? null : mktime(0, 0, 0, $m, $d, $y); 220 252 } 221 253 … … 228 260 * @return array An array with the day, month and year 229 261 */ 230 public function getDateForCulture($date, $culture) 231 { 232 if (!$date) return 0; 233 234 $dateFormatInfo = @sfDateTimeFormatInfo::getInstance($culture); 262 public function getDateForCulture($date, $culture = null) 263 { 264 if (!$date) 265 { 266 return null; 267 } 268 269 $dateFormatInfo = @sfDateTimeFormatInfo::getInstance(is_null($culture) ? $this->culture : $culture); 235 270 $dateFormat = $dateFormatInfo->getShortDatePattern(); 236 271 branches/1.1/test/unit/i18n/fixtures/messages.fr.xml
r4576 r6684 11 11 <target>une autre phrase en français</target> 12 12 </trans-unit> 13 <trans-unit id="2"> 14 <source>Current timestamp is %timestamp%</source> 15 <target>Le timestamp courant est %timestamp%</target> 16 </trans-unit> 13 17 </body> 14 18 </file>