namespace Cubist\Locale;
+use Cubist\Util\Files\Files;
use \RecursiveIteratorIterator;
use \RecursiveDirectoryIterator;
use \SplFileInfo;
protected $_paths = [];
protected $_extensions = ['php', 'phtml'];
+ protected $_exclude = ['^\.git'];
protected $_toTranslate = [];
protected static $_parsed = [];
if (isset(self::$_parsed[$path])) {
continue;
}
- start_measure('Parse path for translations ' . $path);
- $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
- foreach ($iterator as $p) {
- if ($p->isFile()) {
- $this->_parseFile($p, $sectionLabel);
- }
+ if (!isset($this->_toTranslate[$sectionLabel])) {
+ $this->_toTranslate[$sectionLabel] = [];
}
+
+ start_measure('Get translate hash ' . $path);
+ $cacheKey = 'translate_path_' . sha1($path . '//' . Files::dirmtime($path, true, $this->_extensions));
+ stop_measure('Get translate hash ' . $path);
+ $this->_toTranslate[$sectionLabel] = array_merge($this->_toTranslate[$sectionLabel], cache()->remember($cacheKey, 3600, function () use ($path) {
+ start_measure('Parse path for translations ' . $path);
+ $res = [];
+ $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
+ foreach ($iterator as $p) {
+ if ($p->isFile() && in_array($p->getExtension(), $this->_extensions)) {
+ $res = array_merge($res, self::extractTextsToTranslate($p));
+ }
+ }
+ stop_measure('Parse path for translations ' . $path);
+ return $res;
+ }));
self::$_parsed[$path] = true;
- stop_measure('Parse path for translations ' . $path);
}
}
- protected function _parseFile(SplFileInfo $file, $sectionLabel)
- {
- if (!in_array($file->getExtension(), $this->_extensions)) {
- return;
- }
- if (!isset($this->_toTranslate[$sectionLabel])) {
- $this->_toTranslate[$sectionLabel] = [];
- }
- $this->_toTranslate[$sectionLabel] = array_merge($this->_toTranslate[$sectionLabel], self::extractTextsToTranslate($file));
- }
public static function extractTextsToTranslate(SplFileInfo $file)
{
- $toTranslate = [];
-
if (!$file->isFile() || !$file->isReadable()) {
- $toTranslate = [];
- } else {
- $c = file_get_contents($file);
+ return [];
+ }
- // @link https://regex101.com/r/uD0eT1/4
- if (preg_match_all('/__\(([\'\"]{1})(.*)(([^\\\\]{1})(\1))(.*)\)/Us', $c, $matches)) {
- foreach ($matches[2] as $k => $m) {
- if ($m == '') {
- continue;
- }
- $m .= $matches[4][$k];
- $toTranslate[] = stripcslashes($m);
+ $c = file_get_contents($file);
+ $res = [];
+ // @link https://regex101.com/r/uD0eT1/4
+ if (preg_match_all('/__\(([\'\"]{1})(.*)(([^\\\\]{1})(\1))(.*)\)/Us', $c, $matches)) {
+ foreach ($matches[2] as $k => $m) {
+ if ($m == '') {
+ continue;
}
+ $m .= $matches[4][$k];
+ $res[] = stripcslashes($m);
}
}
-
- return $toTranslate;
+ return $res;
}
public function hasSomethingToTranslate()