/** * Retrieve translate object * * @throws Zend_Application_Resource_Exception if registry key was used * already but is no instance of Zend_Translate * @return Zend_Translate */ public function getTranslate() { if (null === $this->_translate) { $this->buildLog(); // retrieve cache if requested if (isset($this->_options['cacheEnabled']) && $this->_options['cacheEnabled']) { // check for cachemanager in bootstrap if (!$this->getBootstrap()->hasPluginResource('cachemanager')) { throw new Zend_Application_Resource_Exception("You must configure the cachemanager with " . "the key {$this->getCacheKey()}"); } // bootstrap the cachemanager and retrieve it /** @var $cacheManager Zend_Cache_Manager */ $cacheManager = $this->getBootstrap()->bootstrap('cachemanager')->getResource('cachemanager'); // check for the given key if (!$cacheManager->hasCache($this->getCacheKey())) { throw new Zend_Application_Resource_Exception("You must configure the cachemanager with " . "the key {$this->getCacheKey()}"); } // set cache for translator Zend_Translate_Adapter::setCache($cacheManager->getCache($this->getCacheKey())); } // fetch translate object into local variable $this->_translate = parent::getTranslate(); } return $this->_translate; }
/** * Retrieve translation configuration options. * * @return string */ private function _setTranslate($locale, $cache) { $options = array('bootstrap' => $this->getBootstrap(), 'locale' => $locale, 'localeName' => $locale, 'adapter' => 'gettext', 'disableNotices' => true); if ($cache) { $options['cache'] = $cache; } $translatePath = LANGUAGES_DIR . "/{$locale}.mo"; if (is_readable($translatePath)) { $options['content'] = $translatePath; } else { $options['content'] = ''; } $translateResource = new Zend_Application_Resource_Translate($options); $translateResource->getTranslate(); }
/** * @group ZF-7352 */ public function testTranslationIsAddedIfRegistryKeyExistsAlready() { $options1 = array('foo' => 'bar'); $options2 = array_merge_recursive($this->_translationOptions, array('data' => array('message4' => 'bericht4'))); $translate = new Zend_Translate(Zend_Translate::AN_ARRAY, $options1); Zend_Registry::set('Zend_Translate', $translate); $resource = new Zend_Application_Resource_Translate($options2); $this->assertTrue($translate === $resource->getTranslate()); $this->assertEquals('bar', $translate->translate('foo')); $this->assertEquals('bericht4', $translate->translate('message4')); $this->assertEquals('shouldNotExist', $translate->translate('shouldNotExist')); }