Explorar o código

[ZF-9123] Zend_Translate:

- add useId for TMX

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21049 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas %!s(int64=16) %!d(string=hai) anos
pai
achega
5b6874b7b5

+ 1 - 1
documentation/manual/en/module_specs/Zend_Translate-Additional.xml

@@ -210,7 +210,7 @@ $translate->addTranslation('/path/to/new.csv', 'fr', $options);
 
                     <row>
                         <entry>useId</entry>
-                        <entry>Xliff</entry>
+                        <entry>Xliff and Tmx</entry>
 
                         <entry>
                             If you set this option to <constant>FALSE</constant>, then the source

+ 37 - 13
documentation/manual/en/module_specs/Zend_Translate-SourceCreation.xml

@@ -152,19 +152,21 @@ print_r($translate->getAdapterInfo());
 <?xml version="1.0" ?>
 <!DOCTYPE tmx SYSTEM "tmx14.dtd">
 <tmx version="1.4">
- <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence"
-         adminlang="en-us" srclang="de-at" o-tmf="abc"
-         creationtool="XYZTool" >
- </header>
- <body>
-  <tu tuid='message1'>
-   <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
-   <tuv xml:lang="en"><seg>message1</seg></tuv>
-  </tu>
-  <tu tuid='message2'>
-   <tuv xml:lang="en"><seg>message2</seg></tuv>
-   <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
-  </tu>
+   <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence"
+           adminlang="en-us" srclang="de-at" o-tmf="abc"
+           creationtool="XYZTool" >
+   </header>
+   <body>
+       <tu tuid='message1'>
+           <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
+           <tuv xml:lang="en"><seg>message1</seg></tuv>
+       </tu>
+       <tu tuid='message2'>
+           <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
+           <tuv xml:lang="en"><seg>message2</seg></tuv>
+       </tu>
+   </body>
+</tmx>
 ]]></programlisting>
 
             <programlisting language="php"><![CDATA[
@@ -185,6 +187,28 @@ $translate = new Zend_Translate('tmx', 'path/to/mytranslation.tmx', 'en');
             <methodname>addLanguage()</methodname>. The default value for this option is to add all
             languages.
         </para>
+
+        <note>
+            <title>Option useId</title>
+
+            <para>
+                When you set the <emphasis>useId</emphasis> option to <constant>FALSE</constant>
+                then the <emphasis>srclang</emphasis> header will be used to define the language
+                which sets the message.
+            </para>
+
+            <para>
+                In our example the message key would <emphasis>message1</emphasis> per default.
+                When this option is set to <constant>FALSE</constant> the message key
+                <emphasis>Nachricht1</emphasis> would be used.
+            </para>
+
+            <para>
+                Note that the <emphasis>tuv</emphasis> entry which is related to the
+                <emphasis>srclang</emphasis> entry must be the first
+                <emphasis>tuv</emphasis> entry which is set like shown in the above example.
+            </para>
+        </note>
     </sect2>
 
     <sect2 id="zend.translate.sourcecreation.csv">

+ 30 - 4
library/Zend/Translate/Adapter/Tmx.php

@@ -36,6 +36,8 @@ require_once 'Zend/Translate/Adapter.php';
 class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
     // Internal variables
     private $_file    = false;
+    private $_useId   = true;
+    private $_srclang = null;
     private $_tu      = null;
     private $_tuv     = null;
     private $_seg     = null;
@@ -60,6 +62,10 @@ class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
             throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
         }
 
+        if (isset($options['useId'])) {
+            $this->_useId = (boolean) $options['useId'];
+        }
+
         $encoding = $this->_findEncoding($filename);
         $this->_file = xml_parser_create($encoding);
         xml_set_object($this->_file, $this);
@@ -96,13 +102,30 @@ class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
             $this->_content .= ">";
         } else {
             switch(strtolower($name)) {
+                case 'header':
+                    if (empty($this->_useId) && isset($attrib['srclang'])) {
+                        if (Zend_Locale::isLocale($attrib['srclang'])) {
+                            $this->_srclang = Zend_Locale::findLocale($attrib['srclang']);
+                        } else {
+                            if (!$this->_options['disableNotices']) {
+                                if ($this->_options['log']) {
+                                    $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist.");
+                                } else {
+                                    trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE);
+                                }
+                            }
+
+                            $this->_srclang = $attrib['srclang'];
+                        }
+                    }
+                    break;
                 case 'tu':
-                    if (isset($attrib['tuid']) === true) {
+                    if (isset($attrib['tuid'])) {
                         $this->_tu = $attrib['tuid'];
                     }
                     break;
                 case 'tuv':
-                    if (isset($attrib['xml:lang']) === true) {
+                    if (isset($attrib['xml:lang'])) {
                         if (Zend_Locale::isLocale($attrib['xml:lang'])) {
                             $this->_tuv = Zend_Locale::findLocale($attrib['xml:lang']);
                         } else {
@@ -117,7 +140,7 @@ class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
                             $this->_tuv = $attrib['xml:lang'];
                         }
 
-                        if (isset($this->_data[$this->_tuv]) === false) {
+                        if (!isset($this->_data[$this->_tuv])) {
                             $this->_data[$this->_tuv] = array();
                         }
                     }
@@ -153,6 +176,10 @@ class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
                     break;
                 case 'seg':
                     $this->_seg = null;
+                    if (!empty($this->_srclang) && ($this->_srclang == $this->_tuv)) {
+                        $this->_tu = $this->_content;
+                    }
+
                     if (!empty($this->_content) or (!isset($this->_data[$this->_tuv][$this->_tu]))) {
                         $this->_data[$this->_tuv][$this->_tu] = $this->_content;
                     }
@@ -163,7 +190,6 @@ class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
         }
     }
 
-
     /**
      * Internal method, called by xml element handler for content
      *

+ 9 - 0
tests/Zend/Translate/Adapter/TmxTest.php

@@ -236,6 +236,15 @@ class Zend_Translate_Adapter_TmxTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('Message 1 (fr)', $adapter->_('Message 1', 'fr_FR'));
     }
 
+    public function testUseId()
+    {
+        $adapter = new Zend_Translate_Adapter_Tmx(dirname(__FILE__) . '/_files/translation_en2.tmx', 'en', array('useId' => false));
+        $this->assertEquals(false, $adapter->getOptions('useId'));
+        $this->assertEquals('Message 1 (en)', $adapter->translate('Nachricht 1'));
+        $this->assertEquals('Message 1 (en)', $adapter->_('Nachricht 1'));
+        $this->assertEquals('Nachricht 6', $adapter->translate('Nachricht 6'));
+    }
+
     /**
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      *