示例#1
0
 /**
  * Short description of method write
  *
  * @access public
  * @author firstname and lastname of author, <*****@*****.**>
  * @return mixed
  */
 public function write()
 {
     $buffer = '';
     $file = $this->getTranslationFile();
     // Add PO Headers.
     $buffer .= 'msgid ""' . "\n";
     $buffer .= 'msgstr ""' . "\n";
     // If the TranslationFile is a specific POFile instance, we add PO Headers
     // to the output.
     if (get_class($this->getTranslationFile()) == 'tao_helpers_translation_POFile') {
         foreach ($file->getHeaders() as $name => $value) {
             $buffer .= '"' . $name . ': ' . $value . '\\n"' . "\n";
         }
     }
     // Write all Translation Units.
     $buffer .= "\n";
     foreach ($this->getTranslationFile()->getTranslationUnits() as $tu) {
         $c = tao_helpers_translation_POUtils::sanitize($tu->getContext(), true);
         $s = tao_helpers_translation_POUtils::sanitize($tu->getSource(), true);
         $t = tao_helpers_translation_POUtils::sanitize($tu->getTarget(), true);
         $a = tao_helpers_translation_POUtils::serializeAnnotations($tu->getAnnotations());
         if (!empty($a)) {
             $buffer .= "{$a}\n";
         }
         if ($c) {
             $buffer .= "msgctxt \"{$c}\"\n";
         }
         $buffer .= "msgid \"{$s}\"\n";
         $buffer .= "msgstr \"{$t}\"\n";
         $buffer .= "\n";
     }
     return file_put_contents($this->getFilePath(), $buffer);
 }
 public function testPOAnnotationsWriting()
 {
     // Test flag utilities.
     $comment = '';
     $this->assertEquals(tao_helpers_translation_POUtils::addFlag($comment, 'tao-public'), 'tao-public');
     $comment = 'no-error test-flag';
     $this->assertEquals(tao_helpers_translation_POUtils::addFlag($comment, 'tao-public'), 'no-error test-flag tao-public');
     $comment = 'foo bar code';
     $this->assertEquals(tao_helpers_translation_POUtils::addFlag($comment, 'bar '), 'foo bar code');
     // Test PO comments serialization.
     $annotations = array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => 'A single line translator comment.');
     $comment = '# A single line translator comment.';
     $this->assertEquals(tao_helpers_translation_POUtils::serializeAnnotations($annotations), $comment);
     $annotations = array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => "A multi line translator comment...\nWith a second line.", tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS => "An extracted comment.", tao_helpers_translation_POTranslationUnit::FLAGS => "tao-public foo-bar-code php-format");
     $comment = "# A multi line translator comment...\n# With a second line.\n#. An extracted comment.\n#, tao-public foo-bar-code php-format";
     $this->assertEquals(tao_helpers_translation_POUtils::serializeAnnotations($annotations), $comment);
     $annotations = array(tao_helpers_translation_POTranslationUnit::FLAGS => "tao-public");
     $comment = "#, tao-public";
     $this->assertEquals(tao_helpers_translation_POUtils::serializeAnnotations($annotations), $comment);
 }