Пример #1
0
 /**
  * Short description of method read
  *
  * @access public
  * @author firstname and lastname of author, <*****@*****.**>
  * @throws tao_helpers_translation_TranslationException
  * @return mixed
  */
 public function read()
 {
     $file = $this->getFilePath();
     if (!file_exists($file)) {
         throw new tao_helpers_translation_TranslationException("The translation file '{$file}' does not exist.");
     }
     // Create the translation file.
     $tf = new tao_helpers_translation_POFile();
     $fc = implode('', file($file));
     $matched = preg_match_all('/((?:#[\\.:,\\|]{0,1}\\s+(?:.*?)\\n)*)' . '(msgctxt\\s+(?:"(?:[^"]|\\\\")*?"\\s*)+)?' . '(msgid\\s+(?:"(?:[^"]|\\\\")*?"\\s*)+)\\s+' . '(msgstr\\s+(?:"(?:[^"]|\\\\")*?(?<!\\\\)"\\s*)+)/', $fc, $matches);
     preg_match('/sourceLanguage: (.*?)\\n/s', $fc, $sourceLanguage);
     preg_match('/targetLanguage: (.*?)\\n/s', $fc, $targetLanguage);
     if (count($sourceLanguage)) {
         $tf->setSourceLanguage(substr($sourceLanguage[1], 0, 5));
     }
     if (count($targetLanguage)) {
         $tf->setTargetLanguage(substr($targetLanguage[1], 0, 5));
     }
     if ($matched) {
         for ($i = 0; $i < $matched; $i++) {
             $annotations = $matches[1][$i];
             $msgctxt = preg_replace('/\\s*msgctxt\\s*"(.*)"\\s*/s', '\\1', $matches[2][$i]);
             $msgid = preg_replace('/\\s*msgid\\s*"(.*)"\\s*/s', '\\1', $matches[3][$i]);
             $msgstr = preg_replace('/\\s*msgstr\\s*"(.*)"\\s*/s', '\\1', $matches[4][$i]);
             // Do not include meta data as a translation unit..
             if ($msgid !== '') {
                 // Sanitze the strings.
                 $msgid = tao_helpers_translation_POUtils::sanitize($msgid);
                 $msgstr = tao_helpers_translation_POUtils::sanitize($msgstr);
                 $msgctxt = tao_helpers_translation_POUtils::sanitize($msgctxt);
                 $tu = new tao_helpers_translation_POTranslationUnit();
                 // Set up source & target.
                 $tu->setSource($msgid);
                 if ($msgstr !== '') {
                     $tu->setTarget($msgstr);
                 }
                 if ($msgctxt) {
                     $tu->setContext($msgctxt);
                 }
                 // Deal with annotations
                 $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($annotations);
                 foreach ($annotations as $name => $value) {
                     $tu->addAnnotation($name, $value);
                 }
                 $tf->addTranslationUnit($tu);
             }
         }
     }
     $this->setTranslationFile($tf);
 }
 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);
 }