public function findTranslations($path = null)
{
$functions = array('trans', 'trans_choice', 'noEditTrans', 'ifEditTrans', 'Lang::get', 'Lang::choice', 'Lang::trans', 'Lang::transChoice', '@lang', '@choice');
$pattern = "(" . implode('|', $functions) . ")" . "\\(" . "(['\"])" . "(" . "[a-zA-Z0-9_-]+" . "([.][^)]+)+" . ")" . "['\"]" . "[\\),]";
// Close parentheses or new parameter
// Find all PHP + Twig files in the app folder, except for storage
$paths = $path ? [$path] : array_merge([$this->app->basePath() . '/app'], $this->app['config']['view']['paths']);
$keys = array();
foreach ($paths as $path) {
$finder = new Finder();
$finder->in($path)->name('*.php')->name('*.twig')->files();
/** @var \Symfony\Component\Finder\SplFileInfo $file */
foreach ($finder as $file) {
// Search the current file for the pattern
$fileContents = $file->getContents();
if (preg_match_all("/{$pattern}/siU", $fileContents, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE)) {
// Get all matches
$fileLines = null;
foreach ($matches[3] as $index => $key) {
$quote = $matches[2][$index][0];
$keyValue = $key[0];
if ($quote == '\'' && !str_contains($keyValue, ["\"", "'", "->"]) || $quote == '"' && !str_contains($keyValue, ["\$", "\"", "'", "->"])) {
if ($fileLines == null) {
$fileLines = self::computeFileLines($fileContents);
}
$keys[$keyValue][$file->getPath() . '/' . $file->getFilename()][] = self::offsetLine($fileLines, $key[1]);
}
}
}
}
}
// Add the translations to the database, if not existing.
$count = 0;
foreach ($keys as $key => $filePathsAndLocation) {
// Split the group and item
list($group, $item) = explode('.', $key, 2);
$translation = $this->missingKey('', $group, $item, null, false, true);
// create references
$paths = '';
foreach ($filePathsAndLocation as $filePath => $locations) {
$paths .= $filePath . ':' . implode(',', $locations) . "\n";
}
if (!$translation->exists) {
// this one is new
$translation->is_auto_added = true;
$count++;
}
$translation->source = $paths;
$translation->save();
}
// Return the number of found translations
return $count;
}