/**
  * Short description of method read
  *
  * @access public
  * @author firstname and lastname of author, <*****@*****.**>
  * @throws tao_helpers_translation_TranslationException
  * @return mixed
  */
 public function read()
 {
     $file = $this->getFilePath();
     if (!file_exists($file)) {
         throw new tao_helpers_translation_TranslationException("The translation file '{$file}' does not exist.");
     }
     // Create the translation file.
     $tf = new tao_helpers_translation_POFile();
     $fc = implode('', file($file));
     $matched = preg_match_all('/((?:#[\\.:,\\|]{0,1}\\s+(?:.*?)\\n)*)' . '(msgctxt\\s+(?:"(?:[^"]|\\\\")*?"\\s*)+)?' . '(msgid\\s+(?:"(?:[^"]|\\\\")*?"\\s*)+)\\s+' . '(msgstr\\s+(?:"(?:[^"]|\\\\")*?(?<!\\\\)"\\s*)+)/', $fc, $matches);
     preg_match('/sourceLanguage: (.*?)\\n/s', $fc, $sourceLanguage);
     preg_match('/targetLanguage: (.*?)\\n/s', $fc, $targetLanguage);
     if (count($sourceLanguage)) {
         $tf->setSourceLanguage(substr($sourceLanguage[1], 0, 5));
     }
     if (count($targetLanguage)) {
         $tf->setTargetLanguage(substr($targetLanguage[1], 0, 5));
     }
     if ($matched) {
         for ($i = 0; $i < $matched; $i++) {
             $annotations = $matches[1][$i];
             $msgctxt = preg_replace('/\\s*msgctxt\\s*"(.*)"\\s*/s', '\\1', $matches[2][$i]);
             $msgid = preg_replace('/\\s*msgid\\s*"(.*)"\\s*/s', '\\1', $matches[3][$i]);
             $msgstr = preg_replace('/\\s*msgstr\\s*"(.*)"\\s*/s', '\\1', $matches[4][$i]);
             // Do not include meta data as a translation unit..
             if ($msgid !== '') {
                 // Sanitze the strings.
                 $msgid = tao_helpers_translation_POUtils::sanitize($msgid);
                 $msgstr = tao_helpers_translation_POUtils::sanitize($msgstr);
                 $msgctxt = tao_helpers_translation_POUtils::sanitize($msgctxt);
                 $tu = new tao_helpers_translation_POTranslationUnit();
                 // Set up source & target.
                 $tu->setSource($msgid);
                 if ($msgstr !== '') {
                     $tu->setTarget($msgstr);
                 }
                 if ($msgctxt) {
                     $tu->setContext($msgctxt);
                 }
                 // Deal with annotations
                 $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($annotations);
                 foreach ($annotations as $name => $value) {
                     $tu->addAnnotation($name, $value);
                 }
                 $tf->addTranslationUnit($tu);
             }
         }
     }
     $this->setTranslationFile($tf);
 }
Beispiel #2
0
 /**
  * @param $child
  * @param $xmlNS
  * @param $about
  * @param $tus
  * @return array
  */
 protected function processUnit($child, $xmlNS, $about, $tus)
 {
     if ($child->hasAttributeNS($xmlNS, 'lang')) {
         $sourceLanguage = 'en-US';
         $targetLanguage = $child->getAttributeNodeNS($xmlNS, 'lang')->value;
         $source = $child->nodeValue;
         $target = '';
         //$child->nodeValue;
         $tu = new tao_helpers_translation_POTranslationUnit();
         $tu->setSource($source);
         $tu->setTarget($target);
         $tu->setSourceLanguage($sourceLanguage);
         $tu->setTargetLanguage($targetLanguage);
         $tu->setAnnotations(array($about));
         $tu->setContext($child->namespaceURI . $child->localName);
         $tus[] = $tu;
         return $tus;
     }
     return $tus;
 }
 /**
  * Extracts the translation units from a structures.xml file.
  * Translation Units can be retrieved after extraction by calling the getTranslationUnits
  * method.
  *
  * @access public
  * @author Jerome Bogaerts <*****@*****.**>
  * @throws tao_helpers_translation_TranslationException If an error occurs.
  */
 public function extract()
 {
     $translationUnits = array();
     foreach ($this->getPaths() as $file) {
         $xml = new \SimpleXMLElement($file, null, true);
         if ($xml instanceof SimpleXMLElement) {
             // look up for "name" attributes of structure elements.
             $nodes = $xml->xpath("//structure[@name]|//section[@name]");
             foreach ($nodes as $node) {
                 if (isset($node['name'])) {
                     $nodeName = (string) $node['name'];
                     $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
                     $newTranslationUnit->setSource($nodeName);
                     $newTranslationUnit->addFlag('tao-public');
                     $translationUnits[$nodeName] = $newTranslationUnit;
                 }
             }
             // look up for "name" attributes of action elements.
             $nodes = $xml->xpath("//action[@name]|//tree[@name]");
             foreach ($nodes as $node) {
                 if (isset($node['name'])) {
                     $nodeName = (string) $node['name'];
                     $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
                     $newTranslationUnit->setSource($nodeName);
                     $newTranslationUnit->addFlag('tao-public');
                     $translationUnits[$nodeName] = $newTranslationUnit;
                 }
             }
             // look up for "description" elements.
             $nodes = $xml->xpath("//description");
             foreach ($nodes as $node) {
                 if ((string) $node != '') {
                     $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
                     $newTranslationUnit->setSource((string) $node);
                     $newTranslationUnit->addFlag('tao-public');
                     $translationUnits[(string) $node] = $newTranslationUnit;
                 }
             }
             // look up for "action" elements inside a toolbar
             $nodes = $xml->xpath("//toolbar/toolbaraction");
             foreach ($nodes as $node) {
                 $nodeValue = (string) $node;
                 if (trim($nodeValue) != '' && !array_key_exists($nodeValue, $translationUnits)) {
                     $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
                     $newTranslationUnit->setSource($nodeValue);
                     $newTranslationUnit->addFlag('tao-public');
                     $translationUnits[$nodeValue] = $newTranslationUnit;
                 }
             }
             // look up for the "title" attr of an "action" element inside a toolbar
             $nodes = $xml->xpath("//toolbar/toolbaraction[@title]");
             foreach ($nodes as $node) {
                 if (isset($node['title'])) {
                     $nodeTitle = (string) $node['title'];
                     $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
                     $newTranslationUnit->setSource($nodeTitle);
                     $newTranslationUnit->addFlag('tao-public');
                     $translationUnits[$nodeTitle] = $newTranslationUnit;
                 }
             }
             // look up for the "entrypoint" elements
             $nodes = $xml->xpath("//entrypoint");
             foreach ($nodes as $node) {
                 if (isset($node['title'])) {
                     $nodeTitle = (string) $node['title'];
                     $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
                     $newTranslationUnit->setSource($nodeTitle);
                     $newTranslationUnit->addFlag('tao-public');
                     $translationUnits[$nodeTitle] = $newTranslationUnit;
                 }
                 if (isset($node['label'])) {
                     $nodeLabel = (string) $node['label'];
                     $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
                     $newTranslationUnit->setSource($nodeLabel);
                     $newTranslationUnit->addFlag('tao-public');
                     $translationUnits[$nodeTitle] = $newTranslationUnit;
                 }
             }
         }
     }
     $this->setTranslationUnits(array_values($translationUnits));
 }