Пример #1
0
 /**
  *   Dump PoeditFile translated keys into key/value .json file
  *
  *   @param PoeditFile   $file
  *   @param string       $filename
  *
  *   @throws MissingFileLanguageException   if no language is set and we have plural forms
  *
  *   @return boolean
  */
 public function dump(PoeditFile $file, $filename = null)
 {
     $content = array();
     $pluralForms = null;
     $filename = null !== $filename ? $filename : $this->file;
     $strings = $file->getTranslated();
     foreach ($strings as $string) {
         if ($string instanceof PoeditPluralString) {
             // if we found a plural form, we need to look for file language
             if (null === $pluralForms) {
                 $pluralForms = $this->getLangPluralForm($file->getLang());
             }
             if (null === $file->getLang()) {
                 throw new MissingFileLanguageException('Your .po file must have a language defined in order to dump plural forms', 1);
             }
             $content[$string->getKey()] = array_combine($pluralForms, $string->getPlurals());
         } else {
             $content[$string->getKey()] = $string->getValue();
         }
     }
     $content = json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
     // fix some unwanted backslashes, auto-escaped by json_encode()
     $content = str_replace(array('\\\\n', '\\\\r', '\\\\t'), array('\\n', '\\r', '\\t'), $content);
     // ensure that path and file exists
     File::mkdirr(substr($filename, 0, strrpos($filename, '/')));
     return false !== file_put_contents($filename, $content);
 }
Пример #2
0
 /**
  *   Dump PoeditFile into .po file
  *
  *   @param PoeditFile   $file
  *   @param string       $filename
  *   @param boolean      $sort       if enabled, sort strings and their comments. implemented to avoid too many git conflicts
  *   @param string       $charset
  *
  *   @return boolean
  */
 public function dump(PoeditFile $file, $filename = null, $sort = false, $charset = 'UTF-8')
 {
     $filename = null !== $filename ? $filename : $this->file;
     $content = $file->getHeaders() . PHP_EOL;
     $content .= "\"Content-Type: text/plain; charset=" . $charset . "\\n\"" . PHP_EOL . PHP_EOL;
     $strings = true === $sort ? $file->sortStrings()->getStrings() : $file->getStrings();
     foreach ($strings as $string) {
         $content .= true === $sort ? $string->sortReferences()->sortComments()->sortExtracteds()->sortFlags() : $string;
     }
     // ensure that path and file exists
     File::mkdirr(substr($filename, 0, strrpos($filename, '/')));
     return false !== file_put_contents($filename, $content);
 }
Пример #3
0
 public function __construct(array $files, $output, array $keywords = array('_'), $parser = 'javascript', $enc = 'UTF-8', $cli = false)
 {
     $this->cli = $cli;
     $parser = 'Xgettext\\Parser\\' . ucfirst(strtolower($parser)) . 'Parser';
     if (empty($files)) {
         throw new InvalidArgumentException('You did not provide any input file.');
     }
     if (empty($output)) {
         throw new InvalidArgumentException('You did not provide any output file.');
     }
     $poeditFile = new PoeditFile();
     foreach ($files as $file) {
         try {
             $fileParser = new $parser($file, $keywords);
             $poeditFile->addStrings($fileParser->parse());
         } catch (Exception $e) {
             throw new InvalidArgumentException(sprintf('"%s" parser does not exist', $parser));
         }
     }
     $poeditDumper = new PoeditDumper($output);
     $poeditDumper->dump($poeditFile, null, false, $enc);
 }
Пример #4
0
 public function testStringsConflict()
 {
     $file = new PoeditFile();
     $file->addString(new String('foo', 'bar', array('comment1'), array('extracted1'), array('ref1'), array('flag1'), true));
     $file->addString(new String('foo', 'baz', array('comment2'), array('extracted2'), array('ref2'), array('flag2')));
     $this->assertEquals('bar', $file->getString('foo')->getValue());
     $this->assertEquals(array('comment1', 'comment2'), $file->getString('foo')->getComments());
     $this->assertEquals(array('extracted1', 'extracted2'), $file->getString('foo')->getExtracteds());
     $this->assertEquals(array('ref1', 'ref2'), $file->getString('foo')->getReferences());
     $this->assertEquals(array('flag1', 'flag2'), $file->getString('foo')->getFlags());
     $file->addString(new String('foo', 'baz', array('comment3'), array('extracted3'), array('ref3'), array('flag3')));
     $this->assertEquals('bar', $file->getString('foo')->getValue());
     $this->assertEquals(array('comment1', 'comment2', 'comment3'), $file->getString('foo')->getComments());
     $this->assertEquals(array('extracted1', 'extracted2', 'extracted3'), $file->getString('foo')->getExtracteds());
     $this->assertEquals(array('ref1', 'ref2', 'ref3'), $file->getString('foo')->getReferences());
     $this->assertEquals(array('flag1', 'flag2', 'flag3'), $file->getString('foo')->getFlags());
 }