diff --git a/Tools/langUsageInspector.php b/Tools/langUsageInspector.php new file mode 100644 index 0000000..dd2a0cc --- /dev/null +++ b/Tools/langUsageInspector.php @@ -0,0 +1,79 @@ + -m ' . "\n\n"; + echo "\t" . '-d Destination/output directory.' . "\n"; + echo "\t" . '-m Module directory.' . "\n"; +} + +$destination = ($key = \array_search('-d', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" '); +$modulePath = ($key = \array_search('-m', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" '); + +if (!isset($destination) || !isset($modulePath) ) { + printUsage(); + + return; +} + +$sources = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($modulePath)); +$tpls = []; +$langs = []; + +foreach ($sources as $source) { + if ($source->isFile() + && (($temp = \strlen($source->getPathname()) - \strlen('tpl.php')) >= 0 && \strpos($source->getPathname(), 'tpl.php', $temp) !== false) + ) { + $tpls[] = $source->getPathname(); + } elseif ($source->isFile() + && (($temp = \strlen($source->getPathname()) - \strlen('lang.php')) >= 0 && \strpos($source->getPathname(), 'lang.php', $temp) !== false) + && \strlen(\explode('.', \basename($source->getPathname()))[0]) === 2 + ) { + $langFileContent = include $source->getPathname(); + $langs[\explode('.', \basename($source->getPathname()))[0]] = \end($langFileContent); + } +} + +// Compare language files +$length = 0; +$unequalLength = false; +$unusedLanguage = []; + +foreach($langs as $lang => $data) { + if ($length === 0) { + $length = \count($data); + $unusedLanguage = \array_keys($data); + } + + if ($length !== \count($data)) { + $unequalLength = true; + } + + foreach ($tpls as $tpl) { + $fileContent = \file_get_contents($tpl); + + foreach ($data as $key => $word) { + if (\stripos($fileContent, '$this->getHtml(\'' . $key . '\')') !== false + && ($key = \array_search($key, $unusedLanguage)) !== false + ) { + unset($unusedLanguage[$key]); + } + } + } +} + +echo 'Language files have different length: ' . ($unequalLength ? 'yes' : 'no') . "\n"; +echo 'Unused language components: ' . "\n"; +var_dump($unusedLanguage); \ No newline at end of file