function templize($source) { $output = array(); $inline_re = '/^\\s*trans\\s+("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')\\s*/'; $block_re = "/^\\s*blocktrans(?:\\s+|\$)/"; $endblock_re = "/^\\s*endblocktrans\$/"; $plural_re = "/^\\s*plural\$/"; $var_re = '{ _\\( ( "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | # Double Quote string \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # Single Quote String ) \\) }x'; $lexer = new H2o_Lexer(h2o::getOptions()); $tokenstream = $lexer->tokenize($source); $in_block = false; $is_plural = false; $singulars = array(); $plurals = array(); while ($t = $tokenstream->next()) { if ($in_block) { if ($t->type == 'block' && $t->content == 'endblocktrans') { if ($is_plural) { $output[] = sprintf(" ngettext('%s', '%s', \$count)", join('', $singulars), join('', $plurals)); } else { $output[] = sprintf(" gettext('%s')", join('', $singulars)); } $singulars = $plurals = array(); $in_block = $is_plural = false; } elseif ($t->type == 'block' && $t->content == 'plural') { $is_plural = true; } elseif ($t->type == 'text') { if ($is_plural) { $plurals[] = addslashes($t->content); } else { $singulars[] = addslashes($t->content); } } elseif ($t->type == 'variable') { @(list($var, $filters) = explode('|', $t->content)); if ($is_plural) { $plurals[] = sprintf("%%(%s)", $var); } else { $singulars[] = sprintf("%%(%s)", $var); } } elseif ($t->type == 'block') { throw new Exception('No block tag is allowed in translation block'); } } else { if ($t->type == 'block') { if (preg_match($inline_re, $t->content, $matches)) { $output[] = sprintf(" gettext(%s)", $matches[1]); } elseif (preg_match($block_re, $t->content, $matches)) { $in_block = true; } } elseif ($t->type == 'variable') { if (preg_match($var_re, $t->content, $matches)) { $output[] = sprintf(" gettext(%s)", $matches[1]); } } } } $result = str_replace("\r", '', implode(";\n", $output)); if ($result) { return "\n" . $result . ";\n"; } }