public function testSourceExtraction()
 {
     // Test with only PHP Actions.
     $sourceCodePaths = array(dirname(__FILE__) . self::FAKE_ACTIONS);
     $extensions = array('php');
     $extractor = new tao_helpers_translation_SourceCodeExtractor($sourceCodePaths, $extensions);
     $extractor->extract();
     $tus = $extractor->getTranslationUnits();
     $this->assertEquals(count($tus), 21);
     // Complete test.
     $extensions = array('php', 'tpl', 'js');
     $sourceCodePaths = array(dirname(__FILE__) . self::FAKE_ACTIONS, dirname(__FILE__) . self::FAKE_VIEWS);
     $extractor->setFileTypes($extensions);
     $extractor->setPaths($sourceCodePaths);
     $extractor->extract();
     $tus = $extractor->getTranslationUnits();
     $this->assertEquals(count($tus), 60);
     $this->assertEquals('Import', $tus[1]->getSource());
     $this->assertEquals(' Please select the input data format to import ', $tus[2]->getSource());
     $this->assertEquals('Please upload a CSV file formated as "defined" %min by %max the options above.', $tus[5]->getSource());
     $this->assertEquals("Please upload \t an RDF file.\n\n", $tus[8]->getsource());
 }
Exemplo n.º 2
0
 /**
  * Implementation of the 'update' action.
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @return void
  */
 public function actionUpdate()
 {
     $this->outVerbose("Updating language '" . $this->options['language'] . "' for extension '" . $this->options['extension'] . "'...");
     $sortingMethod = tao_helpers_translation_TranslationFile::SORT_ASC_I;
     // Get virgin translations from the source code and manifest.
     $filePaths = array();
     foreach (self::$WHITE_LIST as $subFolder) {
         $filePaths[] = $this->options['input'] . DIRECTORY_SEPARATOR . $subFolder;
     }
     $extensions = array('php', 'tpl', 'js', 'ejs');
     $sourceCodeExtractor = new tao_helpers_translation_SourceCodeExtractor($filePaths, $extensions);
     $sourceCodeExtractor->extract();
     $translationFile = new tao_helpers_translation_POFile();
     $translationFile->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage());
     $translationFile->setTargetLanguage($this->options['language']);
     $translationFile->addTranslationUnits($sourceCodeExtractor->getTranslationUnits());
     $file = MenuService::getStructuresFilePath($this->options['extension']);
     if (!is_null($file)) {
         $structureExtractor = new tao_helpers_translation_StructureExtractor(array($file));
         $structureExtractor->extract();
         $structureUnits = $structureExtractor->getTranslationUnits();
         $this->outVerbose(count($structureUnits) . ' units extracted from structures.xml.');
         $translationFile->addTranslationUnits($structureUnits);
     }
     // For each TU that was recovered, have a look in an older version
     // of the translations.
     $oldFilePath = $this->buildLanguagePath($this->options['extension'], $this->options['language']) . '/' . self::DEF_PO_FILENAME;
     $translationFileReader = new tao_helpers_translation_POFileReader($oldFilePath);
     $translationFileReader->read();
     $oldTranslationFile = $translationFileReader->getTranslationFile();
     foreach ($oldTranslationFile->getTranslationUnits() as $oldTu) {
         if (($newTu = $translationFile->getBySource($oldTu)) !== null && $oldTu->getTarget() != '') {
             // No duplicates in TFs so I simply add it whatever happens.
             // If it already has the same one, it means we will update it.
             $newTu->setTarget($oldTu->getTarget());
         }
     }
     $sortedTranslationFile = new tao_helpers_translation_POFile();
     $sortedTranslationFile->setSourceLanguage($translationFile->getSourceLanguage());
     $sortedTranslationFile->setTargetLanguage($translationFile->getTargetLanguage());
     $sortedTranslationFile->addTranslationUnits($translationFile->sortBySource($sortingMethod));
     $this->preparePOFile($sortedTranslationFile, true);
     // Write the new ones.
     $poFileWriter = new tao_helpers_translation_POFileWriter($oldFilePath, $sortedTranslationFile);
     $poFileWriter->write();
     $this->outVerbose("PO translation file '" . basename($oldFilePath) . "' in '" . $this->options['language'] . "' updated for extension '" . $this->options['extension'] . "'.");
     $translatableProperties = array(RDFS_LABEL, RDFS_COMMENT);
     // We now deal with RDF models.
     foreach ($this->getOntologyFiles() as $f) {
         // Loop on 'master' models.
         $translationFile = $this->extractPoFileFromRDF($f, $translatableProperties);
         // The slave RDF file is the translation of the ontology that we find in /extId/Locales/langCode.
         $slavePOFilePath = $this->buildLanguagePath($this->options['extension'], $this->options['language']) . '/' . $this->getOntologyPOFileName($f);
         if (file_exists($slavePOFilePath)) {
             // Read the existing RDF Translation file for this RDF model.
             $poReader = new tao_helpers_translation_POFileReader($slavePOFilePath);
             $poReader->read();
             $slavePOFile = $poReader->getTranslationFile();
             // Try to update translation units found in the master PO file with
             // targets found in the old translation of the ontology.
             foreach ($slavePOFile->getTranslationUnits() as $oTu) {
                 $translationFile->addTranslationUnit($oTu);
             }
             // Remove Slave PO file. It will be overwritten by the modified Master PO file.
             tao_helpers_File::remove($slavePOFilePath);
         }
         // Write Master PO file as the new Slave PO file.
         $rdfWriter = new tao_helpers_translation_POFileWriter($slavePOFilePath, $translationFile);
         $rdfWriter->write();
         $this->outVerbose("Translation model {$this->getOntologyPOFileName($f)}  in '" . $this->options['language'] . "' updated for extension '" . $this->options['extension'] . "'.");
     }
     $this->outVerbose("Language '" . $this->options['language'] . "' updated for extension '" . $this->options['extension'] . "'.");
 }