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);
 }
Esempio n. 2
0
 /**
  * Implementation of the 'compile' action.
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @return void
  */
 public function actionCompile()
 {
     $extensionsToCreate = explode(',', $this->options['extension']);
     $extensionsToCreate = array_unique($extensionsToCreate);
     foreach ($extensionsToCreate as $extension) {
         $language = $this->options['language'];
         $compiledTranslationFile = new tao_helpers_translation_TranslationFile();
         $compiledTranslationFile->setTargetLanguage($this->options['language']);
         $this->outVerbose("Compiling language '{$language}' for extension '{$extension}'...");
         // Get the dependencies of the target extension.
         // @todo Deal with dependencies at compilation time.
         $dependencies = array();
         if ($extension !== 'tao') {
             $dependencies[] = 'tao';
         }
         $this->outVerbose("Resolving Dependencies...");
         foreach ($dependencies as $depExtId) {
             $this->outVerbose("Adding messages from extension '{$depExtId}' in '{$language}'...");
             // Does the locale exist for $depExtId?
             $depPath = $this->buildLanguagePath($depExtId, $language) . '/' . self::DEF_PO_FILENAME;
             if (!file_exists($depPath) || !is_readable($depPath)) {
                 $this->outVerbose("Dependency on extension '{$depExtId}' in '{$language}' does not exist. Trying to resolve default language...");
                 $depPath = $this->buildLanguagePath($depExtId, tao_helpers_translation_Utils::getDefaultLanguage() . '/' . self::DEF_PO_FILENAME);
                 if (!file_exists($depPath) || !is_readable($depPath)) {
                     $this->outVerbose("Dependency on extension '{$depExtId}' in '{$language}' does not exist.");
                     continue;
                 }
             }
             // Recompile the dependent extension (for the moment 'tao' meta-extension only).
             $oldVerbose = $this->options['verbose'];
             $this->parameters['verbose'] = false;
             $this->options['extension'] = $depExtId;
             $this->actionCompile();
             $this->options['extension'] = $extension;
             $this->parameters['verbose'] = $oldVerbose;
             $poFileReader = new tao_helpers_translation_POFileReader($depPath);
             $poFileReader->read();
             $poFile = $poFileReader->getTranslationFile();
             $poCount = $poFile->count();
             $compiledTranslationFile->addTranslationUnits($poFile->getTranslationUnits());
             $this->outVerbose("{$poCount} messages added.");
         }
         if (($extDirectories = scandir(ROOT_PATH)) !== false) {
             // Get all public messages accross extensions.
             foreach ($extDirectories as $extDir) {
                 $extPath = ROOT_PATH . '/' . $extDir;
                 if (is_dir($extPath) && is_readable($extPath) && $extDir[0] != '.' && !in_array($extDir, $dependencies) && $extDir != $extension && $extDir != 'generis') {
                     $this->outVerbose("Adding public messages from extension '{$extDir}' in '{$language}'...");
                     $poPath = $this->buildLanguagePath($extDir, $language) . '/' . self::DEF_PO_FILENAME;
                     if (!file_exists($poPath) || !is_readable($poPath)) {
                         $this->outVerbose("Extension '{$extDir}' is not translated in language '{$language}'. Trying to retrieve default language...");
                         $poPath = $this->buildLanguagePath($extDir, tao_helpers_translation_Utils::getDefaultLanguage()) . '/' . self::DEF_PO_FILENAME;
                         if (!file_exists($poPath) || !is_readable($poPath)) {
                             $this->outVerbose("Extension '{$extDir}' in '{$language}' does not exist.");
                             continue;
                         }
                     }
                     $poFileReader = new tao_helpers_translation_POFileReader($poPath);
                     $poFileReader->read();
                     $poFile = $poFileReader->getTranslationFile();
                     $poUnits = $poFile->getByFlag('tao-public');
                     $poCount = count($poUnits);
                     $compiledTranslationFile->addTranslationUnits($poUnits);
                     $this->outVerbose("{$poCount} public messages added.");
                 }
             }
             // Finally, add the translation units related to the target extension.
             $path = $this->buildLanguagePath($extension, $language) . '/' . self::DEF_PO_FILENAME;
             if (file_exists($path) && is_readable($path)) {
                 $poFileReader = new tao_helpers_translation_POFileReader($path);
                 $poFileReader->read();
                 $poFile = $poFileReader->getTranslationFile();
                 $compiledTranslationFile->addTranslationUnits($poFile->getTranslationUnits());
                 // Sort the TranslationUnits.
                 $sortingMethod = tao_helpers_translation_TranslationFile::SORT_ASC_I;
                 $compiledTranslationFile->setTranslationUnits($compiledTranslationFile->sortBySource($sortingMethod));
                 $phpPath = $this->buildLanguagePath($extension, $language) . '/' . self::DEF_PHP_FILENAME;
                 $phpFileWriter = new tao_helpers_translation_PHPFileWriter($phpPath, $compiledTranslationFile);
                 $phpFileWriter->write();
                 $this->outVerbose("PHP compiled translations for extension '{$extension}' with '{$language}' written.");
                 $jsPath = $this->buildLanguagePath($extension, $language) . '/' . self::DEF_JS_FILENAME;
                 $jsFileWriter = new tao_helpers_translation_JSFileWriter($jsPath, $compiledTranslationFile);
                 $jsFileWriter->write();
                 $this->outVerbose("JavaScript compiled translations for extension '{$extension}' with '{$language}' written.");
             } else {
                 $this->err("PO file '{$path}' for extension '{$extension}' with language '{$language}' cannot be read.", true);
             }
         } else {
             $this->err("Cannot list TAO Extensions from root path. Check your system rights.", true);
         }
         $this->outVerbose("Translations for '{$extension}' with language '{$language}' gracefully compiled.");
     }
 }