/** * Find all of the ts() calls * * @param array $tokens the array with tokens from token_get_all() * @param string $file the string containing the file name * @param Pot $pot * * @return void */ protected function findTsCalls($tokens, $file, Pot $pot) { // iterate through all the tokens while there's still a chance for // a ts() call while (count($tokens) > 3) { list($ctok, $par, $mid) = $tokens; // the first token has to be a T_STRING (with a function name) if (!is_array($ctok)) { array_shift($tokens); continue; } // check whether we're at ts( list($type, $string, $line) = $ctok; if ($type == T_STRING && ($string == 'ts' or $string == 't') && $par == '(') { switch ($this->tsCallType($tokens)) { case TS_CALL_TYPE_SINGLE: $pot->add(array('file' => $file, 'msgid' => $this->formatQuotedString($mid[1]), 'msgstr' => '')); break; case TS_CALL_TYPE_PLURAL: $pot->add(array('file' => $file, 'msgid' => $this->formatQuotedString($mid[1]), 'msgid_plural' => $this->formatQuotedString($this->getPluralString($tokens)), 'msgstr[0]' => '', 'msgstr[1]' => '')); break; case TS_CALL_TYPE_INVALID: $this->markerError($file, $line, 'ts', $tokens); break; default: break; } } array_shift($tokens); } }
/** * Rips gettext strings from $file and prints them in C format. * * @param string $file * @param string $content * @param Pot $pot */ public function parse($file, $content, Pot $pot) { if (empty($content)) { return; } // Match all calls to ts() // Note: \s also matches newlines with the 's' modifier. preg_match_all('~ [^\\w]ts\\s* # match "ts" with whitespace \\(\\s* # match "(" argument list start ((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\\s*\\+\\s*)?)+)\\s* [,\\)] # match ")" or "," to finish ~sx', $content, $matches); foreach ($matches[1] as $text) { if ($text = self::fs($text)) { $pot->add(array('file' => $file, 'msgid' => stripcslashes($text), 'msgstr' => '')); } } }
protected function execute(InputInterface $input, OutputInterface $output) { $defaults = array(); if ($input->getOption('msgctxt')) { $defaults['msgctxt'] = $input->getOption('msgctxt'); } $this->pot = new Pot($input->getOption('base'), array(), $defaults); $files = $input->getArgument('files'); if (in_array('-', $files)) { $files = array_merge($files, explode("\n", stream_get_contents($this->stdin))); } $actualFiles = $this->findFiles($files); if (!$input->getOption('out')) { foreach ($actualFiles as $file) { $this->extractFile($file); } if ($input->getOption('header')) { $output->write(file_get_contents($input->getOption('header'))); } $output->write($this->pot->toString($input)); } else { $progress = new ProgressHelper(); $progress->start($output, 1 + count($actualFiles)); $progress->advance(); foreach ($actualFiles as $file) { $this->extractFile($file); $progress->advance(); } $content = ''; ## If header is supplied and if we're starting a new file. if ($input->getOption('header')) { if (!file_exists($input->getOption('out')) || !$input->getOption('append')) { $content .= file_get_contents($input->getOption('header')); } } $content .= $this->pot->toString($input); file_put_contents($input->getOption('out'), $content, $input->getOption('append') ? FILE_APPEND : NULL); $progress->finish(); } }