Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@

$buffer = COH::buffer();
$lines = explode("\n", $this->getActualTableWithNewKeys());
$lines = array_splice($lines, 2);

foreach ($lines as $line) {
expect($buffer)->toMatch(fn($actual) => str_contains($actual, $line));
Expand All @@ -245,6 +246,7 @@

$buffer = COH::buffer();
$lines = explode("\n", $this->getActualTableWithBadKeys());
$lines = array_splice($lines, 2);
// hack pour les systemes linux (github actions)
$lines = array_map(fn($line) => str_replace(['Services\\', 'Translation\\'], ['Services' . DS, 'Translation' . DS], $line), $lines);

Expand Down
75 changes: 46 additions & 29 deletions src/Cli/Commands/Translation/TranslationsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace BlitzPHP\Cli\Commands\Translation;

use Ahc\Cli\Output\Color;
use BlitzPHP\Cli\Console\Command;
use BlitzPHP\Utilities\Iterable\Arr;
use Locale;
Expand Down Expand Up @@ -50,24 +51,24 @@ class TranslationsFinder extends Command
*/
public function execute(array $params)
{
$this->verbose = array_key_exists('verbose', $params);
$this->showNew = array_key_exists('show-new', $params);
$this->verbose = $this->option('verbose', false);
$this->showNew = $this->option('show-new', false);
$optionLocale = $params['locale'] ?? null;
$optionDir = $params['dir'] ?? null;
$currentLocale = Locale::getDefault();
$currentDir = APP_PATH;
$this->languagePath = $currentDir . 'Translations';

if (ENVIRONMENT === 'testing') {
if (on_test()) {
$currentDir = ROOTPATH . 'Services' . DS;
$this->languagePath = ROOTPATH . 'Translations';
}

if (is_string($optionLocale)) {
if (! in_array($optionLocale, config('app.supported_locales'), true)) {
$this->error(
'Erreur: "' . $optionLocale . '" n\'est pas supporté. Les langues supportées sont: '
. implode(', ', config('app.supported_locales'))
$this->color->error('"' . $optionLocale . '" n\'est pas supporté. Les langues supportées sont: '
. implode(', ', config('app.supported_locales')))
);

return EXIT_USER_INPUT;
Expand All @@ -80,13 +81,13 @@ public function execute(array $params)
$tempCurrentDir = realpath($currentDir . $optionDir);

if ($tempCurrentDir === false) {
$this->error('Erreur: Le dossier doit se trouvé dans "' . $currentDir . '"');
$this->error($this->color->error('Le dossier doit se trouvé dans "' . $currentDir . '"'));

return EXIT_USER_INPUT;
}

if ($this->isSubDirectory($tempCurrentDir, $this->languagePath)) {
$this->error('Erreur: Le dossier "' . $this->languagePath . '" est restreint à l\'analyse.');
$this->error($this->color->error('Le dossier "' . $this->languagePath . '" est restreint à l\'analyse.'));

return EXIT_USER_INPUT;
}
Expand All @@ -96,7 +97,7 @@ public function execute(array $params)

$this->process($currentDir, $currentLocale);

$this->ok('Opérations terminées!');
$this->eol()->ok('Opérations terminées!');

return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -128,6 +129,9 @@ private function process(string $currentDir, string $currentLocale): void
if (is_file($languageFilePath)) {
// Charge les anciennes traductions
$languageStoredKeys = require $languageFilePath;
} elseif (! is_dir($dir = dirname($languageFilePath))) {
// Si le dossier n'existe pas, on le cree
@mkdir($dir, 0777, true);
}

$languageDiff = Arr::diffRecursive($foundLanguageKeys[$langFileName], $languageStoredKeys);
Expand All @@ -140,9 +144,17 @@ private function process(string $currentDir, string $currentLocale): void

if ($languageDiff !== []) {
if (file_put_contents($languageFilePath, $this->templateFile($newLanguageKeys)) === false) {
$this->writeIsVerbose('Fichier de traduction ' . $langFileName . ' (error write).', 'red');
if ($this->verbose) {
$this->justify('Fichier de traduction "' . $langFileName . '"', 'Erreur lors de la modification', [
'second' => ['fg' => Color::RED],
]);
}
} else {
$this->writeIsVerbose('Le fichier de traduction "' . $langFileName . '" a été modifié avec succès!', 'green');
if ($this->verbose) {
$this->justify('Fichier de traduction "' . $langFileName . '"', 'Modification éffectuée avec succès', [
'second' => ['fg' => Color::GREEN],
]);
}
}
}
}
Expand All @@ -153,18 +165,18 @@ private function process(string $currentDir, string $currentLocale): void
$table = [];

foreach ($tableRows as $body) {
$table[] = array_combine(['File', 'Key'], $body);
$table[] = array_combine(['Fichier', 'Clé'], $body);
}
$this->table($table);
}

if (! $this->showNew && $countNewKeys > 0) {
$this->writer->bgRed('Note: Vous devez utiliser votre outil de linting pour résoudre les problèmes liés aux normes de codage.');
if ($this->verbose) {
$this->eol()->border(char: '*');
}

$this->writeIsVerbose('Fichiers trouvés: ' . $countFiles);
$this->writeIsVerbose('Nouvelles traductions trouvées: ' . $countNewKeys);
$this->writeIsVerbose('Mauvaises traductions trouvées: ' . count($badLanguageKeys));
$this->justify('Fichiers analysés', $countFiles, ['second' => ['fg' => Color::GREEN]]);
$this->justify('Nouvelles traductions trouvées', $countNewKeys, ['second' => ['fg' => Color::GREEN]]);
$this->justify('Mauvaises traductions trouvées', count($badLanguageKeys), ['second' => ['fg' => Color::RED]]);

if ($this->verbose && $badLanguageKeys !== []) {
$tableBadRows = [];
Expand All @@ -178,11 +190,15 @@ private function process(string $currentDir, string $currentLocale): void
$table = [];

foreach ($tableBadRows as $body) {
$table[] = array_combine(['Bad Key', 'Filepath'], $body);
$table[] = array_combine(['Mauvaise clé', 'Fichier'], $body);
}

$this->table($table);
}

if (! $this->showNew && $countNewKeys > 0) {
$this->eol()->writer->bgRed('Note: Vous devez utiliser votre outil de linting pour résoudre les problèmes liés aux normes de codage.', true);
}
}

/**
Expand All @@ -200,7 +216,14 @@ private function findTranslationsInFile($file): array
}

$fileContent = file_get_contents($file->getRealPath());
preg_match_all('/lang\(\'([._a-z0-9\-]+)\'\)/ui', $fileContent, $matches);

preg_match_all('/\_\_\(\'([_a-z0-9À-ÿ\-]+)\'\)/ui', $fileContent, $matches);

if ($matches[1] !== []) {
$fileContent = str_replace($matches[0], array_map(static fn ($val) => "lang('App.{$val}')", $matches[1]), $fileContent);
}

preg_match_all('/lang\(\'([._a-z0-9À-ÿ\-]+)\'\)/ui', $fileContent, $matches);

if ($matches[1] === []) {
return compact('foundLanguageKeys', 'badLanguageKeys');
Expand Down Expand Up @@ -351,16 +374,6 @@ private function arrayToTableRows(string $langFileName, array $array): array
return $rows;
}

/**
* Affiche les détails dans la console si l'indicateur est défini
*/
private function writeIsVerbose(string $text = '', ?string $foreground = null, ?string $background = null): void
{
if ($this->verbose) {
$this->write($this->color->line($text, ['fg' => $foreground, 'bg' => $background]));
}
}

private function isSubDirectory(string $directory, string $rootDirectory): bool
{
return 0 === strncmp($directory, $rootDirectory, strlen($directory));
Expand All @@ -383,7 +396,11 @@ private function findLanguageKeysInFiles(array $files): array
continue;
}

$this->writeIsVerbose('Ficher trouvé: ' . mb_substr($file->getRealPath(), mb_strlen(APP_PATH)));
if ($this->verbose) {
$this->justify(mb_substr($file->getRealPath(), mb_strlen(APP_PATH)), 'Analysé', [
'second' => ['fg' => Color::YELLOW],
]);
}
$countFiles++;

$findInFile = $this->findTranslationsInFile($file);
Expand Down
Loading