Skip to content

Commit 59c10db

Browse files
committed
ability to remove translations based on a json file
1 parent f19e9c8 commit 59c10db

File tree

5 files changed

+109
-52
lines changed

5 files changed

+109
-52
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ Every translations found in this directory will be added to ```/resources/lang``
4141

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

44+
### Remove translations
45+
46+
```php
47+
php artisan translations:remove vendor/typicms/pages/src/resources/lang[/lg.json]
48+
```
49+
50+
Every translations found in this file/directory will be removed from ```/resources/lang```
51+
52+
4453
```php
4554
php artisan translations:add vendor/typicms/pages/src/resources/lang --force
4655
```

src/ArtisanTranslationsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\ServiceProvider;
66
use Typidesign\Translations\Console\Commands\AddTranslations;
7+
use Typidesign\Translations\Console\Commands\RemoveTranslations;
78

89
class ArtisanTranslationsServiceProvider extends ServiceProvider
910
{
@@ -21,6 +22,7 @@ public function register()
2122
{
2223
$this->commands([
2324
AddTranslations::class,
25+
RemoveTranslations::class,
2426
]);
2527
}
2628
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Typidesign\Translations\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
Abstract class AbstractTranslations extends Command
9+
{
10+
/**
11+
* The filesystem instance.
12+
*/
13+
protected $files;
14+
15+
/**
16+
* Create a new command instance.
17+
*/
18+
public function __construct(Filesystem $files)
19+
{
20+
parent::__construct();
21+
22+
$this->files = $files;
23+
}
24+
25+
/**
26+
* Get an array containing all translations form a json file
27+
*/
28+
protected function getTranslations(string $file) : array
29+
{
30+
return $this->files->exists($file) ? (array) json_decode($this->files->get($file)) : [];
31+
}
32+
33+
/**
34+
* Update the json file with new object
35+
*/
36+
protected function put(string $file, array $translations)
37+
{
38+
$this->files->put($file, json_encode($translations, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT));
39+
}
40+
41+
/**
42+
* Get file(s) from the path argument
43+
*/
44+
protected function getFiles() : array
45+
{
46+
$fileOrDirectory = base_path($this->argument('path'));
47+
if (!$this->files->exists($fileOrDirectory)) {
48+
$this->error($this->argument('path').' is not a file or a directory.');
49+
exit();
50+
}
51+
if ($this->files->isFile($fileOrDirectory)) {
52+
$files = [$fileOrDirectory];
53+
} else if ($this->files->isDirectory($fileOrDirectory)) {
54+
$files = $this->files->files($fileOrDirectory);
55+
}
56+
57+
return $files;
58+
}
59+
}

src/Console/Commands/AddTranslations.php

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,26 @@
22

33
namespace Typidesign\Translations\Console\Commands;
44

5-
use Illuminate\Console\Command;
6-
use Illuminate\Filesystem\Filesystem;
7-
8-
class AddTranslations extends Command
5+
class AddTranslations extends AbstractTranslations
96
{
107
/**
118
* The name and signature of the console command.
12-
*
13-
* @var string
149
*/
1510
protected $signature = 'translations:add
1611
{path : The file or directory containing the json encoded translations you want to merge.}
1712
{--force : Overwrite existing translations.}';
1813

1914
/**
2015
* The console command description.
21-
*
22-
* @var string
2316
*/
2417
protected $description = 'Add translations strings found in a file or directory.';
2518

26-
/**
27-
* The filesystem instance.
28-
*
29-
* @var \Illuminate\Filesystem\Filesystem
30-
*/
31-
protected $files;
32-
33-
/**
34-
* Create a new command instance.
35-
*
36-
* @return void
37-
*/
38-
public function __construct(Filesystem $files)
39-
{
40-
parent::__construct();
41-
42-
$this->files = $files;
43-
}
44-
4519
/**
4620
* Execute the console command.
47-
*
48-
* @return mixed
4921
*/
5022
public function handle()
5123
{
52-
$fileOrDirectory = base_path($this->argument('path'));
53-
if (!$this->files->exists($fileOrDirectory)) {
54-
return $this->error($this->argument('path').' is not a file or a directory.');
55-
}
56-
if ($this->files->isFile($fileOrDirectory)) {
57-
$files = [$fileOrDirectory];
58-
} else if ($this->files->isDirectory($fileOrDirectory)) {
59-
$files = $this->files->files($fileOrDirectory);
60-
}
61-
foreach ($files as $file) {
24+
foreach ($this->getFiles() as $file) {
6225
$mainFile = resource_path('lang/'.basename($file));
6326

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

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

7639
$this->info(count($translations) - count($existingTranslations).' translations added in '.$mainFile.'.');
7740
}
7841
}
79-
80-
/**
81-
* Get an array containing all translations form a json file
82-
*
83-
* @param string $file
84-
*
85-
* @return array
86-
*/
87-
private function getTranslations($file)
88-
{
89-
return $this->files->exists($file) ? (array) json_decode($this->files->get($file)) : [];
90-
}
9142
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Typidesign\Translations\Console\Commands;
4+
5+
class RemoveTranslations extends AbstractTranslations
6+
{
7+
/**
8+
* The name and signature of the console command.
9+
*/
10+
protected $signature = 'translations:remove
11+
{path : The file or directory containing the json encoded translations you want to remove.}';
12+
13+
/**
14+
* The console command description.
15+
*/
16+
protected $description = 'Remove translations strings found in a file or directory.';
17+
18+
/**
19+
* Execute the console command.
20+
*/
21+
public function handle()
22+
{
23+
foreach ($this->getFiles() as $file) {
24+
$mainFile = resource_path('lang/'.basename($file));
25+
26+
$existingTranslations = $this->getTranslations($mainFile);
27+
$newTranslations = $this->getTranslations($file);
28+
29+
$translations = array_diff($existingTranslations, $newTranslations);
30+
31+
$this->put($mainFile, $translations);
32+
33+
$this->info(count($existingTranslations) - count($translations).' translations removed in '.$mainFile.'.');
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)