/**
  * Test of the different classes composing the Translation Model.
  */
 public function testTranslationModel()
 {
     // en-US (American English) to en-YA (Yoda English) translation units.
     $tu1 = new tao_helpers_translation_TranslationUnit();
     $tu1->setSource('May the force be with you.');
     $tu1->setTarget('The force with you may be.');
     $tu2 = new tao_helpers_translation_TranslationUnit();
     $tu2->setSource('The dark side smells hate.');
     $tu2->setTarget('Hate the dark side smells.');
     $tu3 = new tao_helpers_translation_TranslationUnit();
     $tu3->setSource('Leia Organa of Alderaan is beautiful.');
     $tu3->setTarget('Beautiful Leia Organa of Alderaan is.');
     // Default source and target languages of translation units is en-US.
     $this->assertTrue($tu1->getSourceLanguage() == tao_helpers_translation_Utils::getDefaultLanguage());
     $this->assertTrue($tu2->getTargetLanguage() == tao_helpers_translation_Utils::getDefaultLanguage());
     $tu1->setSourceLanguage('en-US');
     $tu1->setTargetLanguage('en-YA');
     $tu2->setSourceLanguage('en-US');
     $tu2->setTargetLanguage('en-YA');
     $tu3->setSourceLanguage('en-US');
     $tu3->setTargetLanguage('en-YA');
     // Test source and target languages assignment at TranslationUnit level.
     $this->assertEquals('en-US', $tu2->getSourceLanguage());
     $this->assertEquals('en-YA', $tu3->getTargetLanguage());
     $tf = new tao_helpers_translation_TranslationFile();
     $tf->setSourceLanguage('en-US');
     $tf->setTargetLanguage('en-YA');
     $this->assertEquals($tf->getSourceLanguage(), 'en-US');
     $this->assertEquals($tf->getTargetLanguage(), 'en-YA');
     $tf->addTranslationUnit($tu1);
     $tf->addTranslationUnit($tu2);
     $tf->addTranslationUnit($tu3);
     $tus = $tf->getTranslationUnits();
     $this->assertTrue($tu1 == $tus[0]);
     $this->assertTrue($tu2 == $tus[1]);
     $this->assertTrue($tu3 == $tus[2]);
     $this->assertEquals('May the force be with you.', $tu1->getSource());
     $this->assertEquals('Hate the dark side smells.', $tu2->getTarget());
     $tu3->setSource('Lando Calrician is a great pilot.');
     $tu3->setTarget('A great pilot Lando Calrician is.');
     $this->assertEquals('Lando Calrician is a great pilot.', $tu3->getSource());
     $this->assertEquals('A great pilot Lando Calrician is.', $tu3->getTarget());
     $tu4 = new tao_helpers_translation_TranslationUnit();
     $tu4->setSource('There is another Skywalker.');
     $tu4->setTarget('Another Skywalker there is.');
     $tf->addTranslationUnit($tu4);
     $tus = $tf->getTranslationUnits();
     $tu4 = $tus[3];
     $this->assertEquals('en-YA', $tu4->getTargetLanguage());
     $newTu = new tao_helpers_translation_TranslationUnit();
     $newTu->setSource('Lando Calrician is a great pilot.');
     $newTu->setTarget('Han Solo is a great pilot.');
     $tf->addTranslationUnit($newTu);
     $tus = $tf->getTranslationUnits();
     $tu3 = $tus[2];
     $this->assertEquals(4, count($tus));
     $this->assertEquals('Lando Calrician is a great pilot.', $tu3->getSource());
     $this->assertEquals('Han Solo is a great pilot.', $tu3->getTarget());
     // Test Annotable implementation for translationUnit & translationFile.
     $tf = new tao_helpers_translation_TranslationFile();
     $this->assertTrue(is_array($tf->getAnnotations()), "Annotations for a newly instantiated translation file should be an array.");
     $this->assertTrue(count($tf->getAnnotations()) == 2);
     $tf->addAnnotation('context', 'Unit Testing');
     $tf->addAnnotation('author', 'Jane Doe');
     $this->assertTrue($tf->getAnnotation('context') == array('name' => 'context', 'value' => 'Unit Testing'));
     $this->assertTrue($tf->getAnnotation('author') == array('name' => 'author', 'value' => 'Jane Doe'));
     $this->assertEquals($tf->getAnnotations(), array('sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'context' => 'Unit Testing', 'author' => 'Jane Doe'));
     $tf->removeAnnotation('author');
     $this->assertTrue($tf->getAnnotation('author') == null);
     $this->assertEquals($tf->getAnnotations(), array('sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'context' => 'Unit Testing'));
     $tu = new tao_helpers_translation_TranslationUnit('test', 'test');
     $this->assertTrue(is_array($tu->getAnnotations()), "Annotations for a newly instantiated translation unit should be an array.");
     $this->assertTrue(count($tu->getAnnotations()) == 2);
     $tu->addAnnotation('context', 'Unit Testing');
     $tu->addAnnotation('author', 'Jane Doe');
     $this->assertTrue($tu->getAnnotation('context') == array('name' => 'context', 'value' => 'Unit Testing'));
     $this->assertTrue($tu->getAnnotation('author') == array('name' => 'author', 'value' => 'Jane Doe'));
     $this->assertEquals($tu->getAnnotations(), array('sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'context' => 'Unit Testing', 'author' => 'Jane Doe'));
     $tu->removeAnnotation('author');
     $this->assertTrue($tu->getAnnotation('author') == null);
     $this->assertEquals($tu->getAnnotations(), array('sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'context' => 'Unit Testing'));
     // Test utils.
     $this->assertEquals(tao_helpers_translation_Utils::getDefaultLanguage(), 'en-US');
 }
Ejemplo 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.");
     }
 }