Changeset 1698

Show
Ignore:
Timestamp:
02/13/07 05:36:57 (2 years ago)
Author:
david
Message:

new features and changed methods for translation manager and locales, closes #426

Location:
branches/0.11
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • branches/0.11/samples/app/lib/routing/AgaviSampleAppLanguageRoutingCallback.class.php

    r1604 r1698  
    2121    // first, let's check if the locale is allowed 
    2222    try { 
    23       $set = $this->context->getTranslationManager()->getClosestMatchingLocale($parameters['locale']); 
     23      $set = $this->context->getTranslationManager()->getLocaleIdentifier($parameters['locale']); 
    2424      $found = true; 
    2525    } catch(AgaviException $e) { 
  • branches/0.11/src/translation/AgaviLocale.class.php

    r1656 r1698  
    2828 * @version    $Id$ 
    2929 */ 
    30 class AgaviLocale 
     30class AgaviLocale extends AgaviParameterHolder 
    3131{ 
    3232  /** 
     
    5050   * 
    5151   * @param      AgaviContext The current application context. 
     52   * @param      array        An associative array of initialization parameters. 
    5253   * @param      string       The identifier of the locale 
    53    * @param      array        An associative array of initialization parameters. 
     54   * @param      array        The locale data. 
    5455   * 
    5556   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     57   * @author     David Zuelke <dz@bitxtender.com> 
    5658   * @since      0.11.0 
    5759   */ 
    58   public function initialize(AgaviContext $context, $identifier, array $data = array()) 
     60  public function initialize(AgaviContext $context, array $parameters = array(), $identifier, array $data = array()) 
    5961  { 
    6062    $this->context = $context; 
     63    $this->parameters = $parameters; 
     64     
    6165    $this->identifier = $identifier; 
    6266    $this->data = $data; 
  • branches/0.11/src/translation/AgaviTranslationManager.class.php

    r1695 r1698  
    6262 
    6363  /** 
    64    * @var        string The current locale. 
     64   * @var        string The default locale identifier. 
    6565   */ 
    6666  protected $defaultLocaleIdentifier = null; 
     
    8181   */ 
    8282  protected $availableLocales = array(); 
     83 
     84  /** 
     85   * @var        array A cache for locale instances. 
     86   */ 
     87  protected $localeCache = array(); 
    8388 
    8489  /** 
     
    166171  public function setLocale($identifier) 
    167172  { 
    168     $this->currentLocaleIdentifier = $this->getClosestMatchingLocale($identifier); 
     173    $this->currentLocaleIdentifier = $this->getLocaleIdentifier($identifier); 
    169174    $givenData = AgaviLocale::parseLocaleIdentifier($identifier); 
    170175    $actualData = AgaviLocale::parseLocaleIdentifier($this->currentLocaleIdentifier); 
     
    203208 
    204209  /** 
     210   * Retrieve the default locale. 
     211   * 
     212   * @return     AgaviLocale The current default. 
     213   * 
     214   * @author     David Zülke <dz@bitxtender.com> 
     215   * @since      0.11.0 
     216   */ 
     217  public function getDefaultLocale() 
     218  { 
     219    $this->getLocale($this->defaultLocale); 
     220  } 
     221 
     222  /** 
     223   * Retrieve the default locale identifier. 
     224   * 
     225   * @return     string The default locale identifier. 
     226   * 
     227   * @author     David Zülke <dz@bitxtender.com> 
     228   * @since      0.11.0 
     229   */ 
     230  public function getDefaultLocaleIdentifier() 
     231  { 
     232    return $this->defaultLocaleIdentifier; 
     233  } 
     234 
     235  /** 
    205236   * Sets the default domain. 
    206237   * 
     
    250281      $this->loadCurrentLocale(); 
    251282    } elseif(is_string($locale)) { 
    252       $locale = $this->getLocaleFromIdentifier($locale); 
     283      $locale = $this->getLocale($locale); 
    253284    } 
    254285     
     
    285316      $this->loadCurrentLocale(); 
    286317    } elseif(is_string($locale)) { 
    287       $locale = $this->getLocaleFromIdentifier($locale); 
     318      $locale = $this->getLocale($locale); 
    288319    } 
    289320     
     
    320351      $this->loadCurrentLocale(); 
    321352    } elseif(is_string($locale)) { 
    322       $locale = $this->getLocaleFromIdentifier($locale); 
     353      $locale = $this->getLocale($locale); 
    323354    } 
    324355     
     
    358389      $this->loadCurrentLocale(); 
    359390    } elseif(is_string($locale)) { 
    360       $locale = $this->getLocaleFromIdentifier($locale); 
     391      $locale = $this->getLocale($locale); 
    361392    } 
    362393     
     
    475506  { 
    476507    if(!$this->currentLocale || $this->currentLocale->getIdentifier() != $this->givenLocaleIdentifier) { 
    477       $this->currentLocale = $this->getLocaleFromIdentifier($this->givenLocaleIdentifier); 
     508      $this->currentLocale = $this->getLocale($this->givenLocaleIdentifier); 
    478509      foreach($this->translators as $translatorList) { 
    479510        foreach($translatorList as $translator) { 
     
    507538 
    508539  /** 
     540   * @see        AgaviTranslationManager::getLocaleIdentifier 
     541   * 
     542   * @deprecated 
     543   */ 
     544  public function getClosestMatchingLocale($identifier) 
     545  { 
     546    return $this->getLocaleIdentifier($identifier); 
     547  } 
     548   
     549  /** 
    509550   * Returns the identifier of the available locale which matches the given  
    510551   * locale identifier most. 
    511552   * 
    512    * @param      string The locale identifier 
    513    * 
    514    * @return     string The locale identifier of the available locale. 
    515    * 
    516    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    517    * @since      0.11.0 
    518    */ 
    519   public function getClosestMatchingLocale($identifier) 
    520   { 
    521  
     553   * @param      string A locale identifier 
     554   * 
     555   * @return     string The actual locale identifier of the available locale. 
     556   * 
     557   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     558   * @author     David Zülke <dz@bitxtender.com> 
     559   * @since      0.11.0 
     560   */ 
     561  public function getLocaleIdentifier($identifier) 
     562  { 
    522563    // if a locale with the given identifier doesn't exist try to find the closest 
    523564    // match or bail out on no match or an ambigious match 
     
    566607 
    567608  /** 
     609   * @see        AgaviTranslationManager::getLocale 
     610   * 
     611   * @deprecated 
     612   */ 
     613  public function getLocaleFromIdentifier($identifier) 
     614  { 
     615    return $this->getLocale($identifier); 
     616  } 
     617   
     618  /** 
    568619   * Returns a new AgaviLocale object from the given identifier. 
    569620   * 
    570621   * @param      string The locale identifier 
     622   * @param      bool   Force a new instance even if an identical one exists. 
    571623   * 
    572624   * @return     AgaviLocale The locale instance which matches the available 
     
    574626   * 
    575627   * @author     Dominik del Bondio <ddb@bitxtender.com> 
    576    * @since      0.11.0 
    577    */ 
    578   public function getLocaleFromIdentifier($identifier) 
     628   * @author     David Zülke <dz@bitxtender.com> 
     629   * @since      0.11.0 
     630   */ 
     631  public function getLocale($identifier, $forceNew = false) 
    579632  { 
    580633    // enable shortcut notation to only set options to the current locale 
     
    589642    } 
    590643    // this doesn't care about the options 
    591     $availableLocale = $this->availableLocales[$this->getClosestMatchingLocale($identifier)]; 
     644    $availableLocale = $this->availableLocales[$this->getLocaleIdentifier($identifier)]; 
     645 
     646    // if the user wants all options reset he supplies an 'empty' option set (identifier ends with @) 
     647    if(substr($identifier, -1) == '@') { 
     648      $idData['options'] = array(); 
     649    } else { 
     650      $idData['options'] = array_merge($availableLocale['identifierData']['options'], $idData['options']); 
     651    } 
     652 
     653    if(($atPos = strpos($identifier, '@')) !== false) { 
     654      $identifier = $availableLocale['identifierData']['locale_str'] . substr($identifier, $atPos); 
     655    } else { 
     656      $identifier = $availableLocale['identifier']; 
     657    } 
     658 
     659    if(!$forceNew && isset($this->localeCache[$identifier])) { 
     660      return $this->localeCache[$identifier]; 
     661    } 
    592662 
    593663    if(!isset($this->localeDataCache[$idData['locale_str']])) { 
     
    616686      } 
    617687 
    618       $this->localeDataCache[$idData['locale_str']] = array_merge_recursive($data, $availableLocale['parameters']); 
     688      $this->localeDataCache[$idData['locale_str']] = $data; 
    619689    } 
    620690 
    621691    $data = $this->localeDataCache[$idData['locale_str']]; 
    622  
    623     // if the user wants all options reset he supplies an 'empty' option set (identifier ends with @) 
    624     if(substr($identifier, -1) == '@') { 
    625       $idData['options'] = array(); 
    626     } else { 
    627       $idData['options'] = array_merge($availableLocale['identifierData']['options'], $idData['options']); 
    628     } 
    629692 
    630693    if(isset($idData['options']['calendar'])) { 
     
    640703    } 
    641704 
    642     if(($atPos = strpos($identifier, '@')) !== false) { 
    643       $identifier = $availableLocale['identifierData']['locale_str'] . substr($identifier, $atPos); 
    644     } else { 
    645       $identifier = $availableLocale['identifier']; 
    646     } 
    647  
    648705    $locale = new AgaviLocale(); 
    649     $locale->initialize($this->context, $identifier, $data); 
     706    $locale->initialize($this->context, $availableLocale['parameters'], $identifier, $data); 
     707 
     708    if(!$forceNew) { 
     709      $this->localeCache[$identifier] = $locale; 
     710    } 
    650711 
    651712    return $locale; 
  • branches/0.11/src/validator/AgaviDateTimeValidator.class.php

    r1656 r1698  
    9393 
    9494    $check = $this->getParameter('check', true); 
    95     $locale = $this->hasParameter('locale') ? $tm->getLocaleFromIdentifier($this->getParameter('locale')) : $tm->getCurrentLocale(); 
     95    $locale = $this->hasParameter('locale') ? $tm->getLocale($this->getParameter('locale')) : $tm->getCurrentLocale(); 
    9696 
    9797    if($this->hasMultipleArguments() && !$this->getParameter('arguments_format')) { 
     
    131131      $matchedFormat = false; 
    132132      foreach($this->getParameter('formats', array()) as $item) { 
    133         $itemLocale = empty($item['locale']) ? $locale : $tm->getLocaleFromIdentifier($item['locale']); 
     133        $itemLocale = empty($item['locale']) ? $locale : $tm->getLocale($item['locale']); 
    134134        $type = empty($item['type']) ? 'format' : $item['type']; 
    135135 
  • branches/0.11/src/validator/AgaviNumberValidator.class.php

    r1660 r1698  
    5555    if(!$this->getParameter('no_locale', false)) { 
    5656      if($locale = $this->getParameter('in_locale')) { 
    57         $locale = $this->getContext()->getTranslationManager()->getLocaleFromIdentifier($locale); 
     57        $locale = $this->getContext()->getTranslationManager()->getLocale($locale); 
    5858      } else { 
    5959        $locale = $this->getContext()->getTranslationManager()->getCurrentLocale(); 
  • branches/0.11/tests2/date/CalendarTest.php

    r1281 r1698  
    313313  { 
    314314  // TODO: check how to do this properly ... 
    315     $c = $this->tm->createCalendar($this->tm->getLocaleFromIdentifier('en_US')); 
     315    $c = $this->tm->createCalendar($this->tm->getLocale('en_US')); 
    316316    $c->setLenient(false); 
    317317    $c->clear(); 
     
    727727     * to loop_addroll. - aliu */ 
    728728    $times = 20; 
    729     $cal = $this->tm->createCalendar($this->tm->getLocaleFromIdentifier('de_DE')); 
     729    $cal = $this->tm->createCalendar($this->tm->getLocale('de_DE')); 
    730730    $sdf = new AgaviSimpleDateFormat("YYYY'-W'ww-ee", AgaviLocale::getGermany()); 
    731731    $sdf->applyLocalizedPattern("JJJJ'-W'ww-ee"); 
  • branches/0.11/tests2/date/TimeZoneTest.php

    r1303 r1698  
    815815  public function testDisplayName() 
    816816  { 
    817     $enLocale = $this->tm->getLocaleFromIdentifier('en_US'); 
     817    $enLocale = $this->tm->getLocale('en_US'); 
    818818 
    819819    $i = 0; 
     
    863863    // Make sure we get the default display format for Locales 
    864864    // with no display name data. 
    865     $mt_MT = $this->tm->getLocaleFromIdentifier("mt_MT"); 
     865    $mt_MT = $this->tm->getLocale("mt_MT"); 
    866866 
    867867    $name = $zone->getDisplayName($mt_MT);