protected $_paths = [];
protected $_extensions = ['php', 'phtml'];
- protected $_exclude = ['\/\.git\/'];
+ protected $_exclude = ['\/\.git\/', '\/cache\/'];
protected $_toTranslate = [];
+ protected $_toTranslateLocations = [];
protected static $_parsed = [];
public function __construct()
$this->_extensions = array_unique($this->_extensions);
$this->_paths = array_unique($this->_paths);
-
foreach ($this->_paths as $sectionLabel => $path) {
if (!isset($this->_toTranslate[$sectionLabel])) {
}
start_measure('Get translate hash ' . $path);
- $cacheKey = 'translate_path_' . sha1($path . '--' . print_r($this->_extensions, true) . '//' . Files::dirmtime($path, true, $this->_extensions) . '*');
+ $cacheKey = 'translate_path_' . Files::hashFileAttributes(__FILE__) . sha1($path . '--' . print_r($this->_extensions, true) . '//' . print_r($this->_exclude, true) . '//' . Files::dirmtime($path, true, $this->_extensions) . '*');
stop_measure('Get translate hash ' . $path);
- $found = cache()->remember($cacheKey, 3600, function () use ($path) {
+ $found = cache()->remember($cacheKey, 28800, function () use ($path) {
start_measure('Parse path for translations ' . $path);
$res = [];
+ $locations = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
/** @var SplFileInfo $p */
foreach ($iterator as $p) {
break;
}
}
- if (!$excluded) {
+ if ($excluded) {
continue;
}
- $res = array_merge($res, static::extractTextsToTranslate($p));
+ $extracted = static::extractTextsToTranslate($p);
+ $res = array_merge($res, $extracted['str']);
+ $locations = array_merge_recursive($locations, $extracted['locations']);
}
}
stop_measure('Parse path for translations ' . $path);
- return $res;
+ return ['str' => $res, 'locations' => $locations];
});
- $this->_toTranslate[$sectionLabel] = array_merge($this->_toTranslate[$sectionLabel], $found);
+ $this->_toTranslate[$sectionLabel] = array_merge($this->_toTranslate[$sectionLabel], $found['str']);
+ $this->_toTranslateLocations = $found['locations'];
}
}
if (!$file->isFile() || !$file->isReadable()) {
return [];
}
- $cacheKey = 'totranslate_' . Files::hashFileAttributes($file->getPathname());
- return Cache::remember($cacheKey, 3600, function () use ($file) {
+ $cacheKey = 'totranslate_' . Files::hashFileAttributes(__FILE__) . '_' . Files::hashFileAttributes($file->getPathname());
+ return Cache::remember($cacheKey, 28800, function () use ($file) {
$c = file_get_contents($file);
$res = [];
+ $locations = [];
// @link https://regex101.com/r/uD0eT1/4
if (preg_match_all('/__\(([\'\"]{1})(.*)(([^\\\\]{1})(\1))(.*)\)/Us', $c, $matches)) {
foreach ($matches[2] as $k => $m) {
continue;
}
$m .= $matches[4][$k];
- $res[] = stripcslashes($m);
+ $m = stripcslashes($m);
+ $res[] = $m;
+ if (!isset($locations[$m])) {
+ $locations[$m] = [];
+ }
+ if (!in_array($file->getPathname(), $locations[$m])) {
+ $locations[$m][] = $file->getPathname();
+ }
}
}
- return $res;
+ return ['str' => $res, 'locations' => $locations];
});
}
return $this->_toTranslate;
}
+ public function getStringToTranslateLocations()
+ {
+ return $this->_toTranslateLocations;
+ }
+
public static function saveTranslations($data, $path)
{
file_put_contents($path, '<?php return ' . var_export($data, true) . ';');