Example #1
0
 public function testFormatWithCurrentLocaleTimeZone()
 {
     $zoneId = 'Europe/Berlin';
     $tm = $this->getContext()->getTranslationManager();
     $tm->setDefaultTimeZone('Europe/Moscow');
     $currentLocale = AgaviLocale::parseLocaleIdentifier($tm->getCurrentLocale()->getIdentifier());
     $tm->setLocale($currentLocale['locale_str'] . '@timezone=' . $zoneId);
     $inputFormat = $tm->createDateFormat('yyy-MM-dd HH:mm:ss v');
     $cal = $inputFormat->parse('2008-11-19 23:00:00 America/New_York');
     $this->assertEquals('2008-11-20 05:00:00 ' . $zoneId, $inputFormat->format($cal));
 }
 /**
  * Execute this configuration handler.
  *
  * @param      AgaviXmlConfigDomDocument The document to parse.
  *
  * @return     string Data to be written to a cache file.
  *
  * @throws     <b>AgaviParseException</b> If a requested configuration file is
  *                                        improperly formatted.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @author     David Zülke <*****@*****.**>
  * @since      0.11.0
  */
 public function execute(AgaviXmlConfigDomDocument $document)
 {
     // set up our default namespace
     $document->setDefaultNamespace(self::XML_NAMESPACE, 'translation');
     $config = $document->documentURI;
     $translatorData = array();
     $localeData = array();
     $defaultDomain = '';
     $defaultLocale = null;
     $defaultTimeZone = null;
     foreach ($document->getConfigurationElements() as $cfg) {
         if ($cfg->hasChild('available_locales')) {
             $availableLocales = $cfg->getChild('available_locales');
             // TODO: is this really optional? according to the schema: yes...
             $defaultLocale = $availableLocales->getAttribute('default_locale', $defaultLocale);
             $defaultTimeZone = $availableLocales->getAttribute('default_timezone', $defaultTimeZone);
             foreach ($availableLocales as $locale) {
                 $name = $locale->getAttribute('identifier');
                 if (!isset($localeData[$name])) {
                     $localeData[$name] = array('name' => $name, 'params' => array(), 'fallback' => null, 'ldml_file' => null);
                 }
                 $localeData[$name]['params'] = $locale->getAgaviParameters($localeData[$name]['params']);
                 $localeData[$name]['fallback'] = $locale->getAttribute('fallback', $localeData[$name]['fallback']);
                 $localeData[$name]['ldml_file'] = $locale->getAttribute('ldml_file', $localeData[$name]['ldml_file']);
             }
         }
         if ($cfg->hasChild('translators')) {
             $translators = $cfg->getChild('translators');
             $defaultDomain = $translators->getAttribute('default_domain', $defaultDomain);
             $this->getTranslators($translators, $translatorData);
         }
     }
     $data = array();
     $data[] = sprintf('$this->defaultDomain = %s;', var_export($defaultDomain, true));
     $data[] = sprintf('$this->defaultLocaleIdentifier = %s;', var_export($defaultLocale, true));
     $data[] = sprintf('$this->defaultTimeZone = %s;', var_export($defaultTimeZone, true));
     foreach ($localeData as $locale) {
         // TODO: fallback stuff
         $data[] = sprintf('$this->availableConfigLocales[%s] = array(\'identifier\' => %s, \'identifierData\' => %s, \'parameters\' => %s);', var_export($locale['name'], true), var_export($locale['name'], true), var_export(AgaviLocale::parseLocaleIdentifier($locale['name']), true), var_export($locale['params'], true));
     }
     foreach ($translatorData as $domain => $translator) {
         foreach (array('msg', 'num', 'cur', 'date') as $type) {
             if (isset($translator[$type]['class'])) {
                 if (!class_exists($translator[$type]['class'])) {
                     throw new AgaviConfigurationException(sprintf('The Translator or Formatter class "%s" for domain "%s" could not be found.', $translator[$type]['class'], $domain));
                 }
                 $data[] = join("\n", array(sprintf('$this->translators[%s][%s] = new %s();', var_export($domain, true), var_export($type, true), $translator[$type]['class']), sprintf('$this->translators[%s][%s]->initialize($this->getContext(), %s);', var_export($domain, true), var_export($type, true), var_export($translator[$type]['params'], true)), sprintf('$this->translatorFilters[%s][%s] = %s;', var_export($domain, true), var_export($type, true), var_export($translator[$type]['filters'], true))));
             }
         }
     }
     return $this->generate($data, $config);
 }
 /**
  * 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;
 }