-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to remove translations based on a json file
- Loading branch information
Showing
5 changed files
with
109 additions
and
52 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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Typidesign\Translations\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Filesystem\Filesystem; | ||
|
||
Abstract class AbstractTranslations extends Command | ||
{ | ||
/** | ||
* The filesystem instance. | ||
*/ | ||
protected $files; | ||
|
||
/** | ||
* Create a new command instance. | ||
*/ | ||
public function __construct(Filesystem $files) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->files = $files; | ||
} | ||
|
||
/** | ||
* Get an array containing all translations form a json file | ||
*/ | ||
protected function getTranslations(string $file) : array | ||
{ | ||
return $this->files->exists($file) ? (array) json_decode($this->files->get($file)) : []; | ||
} | ||
|
||
/** | ||
* Update the json file with new object | ||
*/ | ||
protected function put(string $file, array $translations) | ||
{ | ||
$this->files->put($file, json_encode($translations, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT)); | ||
} | ||
|
||
/** | ||
* Get file(s) from the path argument | ||
*/ | ||
protected function getFiles() : array | ||
{ | ||
$fileOrDirectory = base_path($this->argument('path')); | ||
if (!$this->files->exists($fileOrDirectory)) { | ||
$this->error($this->argument('path').' is not a file or a directory.'); | ||
exit(); | ||
} | ||
if ($this->files->isFile($fileOrDirectory)) { | ||
$files = [$fileOrDirectory]; | ||
} else if ($this->files->isDirectory($fileOrDirectory)) { | ||
$files = $this->files->files($fileOrDirectory); | ||
} | ||
|
||
return $files; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Typidesign\Translations\Console\Commands; | ||
|
||
class RemoveTranslations extends AbstractTranslations | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
*/ | ||
protected $signature = 'translations:remove | ||
{path : The file or directory containing the json encoded translations you want to remove.}'; | ||
|
||
/** | ||
* The console command description. | ||
*/ | ||
protected $description = 'Remove translations strings found in a file or directory.'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
foreach ($this->getFiles() as $file) { | ||
$mainFile = resource_path('lang/'.basename($file)); | ||
|
||
$existingTranslations = $this->getTranslations($mainFile); | ||
$newTranslations = $this->getTranslations($file); | ||
|
||
$translations = array_diff($existingTranslations, $newTranslations); | ||
|
||
$this->put($mainFile, $translations); | ||
|
||
$this->info(count($existingTranslations) - count($translations).' translations removed in '.$mainFile.'.'); | ||
} | ||
} | ||
} |