Skip to content

Commit

Permalink
ability to remove translations based on a json file
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed Jun 1, 2017
1 parent f19e9c8 commit 59c10db
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 52 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ Every translations found in this directory will be added to ```/resources/lang``

By default, translation keys will not be overwritten. You can use the ```--force``` option to overwrite existing keys:

### Remove translations

```php
php artisan translations:remove vendor/typicms/pages/src/resources/lang[/lg.json]
```

Every translations found in this file/directory will be removed from ```/resources/lang```


```php
php artisan translations:add vendor/typicms/pages/src/resources/lang --force
```
Expand Down
2 changes: 2 additions & 0 deletions src/ArtisanTranslationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\ServiceProvider;
use Typidesign\Translations\Console\Commands\AddTranslations;
use Typidesign\Translations\Console\Commands\RemoveTranslations;

class ArtisanTranslationsServiceProvider extends ServiceProvider
{
Expand All @@ -21,6 +22,7 @@ public function register()
{
$this->commands([
AddTranslations::class,
RemoveTranslations::class,
]);
}
}
59 changes: 59 additions & 0 deletions src/Console/Commands/AbstractTranslations.php
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;
}
}
55 changes: 3 additions & 52 deletions src/Console/Commands/AddTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,26 @@

namespace Typidesign\Translations\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class AddTranslations extends Command
class AddTranslations extends AbstractTranslations
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translations:add
{path : The file or directory containing the json encoded translations you want to merge.}
{--force : Overwrite existing translations.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add translations strings found in a file or directory.';

/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Filesystem $files)
{
parent::__construct();

$this->files = $files;
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$fileOrDirectory = base_path($this->argument('path'));
if (!$this->files->exists($fileOrDirectory)) {
return $this->error($this->argument('path').' is not a file or a directory.');
}
if ($this->files->isFile($fileOrDirectory)) {
$files = [$fileOrDirectory];
} else if ($this->files->isDirectory($fileOrDirectory)) {
$files = $this->files->files($fileOrDirectory);
}
foreach ($files as $file) {
foreach ($this->getFiles() as $file) {
$mainFile = resource_path('lang/'.basename($file));

$existingTranslations = $this->getTranslations($mainFile);
Expand All @@ -71,21 +34,9 @@ public function handle()
}
ksort($translations);

$this->files->put($mainFile, json_encode($translations, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$this->put($mainFile, $translations);

$this->info(count($translations) - count($existingTranslations).' translations added in '.$mainFile.'.');
}
}

/**
* Get an array containing all translations form a json file
*
* @param string $file
*
* @return array
*/
private function getTranslations($file)
{
return $this->files->exists($file) ? (array) json_decode($this->files->get($file)) : [];
}
}
36 changes: 36 additions & 0 deletions src/Console/Commands/RemoveTranslations.php
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.'.');
}
}
}

0 comments on commit 59c10db

Please sign in to comment.