Fix finding __() and _n() i18n function calls

This commit is contained in:
Tautvidas Sipavičius
2016-12-15 15:55:26 +02:00
parent f4b7acca1e
commit 2be9985d20
2 changed files with 30 additions and 18 deletions

View File

@@ -23,6 +23,7 @@ module.exports = function (grunt) {
cwd: '.', // base path where to look for translatable strings
domainPath: 'lang', // where to save the .pot
exclude: [
'\.mp_svn/.*',
'assets/.*',
'lang/.*',
'node_modules/.*',

View File

@@ -257,30 +257,41 @@ class StringExtractor {
$current_argument = null;
}
} elseif(in_array($extension, array('html', 'hbs'))) {
// get all translatable strings
$functions_pattern = '/('.join('|', $function_names).')\((.*)\)/Us';
preg_match_all($functions_pattern, $code, $matches, PREG_OFFSET_CAPTURE);
for($i = 0, $count = count($matches[1]); $i < $count; $i++) {
// get match offset (position where the match was found)
$offset = array_pop($matches[0][$i]);
// fetches all the text before the match
list($before) = str_split($code, $offset);
// calculate line number
$line_number = strlen($before) - strlen(str_replace("\n", "", $before)) + 1;
$function_patterns = array(
'/(__)\(([\'"].+?[\'"])\)/',
'/(_n)\(([\'"].+?[\'"],\s*[\'"].+?[\'"],\s*.+?)\)/'
);
$matches = array();
foreach($function_patterns as $pattern) {
preg_match_all($pattern, $code, $function_matches, PREG_OFFSET_CAPTURE);
for($i = 0; $i < count($function_matches[1]); $i += 1) {
$matches[] = array(
'call' => $function_matches[0][$i][0],
'call_offset' => $function_matches[0][$i][1],
'name' => $function_matches[1][$i][0],
'arguments' => $function_matches[2][$i][0]
);
}
}
foreach($matches as $match) {
list($text_before_match) = str_split($code, $match['call_offset']);
$number_of_newlines = strlen($text_before_match) - strlen(str_replace("\n", "", $text_before_match));
$line_number = $number_of_newlines + 1;
// extract arguments (potentially multiple)
$arguments_pattern = "/(?s)(?<!\\\\)(\"|')(?:[^\\\\]|\\\\.)*?\\1|[^,\\s]+/";
preg_match_all($arguments_pattern, $matches[2][$i][0], $arguments_matches);
preg_match_all($arguments_pattern, $match['arguments'], $arguments_matches);
//echo var_dump($arguments_matches[0]);
$arguments = array();
foreach($arguments_matches[0] as $arg) {
$arguments[] = trim($arg, "'\"");
foreach($arguments_matches[0] as $argument) {
$arguments[] = trim($argument, "'\"");
}
$call = array(
'name' => $matches[1][$i][0],
'name' => $match['name'],
'args' => $arguments,
'line' => $line_number
);