Преглед изворни кода

[ZF-9489] Zend_Translate:

- added adapter combination

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21643 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas пре 16 година
родитељ
комит
82142c0109

+ 62 - 0
documentation/manual/en/module_specs/Zend_Translate-Additional.xml

@@ -625,6 +625,68 @@ $translate = new Zend_Translate(
         </sect3>
         </sect3>
     </sect2>
     </sect2>
 
 
+    <sect2 id="zend.translate.additional.combination">
+        <title>Combining multiple translation sources</title>
+
+        <para>
+            When you are working with multiple translations you may come into a situation where you
+            want to use different source types. For example the resource files which are provided
+            by the framework and your own translations which are available by using the gettext
+            adapter.
+        </para>
+
+        <para>
+            By combining multiple translation adapters you can use them within one instance. See
+            the following example:
+        </para>
+
+        <programlisting><![CDATA[
+$translate = new Zend_Translate(
+    'gettext',
+    '\path\to\translation.mo',
+    'en'
+);
+
+$translate_second = new Zend_Translate(
+    'array',
+    '\resources\languages\en\Zend_Validate.php',
+    'en'
+);
+
+$translate->addTranslation($translate_second);
+]]></programlisting>
+
+        <para>
+            Now the first instance holds all translations from the second instance and you can use
+            it within the application even if you used different source types.
+        </para>
+
+        <note>
+            <title>Memory savings</title>
+
+            <para>
+                As you may have noted the second instance is no longer used as soon as it has been
+                added to the first instance. To save some memory you may want to unset it.
+            </para>
+        </note>
+
+        <para>
+            When you are scanning for directories you may additionally want to use only one defined
+            language. The predefined resources for example are available in more than 10 languages.
+            But your application is not available in all of those language. Therefor you can also
+            add only one language from the second adapter.
+        </para>
+
+        <programlisting><![CDATA[
+$translate->addTranslation($translate_second, 'en');
+]]></programlisting>
+
+        <para>
+            This allows you still to scan through the directories and still add only those languages
+            which are relevant for your application.
+        </para>
+    </sect2>
+
     <sect2 id="zend.translate.additional.istranslated">
     <sect2 id="zend.translate.additional.istranslated">
         <title>Checking for translations</title>
         <title>Checking for translations</title>
 
 

+ 24 - 3
library/Zend/Translate/Adapter.php

@@ -162,7 +162,9 @@ abstract class Zend_Translate_Adapter {
         }
         }
 
 
         try {
         try {
-            $locale    = Zend_Locale::findLocale($locale);
+            if (!($data instanceof Zend_Translate) && !($data instanceof Zend_Translate_Adapter)) {
+                $locale = Zend_Locale::findLocale($locale);
+            }
         } catch (Zend_Locale_Exception $e) {
         } catch (Zend_Locale_Exception $e) {
             require_once 'Zend/Translate/Exception.php';
             require_once 'Zend/Translate/Exception.php';
             throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
             throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
@@ -460,8 +462,23 @@ abstract class Zend_Translate_Adapter {
      */
      */
     private function _addTranslationData($data, $locale, array $options = array())
     private function _addTranslationData($data, $locale, array $options = array())
     {
     {
+        if (($data instanceof Zend_Translate) || ($data instanceof Zend_Translate_Adapter)) {
+            $options['usetranslateadapter'] = true;
+            if (!empty($locale)) {
+                $data = $data->getMessages($locale);
+            } else {
+                $locales = $data->getList();
+                foreach ($locales as $locale) {
+                    $trans = $data->getMessages($locale);
+                    $this->_addTranslationData($trans, $locale, $options);
+                }
+
+                return $this;
+            }
+        }
+
         try {
         try {
-            $locale    = Zend_Locale::findLocale($locale);
+            $locale = Zend_Locale::findLocale($locale);
         } catch (Zend_Locale_Exception $e) {
         } catch (Zend_Locale_Exception $e) {
             require_once 'Zend/Translate/Exception.php';
             require_once 'Zend/Translate/Exception.php';
             throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
             throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
@@ -485,7 +502,11 @@ abstract class Zend_Translate_Adapter {
         }
         }
 
 
         if ($read) {
         if ($read) {
-            $temp = $this->_loadTranslationData($data, $locale, $options);
+            if (!empty($options['usetranslateadapter'])) {
+                $temp = array($locale => $data);
+            } else {
+                $temp = $this->_loadTranslationData($data, $locale, $options);
+            }
         }
         }
 
 
         if (empty($temp)) {
         if (empty($temp)) {

+ 32 - 0
tests/Zend/TranslateTest.php

@@ -665,6 +665,38 @@ class Zend_TranslateTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 0));
         $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 0));
     }
     }
 
 
+
+    /**
+     * @group ZF-9489
+     */
+    public function testAddingAdapterToSourcealsUsingOwnRule()
+    {
+print "\n=============================================";
+        $translate = new Zend_Translate(
+            Zend_Translate::AN_ARRAY,
+            array('singular' =>
+                array('plural_0 (en)',
+                    'plural_1 (en)',
+                    'plural_2 (en)',
+                    'plural_3 (en)'),
+                'plural' => ''), 'en'
+        );
+
+        $this->assertFalse($translate->isTranslated('Message 1'));
+        $adapter = new Zend_Translate_Adapter_Gettext(dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.mo', 'en');
+        $translate->addTranslation($adapter);
+
+        $this->assertTrue($adapter->isTranslated('Message 1'));
+
+        $adapter2 = new Zend_Translate_Adapter_Gettext(dirname(__FILE__) . '/Translate/Adapter/_files/testmo/de_AT/LC_TEST/translation-de_DE.mo', 'de_AT');
+        $adapter2->addTranslation(dirname(__FILE__) . '/Translate/Adapter/_files/translation_en2.mo', 'fr');
+        $translate->addTranslation($adapter2, 'fr');
+
+        $languages = $translate->getList();
+        $this->assertFalse(array_key_exists('de_AT', $languages));
+        $this->assertTrue(array_key_exists('fr', $languages));
+    }
+
     /**
     /**
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      *
      *