Skip to content

Commit

Permalink
Improved UNSUPPORTED.php and separated laravel 8- and 9+ lang versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipponik committed May 23, 2023
1 parent a98c546 commit 59acde5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```
25 changes: 22 additions & 3 deletions src/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Analyzer
{
private string $directoryPath = '';

private array $foundKeys = [];

private array $incorrectKeys = [];
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/DirectoryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 7 additions & 4 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<?php\n\nreturn $customVarExport;\n";
Helper::saveToFile($directoryName, 'UNSUPPORTED.php', $info);
}
}

public static function customVarExport($inputValue, int $spacesCount = 0): string
public static function customVarExport($inputValue, int $spacesCount = 0, bool $withKey = true): string
{
if (is_array($inputValue)) {
$spaces = str_repeat(' ', $spacesCount);
Expand All @@ -33,7 +34,9 @@ public static function customVarExport($inputValue, int $spacesCount = 0): strin

foreach ($inputValue as $key => $value) {
$exportedVar = self::customVarExport($value, $innerSpacesCount);
$summary .= "$innerSpaces'$key' => $exportedVar,\n";
$summary .= $withKey
? "$innerSpaces'$key' => $exportedVar,\n"
: "$innerSpaces$exportedVar,\n";
}
$summary .= $spaces . ']';

Expand Down

0 comments on commit 59acde5

Please sign in to comment.