public function testRDFTranslationWriting() { $rdfFilePath = tempnam('/tmp', self::TEMP_RDF); $rdfFile = new tao_helpers_translation_RDFTranslationFile('en-US', 'multiple'); $rdfFile->setBase('http://www.tao.lu/Ontologies/TAO.rdf#'); $writer = new tao_helpers_translation_RDFFileWriter($rdfFilePath, $rdfFile); $tu1 = new tao_helpers_translation_RDFTranslationUnit("This is a test"); $tu1->setSourceLanguage('en-US'); $tu1->setTargetLanguage('fr-FR'); $tu1->setSubject('http://www.tao.lu#target1'); $tu1->setPredicate('http://www.w3.org/2000/01/rdf-schema#label'); $rdfFile->addTranslationUnit($tu1); $writer->write(); $this->assertTrue(file_exists($rdfFilePath)); unlink($rdfFilePath); }
/** * @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_RDFTranslationUnit(); $tu->setSource($source); $tu->setTarget($target); $tu->setSourceLanguage($sourceLanguage); $tu->setTargetLanguage($targetLanguage); $tu->setSubject($about); $tu->setPredicate($child->namespaceURI . $child->localName); $tus[] = $tu; return $tus; } return $tus; }
/** * Short description of method read * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @return mixed */ public function read() { $translationUnits = array(); try { $translationFile = $this->getTranslationFile(); } catch (tao_helpers_translation_TranslationException $e) { $translationFile = new tao_helpers_translation_RDFTranslationFile(); } $this->setTranslationFile($translationFile); $inputFile = $this->getFilePath(); if (file_exists($inputFile)) { if (is_file($inputFile)) { if (is_readable($inputFile)) { try { $doc = new DOMDocument('1.0', 'UTF-8'); $doc->load($inputFile); $xpath = new DOMXPath($doc); $rdfNS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; $xmlNS = 'http://www.w3.org/XML/1998/namespace'; $xpath->registerNamespace('rdf', $rdfNS); $rootNodes = $xpath->query('//rdf:RDF'); if ($rootNodes->length == 1) { // Try to get annotations from the root node. $sibling = $rootNodes->item(0)->previousSibling; while ($sibling !== null) { if ($sibling instanceof DOMNode && $sibling->nodeType == XML_COMMENT_NODE) { $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations($sibling->data); $translationFile->setAnnotations($annotations); if (isset($annotations['sourceLanguage'])) { $translationFile->setSourceLanguage($annotations['sourceLanguage']); } if (isset($annotations['targetLanguage'])) { $translationFile->setTargetLanguage($annotations['targetLanguage']); } break; } $sibling = $sibling->previousSibling; } $descriptions = $xpath->query('//rdf:Description'); foreach ($descriptions as $description) { if ($description->hasAttributeNS($rdfNS, 'about')) { $subject = $description->getAttributeNS($rdfNS, 'about'); // Let's retrieve properties. foreach ($description->childNodes as $property) { if ($property->nodeType == XML_ELEMENT_NODE) { // Retrieve namespace uri of this node. if ($property->namespaceURI != null) { $predicate = $property->namespaceURI . $property->localName; // Retrieve an hypothetic target language. $lang = tao_helpers_translation_Utils::getDefaultLanguage(); if ($property->hasAttributeNS($xmlNS, 'lang')) { $lang = $property->getAttributeNS($xmlNS, 'lang'); } $object = $property->nodeValue; $tu = new tao_helpers_translation_RDFTranslationUnit(''); $tu->setTargetLanguage($lang); $tu->setTarget($object); $tu->setSubject($subject); $tu->setPredicate($predicate); // Try to get annotations. $sibling = $property->previousSibling; while ($sibling !== null) { if ($sibling instanceof DOMNode && $sibling->nodeType == XML_COMMENT_NODE) { // We should have the annotations we are looking for. $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations($sibling->data); $tu->setAnnotations($annotations); // Set the found sources and sourcelanguages if found. if (isset($annotations['source'])) { $tu->setSource($annotations['source']); } } $sibling = $sibling->previousSibling; } $translationUnits[] = $tu; } } } } } $this->getTranslationFile()->addTranslationUnits($translationUnits); } else { throw new tao_helpers_translation_TranslationException("'{$inputFile}' has no rdf:RDF root node or more than one rdf:RDF root node."); } } catch (DOMException $e) { throw new tao_helpers_translation_TranslationException("'{$inputFile}' cannot be parsed."); } } else { throw new tao_helpers_translation_TranslationException("'{$inputFile}' cannot be read. Check your system permissions."); } } else { throw new tao_helpers_translation_TranslationException("'{$inputFile}' is not a file."); } } else { throw new tao_helpers_translation_TranslationException("The file '{$inputFile}' does not exist."); } }