/** * Inspect the supplied source, capture gettext references as a PoFile object. * * @param string $source php source code * @param string $refname source identification used for PO reference comments * * @return PoFile */ public function msginitString($source, $refname) { if (!$this->poFile instanceof PoFile) { $this->poFile = new PoFile(); } $tokens = token_get_all($source); $translateTags = array_merge($this->gettextTags, $this->pgettextTags, $this->ngettextTags); $commentText = null; $commentLine = -10; $tokenCount = count($tokens); $i = 0; while ($i < $tokenCount) { $token = $tokens[$i]; if (is_array($token) && $token[0] == T_STRING && in_array($token[1], $translateTags)) { $entry = new PoEntry(); $gtt = array(); list(, $text, $line) = $token; $entry->add(PoTokens::REFERENCE, $refname . ':' . $line); $gtt['line'] = $line; $gtt['function'] = $text; $gtt['args'] = array(); $la = 1; while (is_array($tokens[$i + $la]) && $tokens[$i + $la][0] == T_WHITESPACE) { $la++; } if ($tokens[$i + $la] == '(') { while (')' != ($token = $tokens[$i + $la]) && $la < 10) { if (is_array($token) && ($token[0] == T_CONSTANT_ENCAPSED_STRING || $token[0] == T_ENCAPSED_AND_WHITESPACE)) { list(, $text, $line) = $token; $gtt['args'][] = $text; } $la++; } if (count($gtt['args'])) { if (in_array($gtt['function'], $this->gettextTags)) { $entry->set(PoTokens::MESSAGE, $this->escapeForPo($gtt['args'][0])); } elseif (count($gtt['args']) > 1 && in_array($gtt['function'], $this->pgettextTags)) { $entry->set(PoTokens::CONTEXT, $this->escapeForPo($gtt['args'][0])); $entry->set(PoTokens::MESSAGE, $this->escapeForPo($gtt['args'][1])); } elseif (count($gtt['args']) > 1 && in_array($gtt['function'], $this->ngettextTags)) { $entry->set(PoTokens::MESSAGE, $this->escapeForPo($gtt['args'][0])); $entry->set(PoTokens::PLURAL, $this->escapeForPo($gtt['args'][1])); } $this->checkPhpFormatFlag($entry); if ($gtt['line'] == $commentLine + 1) { $entry->set(PoTokens::EXTRACTED_COMMENTS, $this->stripComment($commentText)); } $this->poFile->mergeEntry($entry); } } } elseif (is_array($token) && $token[0] == T_COMMENT) { list(, $commentText, $commentLine) = $token; } $i++; } return $this->poFile; }
/** * Inspect the supplied source, capture gettext references as a PoFile object. * * @param string $source php source code * @param string $refname source identification used for PO reference comments * * @return PoFile */ public function msginitString($source, $refname) { if (!$this->poFile instanceof PoFile) { $this->poFile = new PoFile(); } $tpl = $this->smarty->createTemplate('eval:' . $source); $tags = $this->smarty->getTags($tpl); $translateTags = array_merge($this->gettextTags, $this->pgettextTags, $this->ngettextTags); foreach ($tags as $tag) { if (in_array($tag[0], $translateTags)) { $entry = new PoEntry(); $haveEntry = false; $entry->add(PoTokens::REFERENCE, $refname); foreach ($tag[1] as $temp) { foreach ($temp as $key => $value) { if ($value[0] == "'" || $value[0] == '"') { if (in_array($key, $this->msgidArgNames)) { $entry->set(PoTokens::MESSAGE, $this->escapeForPo($value)); $haveEntry = true; } elseif (in_array($key, $this->msgidPluralArgNames)) { $entry->set(PoTokens::PLURAL, $this->escapeForPo($value)); } elseif (in_array($key, $this->msgctxtArgNames)) { $entry->set(PoTokens::CONTEXT, $this->escapeForPo($value)); } } } } if ($haveEntry) { $this->checkPhpFormatFlag($entry); $this->poFile->mergeEntry($entry); } } } return $this->poFile; }