/** * 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'); }
/** * Adds a TranslationUnit instance to the file. It is appenned at the end of * collection. * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @param TranslationUnit translationUnit * @return mixed */ public function addTranslationUnit(tao_helpers_translation_TranslationUnit $translationUnit) { // If the translation unit exists, we replace the target with the new one if it exists. foreach ($this->getTranslationUnits() as $tu) { if ($tu->getSource() == $translationUnit->getSource()) { // If we are here, it means that this TU is being overriden by // another one having the same source... // // Let's make sure we don't override the existing one with an empty target! if ($translationUnit->getTarget() !== '') { $tu->setTarget($translationUnit->getTarget()); $tu->setAnnotations($translationUnit->getAnnotations()); } return; } } // If we are here, it means that this TU does not exist. $translationUnit->setSourceLanguage($this->getSourceLanguage()); $translationUnit->setTargetLanguage($this->getTargetLanguage()); array_push($this->translationUnits, $translationUnit); }
/** * Adds a TranslationUnit instance to the file. It is appenned at the end of * collection. * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @param tao_helpers_translation_TranslationUnit $translationUnit * @return mixed */ public function addTranslationUnit(tao_helpers_translation_TranslationUnit $translationUnit) { // If the translation unit exists, we replace the target with the new one if it exists. // also now we take care about context /** @var tao_helpers_translation_TranslationUnit $tu */ foreach ($this->getTranslationUnits() as $tu) { if ($tu->getSource() == $translationUnit->getSource() && (!$translationUnit->getContext() || $tu->getContext() == $translationUnit->getContext())) { $tu->setTarget($translationUnit->getTarget()); $tu->setAnnotations($translationUnit->getAnnotations()); return; } } // If we are here, it means that this TU does not exist. $translationUnit->setSourceLanguage($this->getSourceLanguage()); $translationUnit->setTargetLanguage($this->getTargetLanguage()); $tus = $this->getTranslationUnits(); array_push($tus, $translationUnit); $this->setTranslationUnits($tus); }