public function testPOAnnotationsReading()
 {
     $string = "# This is a comment.\n";
     $string .= "#, flag1 composed-flag flag2";
     $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string);
     $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => 'This is a comment.', tao_helpers_translation_POTranslationUnit::FLAGS => 'flag1 composed-flag flag2'));
     $string = "# The first line of my comment continues...\n";
     $string .= "# At the second line.\n";
     $string .= "#. Please do not touch this!\n";
     $string .= "#| msgctxt A previous testing context.\n";
     $string .= "#|  msgid previous-untranslated-string-singular\n";
     $string .= "#| msgid_plural previous-untranslated-string-plural\n";
     $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string);
     $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => "The first line of my comment continues...\nAt the second line.", tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS => "Please do not touch this!", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT => "A previous testing context.", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID => "previous-untranslated-string-singular", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID_PLURAL => "previous-untranslated-string-plural"));
     $string = "# هذا تعليق\n";
     $string .= "# مع خطوط متعددة في الداخل.\n";
     $string .= "#. لا تغير من فضلك!\n";
     $string .= "#| msgctxt السابقة السياق.";
     $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string);
     $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => "هذا تعليق\nمع خطوط متعددة في الداخل.", tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS => "لا تغير من فضلك!", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT => "السابقة السياق."));
     $string = "^ This should not w#ork but the next...\n";
     $string .= "#, flag-read";
     $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string);
     $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::FLAGS => 'flag-read'));
     $string = "";
     $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string);
     $this->assertEquals($annotations, array());
     $reader = new tao_helpers_translation_POFileReader(dirname(__FILE__) . self::ANNOTATIONS_PO);
     $reader->read();
     $tf = $reader->getTranslationFile();
     $tus = $tf->getTranslationUnits();
     $this->assertEquals(count($tus), 6);
     $this->assertEquals($tus[0]->getAnnotations(), array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => 'This is a comment', 'sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage()));
     $this->assertEquals($tus[1]->getAnnotations(), array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => 'This is another comment', tao_helpers_translation_POTranslationUnit::FLAGS => 'flag1 composed-flag flag2 tao-public', 'sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage()));
     $this->assertEquals($tus[2]->getAnnotations(), array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => "This is a multiline...\ncomment.", 'sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage()));
     $this->assertEquals($tus[3]->getAnnotations(), array('sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage()));
     // Test flag related interface on POTranslationUnit & POTranslationFile.
     $this->assertTrue($tus[5]->hasFlag('flag4'));
     $this->assertEquals($tus[5]->getFlags(), array('flag4'));
     $tus[5]->addFlag('new-flag');
     $this->assertTrue($tus[5]->hasFlag('new-flag'));
     $this->assertEquals($tus[5]->getFlags(), array('flag4', 'new-flag'));
     $tus[5]->addFlag('new-flag');
     $this->assertEquals($tus[5]->getFlags(), array('flag4', 'new-flag'));
     $tus[5]->addFlag('flag5');
     $this->assertEquals($tus[5]->getAnnotation(tao_helpers_translation_POTranslationUnit::FLAGS), array('name' => tao_helpers_translation_POTranslationUnit::FLAGS, 'value' => 'flag4 new-flag flag5'));
     $tus[5]->removeFlag('new-flag');
     $this->assertEquals($tus[5]->getFlags(), array('flag4', 'flag5'));
     $flagTus = $tf->getByFlag('composed-flag');
     $this->assertEquals(count($flagTus), 2);
     $this->assertEquals($flagTus[0]->getSource(), "Thïs téxt cöntàin\$ wéîRd chárâctêrS beçÁuse öf I18N");
     $this->assertEquals($flagTus[1]->getSource(), "This one contains the same flag as the second one");
     $flagTus = $tf->getByFlags(array('composed-flag', 'flag2'));
     $this->assertEquals(count($flagTus), 2);
     $this->assertEquals($flagTus[0]->getSource(), "Thïs téxt cöntàin\$ wéîRd chárâctêrS beçÁuse öf I18N");
     $this->assertEquals($flagTus[1]->getSource(), "This one contains the same flag as the second one");
     // Reload the file.
     // We will check if when the file is written again, we get the same result.
     // In other words, we check idempotency after read/write.
     // We will compare TranslationFiles $tf1 & $tf2.
     $reader->read();
     $tf1 = $reader->getTranslationFile();
     // We write $tf1.
     $path = tempnam('/tmp', self::TEMP_PO);
     $writer = new tao_helpers_translation_POFileWriter($path, $tf1);
     $writer->write();
     // We read $tf2 to be compared with $tf1
     $reader->setFilePath($path);
     $reader->read();
     $tf2 = $reader->getTranslationFile();
     $this->assertEquals($tf1->count(), 6);
     $this->assertEquals($tf2->count(), 6);
     $tus1 = $tf1->getTranslationUnits();
     $tus2 = $tf2->getTranslationUnits();
     $this->assertEquals($tus1[0]->getAnnotations(), $tus2[0]->getAnnotations());
     $this->assertEquals($tus1[1]->getAnnotations(), $tus2[1]->getAnnotations());
     $this->assertEquals($tus1[2]->getAnnotations(), $tus2[2]->getAnnotations());
     $this->assertEquals($tus1[3]->getAnnotations(), $tus2[3]->getAnnotations());
     $this->assertEquals($tus1[4]->getAnnotations(), $tus2[4]->getAnnotations());
     $this->assertEquals($tus1[5]->getAnnotations(), $tus2[5]->getAnnotations());
     unlink($path);
 }
Пример #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'] . "'.");
 }