-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Update
- Loading branch information
cdruc
committed
May 20, 2021
1 parent
56bd8be
commit ca5f92f
Showing
31 changed files
with
412 additions
and
456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
github: cdruc | ||
github: druc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
blank_issues_enabled: false | ||
contact_links: | ||
- name: Ask a question | ||
url: https://github.com/cdruc/laravel-langscanner/discussions/new?category=q-a | ||
url: https://github.com/druc/laravel-langscanner/discussions/new?category=q-a | ||
about: Ask the community for help | ||
- name: Request a feature | ||
url: https://github.com/cdruc/laravel-langscanner/discussions/new?category=ideas | ||
url: https://github.com/druc/laravel-langscanner/discussions/new?category=ideas | ||
about: Share ideas for new features | ||
- name: Report a bug | ||
url: https://github.com/cdruc/laravel-langscanner/issues/new | ||
url: https://github.com/druc/laravel-langscanner/issues/new | ||
about: Report a reproducable bug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,56 @@ | ||
<?php | ||
|
||
namespace Cdruc\Langscanner\Commands; | ||
namespace Druc\Langscanner\Commands; | ||
|
||
use Cdruc\Langscanner\Langscanner; | ||
use Druc\Langscanner\ExistingTranslations; | ||
use Druc\Langscanner\FileTranslations; | ||
use Druc\Langscanner\MissingTranslations; | ||
use Druc\Langscanner\RequiredLanguages; | ||
use Druc\Langscanner\RequiredTranslations; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Filesystem\Filesystem; | ||
|
||
class LangscannerCommand extends Command | ||
{ | ||
protected Langscanner $langscanner; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'langscanner'; | ||
protected $description = "Finds keys without a corresponding translations and writes them into the translation (json) files."; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'List all translation keys which don\'t have a corresponding translation'; | ||
|
||
public function __construct(Langscanner $langscanner) | ||
{ | ||
parent::__construct(); | ||
$this->langscanner = $langscanner; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException | ||
*/ | ||
public function handle() | ||
{ | ||
$missingTranslations = $this->langscanner->missingTranslations(); | ||
|
||
if (empty($missingTranslations)) { | ||
return $this->info("No missing translations were detected."); | ||
} | ||
|
||
$headers = ["Language", "Key", "Path"]; | ||
$rows = []; | ||
|
||
// set some headers for the table of results | ||
$headers = ["Language", "Type", "Group", "Key"]; | ||
|
||
// iterate over each of the missing languages | ||
foreach ($missingTranslations as $language => $types) { | ||
// iterate over each of the file types (json or array) | ||
foreach ($types as $type => $keys) { | ||
// iterate over each of the keys | ||
foreach ($keys as $key => $value) { | ||
// populate the array with the relevant data to fill the table | ||
foreach ($value as $k => $v) { | ||
$rows[] = [$language, $type, $key, $k]; | ||
} | ||
} | ||
$languages = new RequiredLanguages( | ||
new Filesystem, | ||
config('langscanner.languages_path'), | ||
config('langscanner.excluded_languages') | ||
); | ||
|
||
$requiredTranslations = new RequiredTranslations( | ||
new Filesystem, | ||
config('langscanner.paths'), | ||
config('langscanner.excluded_paths'), | ||
config('langscanner.translation_methods') | ||
); | ||
|
||
foreach ($languages->toArray() as $language) { | ||
$missingTranslations = new MissingTranslations( | ||
$requiredTranslations, | ||
new ExistingTranslations( | ||
new Filesystem, | ||
config('langscanner.languages_path'), | ||
$language | ||
) | ||
); | ||
|
||
foreach ($missingTranslations->toArray() as $key => $path) { | ||
$rows[] = [$language, $key, $path]; | ||
} | ||
|
||
(new FileTranslations(config('langscanner.languages_path')."/$language.json")) | ||
->update($missingTranslations); | ||
} | ||
|
||
// render the table of results | ||
$this->table($headers, $rows); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Druc\Langscanner; | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Collection; | ||
|
||
class ExistingTranslations | ||
{ | ||
private Filesystem $disk; | ||
private string $language; | ||
private string $languagesPath; | ||
|
||
public function __construct(Filesystem $disk, string $languagesPath, string $language) | ||
{ | ||
$this->disk = $disk; | ||
$this->language = $language; | ||
$this->languagesPath = $languagesPath; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$translationFiles = new Collection($this->disk->allFiles($this->languagesPath)); | ||
|
||
return $translationFiles->filter(function ($file) { | ||
return strpos($file, "{$this->language}.json"); | ||
})->flatMap(function ($file) { | ||
return json_decode($this->disk->get($file), true); | ||
})->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Druc\Langscanner; | ||
|
||
class FileTranslations | ||
{ | ||
private string $path; | ||
|
||
public function __construct(string $path) | ||
{ | ||
$this->path = $path; | ||
} | ||
|
||
public function update(MissingTranslations $missingTranslations) | ||
{ | ||
$existingTranslations = file_get_contents($this->path); | ||
$existingTranslations = json_decode($existingTranslations, true); | ||
|
||
// Sets keys to an empty string: ['needs translation' => '', etc] | ||
$unfilledTranslations = array_fill_keys(array_keys($missingTranslations->toArray()), ''); | ||
|
||
$mergedTranslations = array_merge($existingTranslations, $unfilledTranslations); | ||
$mergedTranslations = json_encode($mergedTranslations, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); | ||
|
||
file_put_contents($this->path, $mergedTranslations); | ||
} | ||
} |
Oops, something went wrong.