]> _ Git - cubist_locale.git/commitdiff
wip #6462 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Nov 2023 15:56:34 +0000 (16:56 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Nov 2023 15:56:34 +0000 (16:56 +0100)
src/Translate.php

index 52b0c622fda80424f1dcc9a3d927c74542f47c1e..ea7f8b5db2f9e31742600cb399d1f4e83a46a122 100644 (file)
@@ -13,8 +13,9 @@ class Translate
 
     protected $_paths = [];
     protected $_extensions = ['php', 'phtml'];
-    protected $_exclude = ['\/\.git\/'];
+    protected $_exclude = ['\/\.git\/', '\/cache\/'];
     protected $_toTranslate = [];
+    protected $_toTranslateLocations = [];
     protected static $_parsed = [];
 
     public function __construct()
@@ -52,7 +53,6 @@ class Translate
         $this->_extensions = array_unique($this->_extensions);
         $this->_paths = array_unique($this->_paths);
 
-
         foreach ($this->_paths as $sectionLabel => $path) {
 
             if (!isset($this->_toTranslate[$sectionLabel])) {
@@ -60,12 +60,13 @@ class Translate
             }
 
             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) {
@@ -77,18 +78,21 @@ class Translate
                                 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'];
         }
     }
 
@@ -98,10 +102,11 @@ class Translate
         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) {
@@ -109,10 +114,17 @@ class Translate
                         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];
         });
     }
 
@@ -130,6 +142,11 @@ class Translate
         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) . ';');