public function testRDFUtils()
 {
     // Instantiate a new RDF language description.
     // Yoda English is a reversed form of english, spoken a long time ago, in a far away galaxy.
     $languageDescription = tao_helpers_translation_RDFUtils::createLanguageDescription('en-YO', 'Yoda English');
     $this->assertTrue(get_class($languageDescription) == 'DOMDocument');
     // Test with XPath if the resulting DOM tree is fine.
     $xPath = new DOMXPath($languageDescription);
     $xPath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
     $xPath->registerNamespace('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
     // Check for the Language en-YO resource.
     $result = $xPath->query("//rdf:Description[@rdf:about='http://www.tao.lu/Ontologies/TAO.rdf#Langen-YO']");
     $this->assertTrue($result->length == 1);
     // Check for the Language rdf:type.
     $result = $xPath->query("//rdf:Description/rdf:type[@rdf:resource='" . CLASS_LANGUAGES . "']");
     $this->assertTrue($result->length == 1);
     // Check for the Language rdfs:label.
     $result = $xPath->query("//rdf:Description/rdfs:label/text()");
     $this->assertTrue($result->length == 1);
     $this->assertTrue($result->item(0)->nodeValue == 'Yoda English');
     // Check for the Language rdf:value.
     $result = $xPath->query("//rdf:Description/rdf:value/text()");
     $this->assertTrue($result->length == 1);
     $this->assertTrue($result->item(0)->nodeValue == 'en-YO');
     $savePath = tempnam('/tmp', self::TEMP_RDF);
     $languageDescription->save($savePath);
     $this->assertTrue(file_exists($savePath));
     unlink($savePath);
 }
示例#2
0
 /**
  * Implementation of the 'create' action.
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @return void
  */
 public function actionCreate()
 {
     $extensionsToCreate = explode(',', $this->options['extension']);
     $extensionsToCreate = array_unique($extensionsToCreate);
     foreach ($extensionsToCreate as $etc) {
         $this->options['extension'] = $etc;
         $this->options['input'] = dirname(__FILE__) . '/../../' . $etc . '/' . self::DEF_INPUT_DIR;
         $this->options['output'] = dirname(__FILE__) . '/../../' . $etc . '/' . self::DEF_OUTPUT_DIR;
         $this->outVerbose("Creating language '" . $this->options['language'] . "' for extension '" . $this->options['extension'] . "' ...");
         // We first create the directory where locale files will go.
         $dir = $this->buildLanguagePath($this->options['extension'], $this->options['language']);
         $dirExists = false;
         if (file_exists($dir) && is_dir($dir) && $this->options['force'] == true) {
             $dirExists = true;
             $this->outVerbose("Language '" . $this->options['language'] . "' exists for extension '" . $this->options['extension'] . "'. Creation will be forced.");
             // Clean it up.
             foreach (scandir($dir) as $d) {
                 if ($d !== '.' && $d !== '..' && $d !== '.svn') {
                     if (!tao_helpers_File::remove($dir . '/' . $d, true)) {
                         $this->err("Unable to clean up 'language' directory '" . $dir . "'.", true);
                     }
                 }
             }
         } else {
             if (file_exists($dir) && is_dir($dir) && $this->options['force'] == false) {
                 $this->err("The 'language' " . $this->options['language'] . " already exists in the file system. Use the 'force' parameter to overwrite it.", true);
             }
         }
         // If we are still here... it means that we have to create the language directory.
         if (!$dirExists && !@mkdir($dir)) {
             $this->err("Unable to create 'language' directory '" . $this->options['language'] . "'.", true);
         } else {
             if ($this->options['build'] == true) {
                 $sortingMethod = tao_helpers_translation_TranslationFile::SORT_ASC_I;
                 $this->outVerbose("Building language '" . $this->options['language'] . "' for extension '" . $this->options['extension'] . "' ...");
                 // Let's populate the language with raw PO files containing sources but no targets.
                 // Source code extraction.
                 $fileExtensions = array('php', 'tpl', 'js', 'ejs');
                 $filePaths = array();
                 foreach (self::$WHITE_LIST as $subFolder) {
                     $filePaths[] = $this->options['input'] . DIRECTORY_SEPARATOR . $subFolder;
                 }
                 $sourceExtractor = new tao_helpers_translation_SourceCodeExtractor($filePaths, $fileExtensions);
                 $sourceExtractor->extract();
                 $translationFile = new tao_helpers_translation_POFile();
                 $translationFile->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage());
                 $translationFile->setTargetLanguage($this->options['language']);
                 $translationFile->addTranslationUnits($sourceExtractor->getTranslationUnits());
                 $file = MenuService::getStructuresFilePath($this->options['extension']);
                 if (!is_null($file)) {
                     $structureExtractor = new tao_helpers_translation_StructureExtractor(array($file));
                     $structureExtractor->extract();
                     $translationFile->addTranslationUnits($structureExtractor->getTranslationUnits());
                 }
                 $sortedTus = $translationFile->sortBySource($sortingMethod);
                 $sortedTranslationFile = new tao_helpers_translation_POFile();
                 $sortedTranslationFile->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage());
                 $sortedTranslationFile->setTargetLanguage($this->options['language']);
                 $sortedTranslationFile->addTranslationUnits($sortedTus);
                 $this->preparePOFile($sortedTranslationFile, true);
                 $poPath = $dir . '/' . self::DEF_PO_FILENAME;
                 $writer = new tao_helpers_translation_POFileWriter($poPath, $sortedTranslationFile);
                 $writer->write();
                 $this->outVerbose("PO Translation file '" . basename($poPath) . "' in '" . $this->options['language'] . "' created for extension '" . $this->options['extension'] . "'.");
                 $writer->write();
                 // Writing JS files
                 $jsPath = $dir . '/' . self::DEF_JS_FILENAME;
                 $writer = new tao_helpers_translation_JSFileWriter($jsPath, $sortedTranslationFile);
                 $writer->write(false);
                 $this->outVerbose("JavaScript Translation file '" . basename($jsPath) . "' in '" . $this->options['language'] . "' created for extension '" . $this->options['extension'] . "'.");
                 $writer->write();
                 // Now that PO files & JS files are created, we can create the translation models
                 // if we find RDF models to load for this extension.
                 $translatableProperties = array(RDFS_LABEL, RDFS_COMMENT);
                 foreach ($this->getOntologyFiles() as $f) {
                     common_Logger::d('reading rdf ' . $f);
                     $translationFile = $this->extractPoFileFromRDF($f, $translatableProperties);
                     $writer = new tao_helpers_translation_POFileWriter($dir . '/' . $this->getOntologyPOFileName($f), $translationFile);
                     $writer->write();
                     $this->outVerbose("PO Translation file '" . $this->getOntologyPOFileName($f) . "' in '" . $this->options['language'] . "' created for extension '" . $this->options['extension'] . "'.");
                 }
                 $this->outVerbose("Language '" . $this->options['language'] . "' created for extension '" . $this->options['extension'] . "'.");
             } else {
                 // Only build virgin files.
                 // (Like a virgin... woot !)
                 $translationFile = new tao_helpers_translation_POFile();
                 $translationFile->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage());
                 $translationFile->setTargetLanguage($this->options['language']);
                 $this->preparePOFile($translationFile, true);
                 foreach ($this->getOntologyFiles() as $f) {
                     common_Logger::d('reading rdf ' . $f);
                     $translationFile = new tao_helpers_translation_POFile();
                     $translationFile->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage());
                     $translationFile->setTargetLanguage($this->options['language']);
                     $translationFile->setExtensionId($this->options['extension']);
                     $writer = new tao_helpers_translation_POFileWriter($dir . '/' . $this->getOntologyPOFileName($f), $translationFile);
                     $writer->write();
                 }
                 $this->outVerbose("Language '" . $this->options['language'] . "' created for extension '" . $this->options['extension'] . "'.");
             }
             // Create the language manifest in RDF.
             if ($this->options['extension'] == 'tao') {
                 $langDescription = tao_helpers_translation_RDFUtils::createLanguageDescription($this->options['language'], $this->options['languageLabel']);
                 $langDescription->save($dir . '/' . self::DEF_LANG_FILENAME);
             }
         }
     }
 }