diff --git a/README.md b/README.md index b2255b0..5275d69 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,25 @@ composer require filipponik/translate-analyzer --dev ## Usage +1. Use `pwd` to get needed directory, or use `__DIR__` in code. +``` +$ pwd +/home/user/project +``` + +2. Set your directory path, analyze some folder recursively and create language files. ```php $analyzer = new \Filipponik\TranslateAnalyzer\Analyzer(); $analyzer + ->setDirectoryPath('/home/user/project') // Analyze only .php files ->setSuffix('php') // Analyze directory ../app - ->analyze('../app') - // Write to laravel lang files (by default structure) - ->writeResultsToLaravelFiles(['en', 'es', 'ch']) + ->analyze('app') + // Write to laravel 8- file structure + ->toLaravel8AndBefore(['en', 'es', 'ch']) + // Write to laravel 9+ file structure + ->toLaravel9AndAbove(['en', 'es', 'ch']) // Or write lang files to selected directory - ->writeResultsToFiles('my_super_lang_files'); + ->writeResultsToFiles('my_lang_files'); ``` \ No newline at end of file diff --git a/src/Analyzer.php b/src/Analyzer.php index 8d474d6..7c650d8 100644 --- a/src/Analyzer.php +++ b/src/Analyzer.php @@ -6,6 +6,8 @@ class Analyzer { + private string $directoryPath = ''; + private array $foundKeys = []; private array $incorrectKeys = []; @@ -47,15 +49,26 @@ public function writeResultsToFiles(string $directoryName = 'analyze_results'): return $this; } - public function writeResultsToLaravelFiles(array $languages): self + public function writeResultsToLaravelFiles(array $languages, $prefix): self { foreach ($languages as $languageName) { - Formatter::createTranslationFiles("../lang/$languageName", $this->foundKeys, $this->incorrectKeys); + $filename = $this->directoryPath . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR . $languageName; + Formatter::createTranslationFiles($filename, $this->foundKeys, $this->incorrectKeys); } return $this; } + public function toLaravel8AndBefore(array $languages, $prefix = 'resources/lang'): self + { + return $this->writeResultsToLaravelFiles($languages, $prefix); + } + + public function toLaravel9AndAbove(array $languages, $prefix = 'lang'): self + { + return $this->writeResultsToLaravelFiles($languages, $prefix); + } + public function getFoundKeys(): array { return $this->foundKeys; @@ -66,9 +79,15 @@ public function getIncorrectKeys(): array return $this->incorrectKeys; } - public function setSuffix(string $suffix): Analyzer + public function setSuffix(string $suffix): self { $this->suffix = $suffix; return $this; } + + public function setDirectoryPath(string $directoryPath): self + { + $this->directoryPath = $directoryPath; + return $this; + } } diff --git a/src/DirectoryAnalyzer.php b/src/DirectoryAnalyzer.php index 1c0a4fe..0ddb723 100644 --- a/src/DirectoryAnalyzer.php +++ b/src/DirectoryAnalyzer.php @@ -50,7 +50,7 @@ public static function getKeysFromDirectory(string $path, array $inputArr, strin */ public static function getKeysFromFile(SplFileInfo $fileInfo): array { - $re = '/__\(\'[A-z\s.]+\'/m'; + $re = '/__\(\'[A-z\s[\\\'].+\'/m'; $arr = []; do { $string = $fileInfo->fgets(); diff --git a/src/Formatter.php b/src/Formatter.php index 1eaa32a..4f2ac4a 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -18,12 +18,13 @@ public static function createTranslationFiles(string $directoryName, array $keys } if (!empty($unsupportedKeys)) { - $info = implode("\n", $unsupportedKeys); - Helper::saveToFile($directoryName, 'UNSUPPORTED', $info); + $customVarExport = self::customVarExport($unsupportedKeys, 0, false); + $info = " $value) { $exportedVar = self::customVarExport($value, $innerSpacesCount); - $summary .= "$innerSpaces'$key' => $exportedVar,\n"; + $summary .= $withKey + ? "$innerSpaces'$key' => $exportedVar,\n" + : "$innerSpaces$exportedVar,\n"; } $summary .= $spaces . ']';