diff --git a/README.md b/README.md index 943a1d0..cadb31e 100644 --- a/README.md +++ b/README.md @@ -5,16 +5,6 @@ This package scans your project for missing translation keys and then writes them into individual json files for you to fill in. -## Usage - -Scan your project for missing translations: - -``` -php artisan langscanner -// outputs the translations -// writes them to translation files: en.json. nl.json, de.json, etc. -``` - ## Installation You can install the package via composer: @@ -23,6 +13,18 @@ You can install the package via composer: composer require druc/laravel-langscanner ``` +## Usage + +Scan your project for missing translations: + +``` +// outputs and writes translations for the specified language (dutch) +php artisan langscanner nl + +// outputs and writes translations in the existing {language}.json files +php artisan langscanner +``` + ## Credits This package is based on [joedixon/laravel-translation](https://github.com/joedixon/laravel-translation) and [themsaid/laravel-langman-gui](https://github.com/themsaid/laravel-langman-gui) diff --git a/src/Commands/LangscannerCommand.php b/src/Commands/LangscannerCommand.php index 5ae5702..c1e6dde 100644 --- a/src/Commands/LangscannerCommand.php +++ b/src/Commands/LangscannerCommand.php @@ -12,7 +12,7 @@ class LangscannerCommand extends Command { - protected $signature = 'langscanner'; + protected $signature = 'langscanner {language?}'; protected $description = "Finds keys without a corresponding translations and writes them into the translation (json) files."; public function handle() @@ -20,12 +20,6 @@ public function handle() $headers = ["Language", "Key", "Path"]; $rows = []; - $languages = new RequiredLanguages( - new Filesystem, - config('langscanner.languages_path'), - config('langscanner.excluded_languages') - ); - $requiredTranslations = new RequiredTranslations( new Filesystem, config('langscanner.paths'), @@ -33,7 +27,17 @@ public function handle() config('langscanner.translation_methods') ); - foreach ($languages->toArray() as $language) { + if ($this->argument('language')) { + $languages = [$this->argument('language')]; + } else { + $languages = (new RequiredLanguages( + new Filesystem, + config('langscanner.languages_path'), + config('langscanner.excluded_languages') + ))->toArray(); + } + + foreach ($languages as $language) { $missingTranslations = new MissingTranslations( $requiredTranslations, new ExistingTranslations( diff --git a/src/ExistingTranslations.php b/src/ExistingTranslations.php index cdd5459..f8ff060 100644 --- a/src/ExistingTranslations.php +++ b/src/ExistingTranslations.php @@ -11,7 +11,7 @@ class ExistingTranslations private string $language; private string $languagesPath; - public function __construct(Filesystem $disk, string $languagesPath, string $language) + public function __construct(Filesystem $disk, string $languagesPath, string $language) { $this->disk = $disk; $this->language = $language; diff --git a/src/FileTranslations.php b/src/FileTranslations.php index 1cbe328..b39fa61 100644 --- a/src/FileTranslations.php +++ b/src/FileTranslations.php @@ -13,8 +13,11 @@ public function __construct(string $path) public function update(MissingTranslations $missingTranslations) { - $existingTranslations = file_get_contents($this->path); - $existingTranslations = json_decode($existingTranslations, true); + try { + $existingTranslations = json_decode(file_get_contents($this->path), true); + } catch (\Exception $e) { + $existingTranslations = []; + } // Sets keys to an empty string: ['needs translation' => '', etc] $unfilledTranslations = array_fill_keys(array_keys($missingTranslations->toArray()), '');