/**
  * Short description of method getTranslationsInFile
  *
  * @access private
  * @author firstname and lastname of author, <*****@*****.**>
  * @param  string filePath
  * @return mixed
  */
 private function getTranslationsInFile($filePath)
 {
     // File extension ?
     $extOk = false;
     foreach ($this->getFileTypes() as $exp) {
         if (@preg_match("/\\.{$exp}\$/", $filePath)) {
             $extOk = true;
             break;
         }
     }
     if ($extOk) {
         // We read the file.
         $lines = file($filePath);
         foreach ($lines as $line) {
             $strings = array();
             $patternMatch1 = array();
             $patternMatch2 = array();
             preg_match_all("/__\\(['\"](.*?)['\"]\\)/u", $line, $patternMatch1);
             preg_match_all("/\\{\\{__ ['\"](.*?)['\"]\\}\\}/u", $line, $patternMatch2);
             if (!empty($patternMatch1[1])) {
                 $strings = $patternMatch1[1];
             }
             if (!empty($patternMatch2[1])) {
                 $strings = array_merge($strings, $patternMatch2[1]);
             }
             //preg_match_all("/__(\(| )+['\"](.*?)['\"](\))?/u", $line, $string);
             //lookup for __('to translate') or __ 'to translate'
             //    preg_match_all("/__(\(| )+(('(.*?)')|(\"(.*?)\"))(\))?/u", $line, $string);
             foreach ($strings as $s) {
                 $tu = new tao_helpers_translation_TranslationUnit();
                 $tu->setSource(tao_helpers_translation_POUtils::sanitize($s));
                 $tus = $this->getTranslationUnits();
                 $found = false;
                 // We must add the string as a new TranslationUnit only
                 // if a similiar source does not exist.
                 foreach ($tus as $t) {
                     if ($tu->getSource() == $t->getSource()) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     array_push($tus, $tu);
                     $this->setTranslationUnits($tus);
                 }
             }
         }
     }
 }
Esempio n. 2
0
 /**
  * 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);
 }
 /**
  * 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');
 }
Esempio n. 4
0
 /**
  * Checks whether or not a given TranslationUnit has the same target
  * than the current instance.
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  * @param  TranslationUnit translationUnit
  * @return boolean
  */
 public function hasSameTranslationUnitTargetLanguage(tao_helpers_translation_TranslationUnit $translationUnit)
 {
     $returnValue = (bool) false;
     $returnValue = $this->getTargetLanguage() == $translationUnit->getTargetLanguage();
     return (bool) $returnValue;
 }
Esempio n. 5
0
 /**
  * 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);
 }
 /**
  * Short description of method setSource
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  * @param  string source
  * @return mixed
  */
 public function setSource($source)
 {
     parent::setSource($source);
     $this->addAnnotation('source', $source);
 }
 /**
  * Short description of method getTranslationsInFile
  *
  * @access private
  * @author firstname and lastname of author, <*****@*****.**>
  * @param  string $filePath
  */
 private function getTranslationsInFile($filePath)
 {
     // File extension ?
     $extOk = in_array(\Jig\Utils\FsUtils::getFileExtension($filePath), $this->getFileTypes());
     if ($extOk) {
         foreach ($this->getBannedFileType() as $bannedExt) {
             $extOk &= substr_compare($filePath, $bannedExt, strlen($filePath) - strlen($bannedExt), strlen($bannedExt)) !== 0;
         }
     }
     if ($extOk) {
         // We read the file.
         $lines = file($filePath);
         foreach ($lines as $line) {
             $strings = $this->getTranslationPhrases($line);
             //preg_match_all("/__(\(| )+['\"](.*?)['\"](\))?/u", $line, $string);
             //lookup for __('to translate') or __ 'to translate'
             //    preg_match_all("/__(\(| )+(('(.*?)')|(\"(.*?)\"))(\))?/u", $line, $string);
             foreach ($strings as $s) {
                 $tu = new tao_helpers_translation_TranslationUnit();
                 $tu->setSource(tao_helpers_translation_POUtils::sanitize($s));
                 $tus = $this->getTranslationUnits();
                 $found = false;
                 // We must add the string as a new TranslationUnit only
                 // if a similiar source does not exist.
                 foreach ($tus as $t) {
                     if ($tu->getSource() == $t->getSource()) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     $tus[] = $tu;
                     $this->setTranslationUnits($tus);
                 }
             }
         }
     }
 }