Development

Changeset 6684

You must first sign up to be able to contribute.

Changeset 6684

Show
Ignore:
Timestamp:
12/23/07 12:34:28 (10 months ago)
Author:
fabien
Message:

refactored sfI18N class

  • added unit tests for sfI18N
  • removed sfContext dependency (now only depends on event dispatcher)
  • moved options to loadConfiguration() method
  • made culture parameter optional for all methods (takes the current culture by default)
  • fixed various small bugs
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/config/sfFactoryConfigHandler.class.php

    r6662 r6684  
    160160                     "    \$class = sfConfig::get('sf_factory_i18n', '%s');\n". 
    161161                     "%s". 
    162                      "    \$this->factories['i18n'] = new \$class(\$this, \$cache);\n". 
     162                     "    \$this->factories['i18n'] = new \$class(\$this->dispatcher, \$cache);\n". 
    163163                     $configuration. 
    164164                     "  }\n" 
  • branches/1.1/lib/i18n/sfI18N.class.php

    r6497 r6684  
    2020{ 
    2121  protected 
    22     $context       = null, 
     22    $dispatcher    = null, 
    2323    $cache         = null, 
    24     $culture       = null, 
     24    $options       = array(), 
     25    $culture       = 'en', 
    2526    $messageSource = null, 
    2627    $messageFormat = null; 
     
    3132   * @see initialize() 
    3233   */ 
    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); 
    3637  } 
    3738 
     
    3940   * Initializes this class. 
    4041   * 
    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')); 
    5067  } 
    5168 
     
    5673  { 
    5774    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    )); 
    5883  } 
    5984 
     
    6186   * Sets the message source. 
    6287   * 
    63    * @param mixed  An array of i18n directories if message source is XLIFF or gettext, null otherwise 
     88   * @param mixed  An array of i18n directories if message source is a sfMessageSource_File subclass, null otherwise 
    6489   * @param string The culture 
    6590   */ 
     
    101126  public function createMessageSource($dir = null) 
    102127  { 
    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; 
    111142  } 
    112143 
     
    123154    { 
    124155      $this->messageSource->setCulture($culture); 
     156      $this->messageFormat = null; 
    125157    } 
    126158  } 
     
    135167    if (!isset($this->messageSource)) 
    136168    { 
    137       $this->setMessageSource(sfLoader::getI18NGlobalDirs(), $this->context->getUser()->getCulture()); 
     169      $this->setMessageSource(sfLoader::getI18NGlobalDirs(), $this->culture); 
    138170    } 
    139171 
     
    152184      $this->messageFormat = new sfMessageFormat($this->getMessageSource(), sfConfig::get('sf_charset')); 
    153185 
    154       if (sfConfig::get('sf_debug') && sfConfig::get('sf_i18n_debug')
     186      if ($this->options['debug']
    155187      { 
    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'])); 
    157189      } 
    158190    } 
     
    179211   * 
    180212   * @param  string The ISO code 
    181    * @param  string The culture 
     213   * @param  string The culture for the translation 
    182214   * 
    183215   * @return string The country name 
    184216   */ 
    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); 
    188220    $countries = $c->getCountries(); 
    189221 
     
    213245   * @return integer The timestamp 
    214246   */ 
    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); 
    220252  } 
    221253 
     
    228260   * @return array   An array with the day, month and year 
    229261   */ 
    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); 
    235270    $dateFormat = $dateFormatInfo->getShortDatePattern(); 
    236271 
  • branches/1.1/test/unit/i18n/fixtures/messages.fr.xml

    r4576 r6684  
    1111        <target>une autre phrase en français</target> 
    1212      </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> 
    1317    </body> 
    1418  </file>