2
0
Преглед на файлове

[ZF-8383] Zend_Translate:

- corrected wrong id usage

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19457 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas преди 16 години
родител
ревизия
0b0769bf5a

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

@@ -175,6 +175,17 @@ $translate->addTranslation('/path/to/new.csv', 'fr', $options);
                         </entry>
                         <entry><emphasis>0</emphasis></entry>
                     </row>
+                    <row>
+                        <entry>useId</entry>
+                        <entry>Xliff</entry>
+                        <entry>
+                            If you set this option to <constant>FALSE</constant>, then the source
+                            string will be used as message Id. The default for this option is
+                            <constant>TRUE</constant>, which means that the Id from the trans-unit
+                            element will be used as message Id
+                        </entry>
+                        <entry><emphasis>true</emphasis></entry>
+                    </row>
                 </tbody>
             </tgroup>
         </table>

+ 23 - 0
documentation/manual/en/ref/migration-110.xml

@@ -57,6 +57,29 @@
         </sect3>
     </sect2>
 
+    <sect2 id="migration.110.zend.translate">
+        <title>Zend_Translate</title>
+
+        <sect3 id="migration.110.zend.translate.xliff">
+            <title>Xliff adapter</title>
+
+            <para>
+                In past the Xliff adapter used the source string as message Id. According to the
+                Xliff standard the trans-unit Id should be used. This behaviour was corrected with
+                Zend Framework 1.10. Now the trans-unit Id is used as message Id per default.
+            </para>
+
+            <para>
+                But you can still get the incorrect and old behaviour by setting the
+                <property>useId</property> option to <constant>FALSE</constant>.
+            </para>
+
+            <programlisting language="php"><![CDATA[
+$trans = new Zend_Translate('xliff', '/path/to/source', $locale, array('useId' => false));
+]]></programlisting>
+        </sect3>
+    </sect2>
+
     <sect2 id="migration.110.zend.validate">
         <title>Zend_Validate</title>
 

+ 33 - 8
library/Zend/Translate/Adapter/Xliff.php

@@ -36,10 +36,12 @@ require_once 'Zend/Translate/Adapter.php';
 class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter {
     // Internal variables
     private $_file        = false;
+    private $_useId       = true;
     private $_cleared     = array();
     private $_transunit   = null;
     private $_source      = null;
     private $_target      = null;
+    private $_langId      = null;
     private $_scontent    = null;
     private $_tcontent    = null;
     private $_stag        = false;
@@ -64,6 +66,12 @@ class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter {
             throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
         }
 
+        if (empty($options['useId'])) {
+            $this->_useId = false;
+        } else {
+            $this->_useId = true;
+        }
+
         $encoding      = $this->_findEncoding($filename);
         $this->_target = $locale;
         $this->_file   = xml_parser_create($encoding);
@@ -81,6 +89,7 @@ class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter {
             throw new Zend_Translate_Exception($ex);
         }
 
+var_dump($this->_data);
         return $this->_data;
     }
 
@@ -117,6 +126,7 @@ class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter {
                     break;
                 case 'trans-unit':
                     $this->_transunit = true;
+                    $this->_langId = $attrib['id'];
                     break;
                 case 'source':
                     if ($this->_transunit === true) {
@@ -148,20 +158,35 @@ class Zend_Translate_Adapter_Xliff extends Zend_Translate_Adapter {
             switch (strtolower($name)) {
                 case 'trans-unit':
                     $this->_transunit = null;
-                    $this->_scontent = null;
-                    $this->_tcontent = null;
+                    $this->_langId    = null;
+                    $this->_scontent  = null;
+                    $this->_tcontent  = null;
                     break;
                 case 'source':
-                    if (!empty($this->_scontent) and !empty($this->_tcontent) or
-                        (isset($this->_data[$this->_source][$this->_scontent]) === false)) {
-                        $this->_data[$this->_source][$this->_scontent] = $this->_scontent;
+                    if ($this->_useId) {
+                        if (!empty($this->_scontent) && !empty($this->_langId) &&
+                            !isset($this->_data[$this->_source][$this->_langId])) {
+                            $this->_data[$this->_source][$this->_langId] = $this->_scontent;
+                        }
+                    } else {
+                        if (!empty($this->_scontent) &&
+                            !isset($this->_data[$this->_source][$this->_scontent])) {
+                            $this->_data[$this->_source][$this->_scontent] = $this->_scontent;
+                        }
                     }
                     $this->_stag = false;
                     break;
                 case 'target':
-                    if (!empty($this->_scontent) and !empty($this->_tcontent) or
-                        (isset($this->_data[$this->_source][$this->_scontent]) === false)) {
-                        $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
+                    if ($this->_useId) {
+                        if (!empty($this->_tcontent) && !empty($this->_langId) &&
+                            !isset($this->_data[$this->_target][$this->_langId])) {
+                            $this->_data[$this->_target][$this->_langId] = $this->_tcontent;
+                        }
+                    } else {
+                        if (!empty($this->_tcontent) && !empty($this->_scontent) &&
+                            !isset($this->_data[$this->_target][$this->_scontent])) {
+                            $this->_data[$this->_target][$this->_scontent] = $this->_tcontent;
+                        }
                     }
                     $this->_ttag = false;
                     break;

+ 1 - 1
tests/Zend/Translate/Adapter/XliffTest.php

@@ -200,7 +200,7 @@ class Zend_Translate_Adapter_XliffTest extends PHPUnit_Framework_TestCase
 
     public function testIsoEncoding()
     {
-        $adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en3.xliff', 'en');
+        $adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en3.xliff', 'en', array('useId' => false));
         $this->assertEquals('Message 1 (en)', $adapter->translate('Message 1'));
         $this->assertEquals('Message 1 (en)', $adapter->_('Message 1'));
 

+ 7 - 7
tests/Zend/Translate/Adapter/_files/translation_en.xliff

@@ -2,31 +2,31 @@
 <xliff version='1.1' xmlns='urn:oasis:names:tc:xliff:document:1.1'>
  <file original='hello.txt' source-language='en' target-language='fr' datatype='plaintext'>
   <body>
-   <trans-unit id='1'>
+   <trans-unit id='Message 1'>
     <source>Message 1</source>
     <target>Message 1 (en)</target>
    </trans-unit>
-   <trans-unit id='2'>
+   <trans-unit id='Message 2'>
     <source>Message 2</source>
     <target>Message 2 (en)</target>
    </trans-unit>
-   <trans-unit id='3'>
+   <trans-unit id='Message 3'>
     <source>Message 3</source>
     <target>Message 3 (en)</target>
    </trans-unit>
-   <trans-unit id='4'>
+   <trans-unit id='Message 4'>
     <source>Message 4</source>
     <target>Message 4 (en)</target>
    </trans-unit>
-   <trans-unit id='5'>
+   <trans-unit id='Message 5'>
     <source>Message 5</source>
     <target>Message 5 (en)</target>
    </trans-unit>
-   <trans-unit id='6'>
+   <trans-unit id='Küchen Möbel'>
     <source>Küchen Möbel</source>
     <target>Cooking furniture (en)</target>
    </trans-unit>
-   <trans-unit id='7'>
+   <trans-unit id='Cooking furniture'>
     <source>Cooking furniture</source>
     <target>Küchen Möbel (en)</target>
    </trans-unit>