/**
  * Returns a new AgaviLocale object from the given identifier.
  *
  * @param      string The locale identifier
  * @param      bool   Force a new instance even if an identical one exists.
  *
  * @return     AgaviLocale The locale instance which matches the available
  *                         locales most.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @author     David Zülke <*****@*****.**>
  * @since      0.11.0
  */
 public function getLocale($identifier, $forceNew = false)
 {
     // enable shortcut notation to only set options to the current locale
     if ($identifier[0] == '@' && $this->currentLocaleIdentifier) {
         $idData = AgaviLocale::parseLocaleIdentifier($this->currentLocaleIdentifier);
         $identifier = $idData['locale_str'] . $identifier;
         $newIdData = AgaviLocale::parseLocaleIdentifier($identifier);
         $idData['options'] = array_merge($idData['options'], $newIdData['options']);
     } else {
         $idData = AgaviLocale::parseLocaleIdentifier($identifier);
     }
     // this doesn't care about the options
     $availableLocale = $this->availableLocales[$this->getLocaleIdentifier($identifier)];
     // if the user wants all options reset he supplies an 'empty' option set (identifier ends with @)
     if (substr($identifier, -1) == '@') {
         $idData['options'] = array();
     } else {
         $idData['options'] = array_merge($availableLocale['identifierData']['options'], $idData['options']);
     }
     if (($atPos = strpos($identifier, '@')) !== false) {
         $identifier = $availableLocale['identifierData']['locale_str'] . substr($identifier, $atPos);
     } else {
         $identifier = $availableLocale['identifier'];
     }
     if (!$forceNew && isset($this->localeCache[$identifier])) {
         return $this->localeCache[$identifier];
     }
     if (!isset($this->localeDataCache[$idData['locale_str']])) {
         $lookupPath = AgaviLocale::getLookupPath($availableLocale['identifierData']);
         $cldrDir = AgaviConfig::get('core.cldr_dir');
         $data = null;
         foreach ($lookupPath as $localeName) {
             $fileName = $cldrDir . '/locales/' . $localeName . '.xml';
             if (is_readable($fileName)) {
                 $data = (include AgaviConfigCache::checkConfig($fileName));
                 break;
             }
         }
         if ($data === null) {
             throw new AgaviException('No data available for locale ' . $identifier);
         }
         if ($availableLocale['identifierData']['territory']) {
             $territory = $availableLocale['identifierData']['territory'];
             if (isset($this->supplementalData['territories'][$territory]['currencies'])) {
                 $slice = array_slice($this->supplementalData['territories'][$territory]['currencies'], 0, 1);
                 $currency = current($slice);
                 $data['locale']['currency'] = $currency['currency'];
             }
         }
         $this->localeDataCache[$idData['locale_str']] = $data;
     }
     $data = $this->localeDataCache[$idData['locale_str']];
     if (isset($idData['options']['calendar'])) {
         $data['locale']['calendar'] = $idData['options']['calendar'];
     }
     if (isset($idData['options']['currency'])) {
         $data['locale']['currency'] = $idData['options']['currency'];
     }
     if (isset($idData['options']['timezone'])) {
         $data['locale']['timezone'] = $idData['options']['timezone'];
     }
     $locale = new AgaviLocale();
     $locale->initialize($this->context, $availableLocale['parameters'], $identifier, $data);
     if (!$forceNew) {
         $this->localeCache[$identifier] = $locale;
     }
     return $locale;
 }