Procházet zdrojové kódy

Resolves ZF-7352. Zend_App_Resource_Translate now uses an existing translate adapter in case it's available in the registry already.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18597 44c647ce-9c0f-0410-b52a-842ac1e357ba
freak před 16 roky
rodič
revize
9f580165b1

+ 21 - 6
library/Zend/Application/Resource/Translate.php

@@ -53,6 +53,8 @@ class Zend_Application_Resource_Translate extends Zend_Application_Resource_Reso
      * Retrieve translate object
      *
      * @return Zend_Translate
+     * @throws Zend_Application_Resource_Exception if registry key was used
+     *          already but is no instance of Zend_Translate
      */
     public function getTranslate()
     {
@@ -67,17 +69,30 @@ class Zend_Application_Resource_Translate extends Zend_Application_Resource_Reso
             $locale  = isset($options['locale'])  ? $options['locale']  : null;
             $translateOptions = isset($options['options']) ? $options['options'] : array();
 
-            $this->_translate = new Zend_Translate(
-                $adapter, $options['data'], $locale, $translateOptions
-            );
-
             $key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
                  ? $options['registry_key']
                  : self::DEFAULT_REGISTRY_KEY;
 
-            Zend_Registry::set($key, $this->_translate);
+            if(Zend_Registry::isRegistered($key)) {
+                $translate = Zend_Registry::get($key);
+                if(!$translate instanceof Zend_Translate) {
+                	require_once 'Zend/Application/Resource/Exception.php';
+                	throw new Zend_Application_Resource_Exception($key
+                	               . ' already registered in registry but is '
+                	               . 'no instance of Zend_Translate');
+                }
+                
+                $translate->addTranslation($options['data'], $locale, $options);
+                $this->_translate = $translate;
+            } else {
+                $this->_translate = new Zend_Translate(
+                    $adapter, $options['data'], $locale, $translateOptions
+                );
+                
+                Zend_Registry::set($key, $this->_translate);
+            }
         }
 
         return $this->_translate;
     }
-}
+}

+ 20 - 0
tests/Zend/Application/Resource/TranslateTest.php

@@ -128,6 +128,26 @@ class Zend_Application_Resource_TranslateTest extends PHPUnit_Framework_TestCase
     		$this->assertType('Zend_Application_Resource_Exception', $e);
     	}
     }
+    
+    /**
+     * @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'));
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_TranslateTest::main') {