Skip to content

Commit 6126e0f

Browse files
Added an option to remove missing keys (#42)
* Added an option --remove-missing which removes missing keys. Updated readme and added test * Try this * Fix styling * Try this Co-authored-by: Amir <amirrami.ce@gmail.com> Co-authored-by: amiranagram <amiranagram@users.noreply.github.com>
1 parent 3696901 commit 6126e0f

File tree

7 files changed

+74
-24
lines changed

7 files changed

+74
-24
lines changed

Diff for: .github/workflows/tests.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ jobs:
1010
matrix:
1111
os: [ ubuntu-latest, windows-latest ]
1212
php: [ 7.2.5, 8.0 ]
13-
laravel: [ 6.*, 8.*, 9.0 ]
13+
laravel: [ 6.*, 8.*, ^9.33 ]
1414
testbench: [ ^4.0, ^6.6, ^7.0 ]
1515
stability: [ prefer-lowest, prefer-stable ]
1616
exclude:
1717
- php: 7.2.5
1818
laravel: 8.*
1919
- php: 7.2.5
20-
laravel: 9.0
20+
laravel: ^9.33
2121
- php: 7.2.5
2222
testbench: ^6.6
2323
- php: 7.2.5
@@ -30,9 +30,9 @@ jobs:
3030
testbench: ^4.0
3131
- laravel: 8.*
3232
testbench: ^7.0
33-
- laravel: 9.0
33+
- laravel: ^9.33
3434
testbench: ^4.0
35-
- laravel: 9.0
35+
- laravel: ^9.33
3636
testbench: ^6.6
3737

3838
name: php-${{ matrix.php }} - laravel-${{ matrix.laravel }} - testbench-${{ matrix.testbench }} - ${{ matrix.stability }} - ${{ matrix.os }}

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ In this case translation strings will be generated for the language specified in
142142

143143
> Note: Strings you have already translated will not be overwritten.
144144
145+
### Remove Missing Keys
146+
147+
By default, the strings inside the locale files will be preserved even if they are not present the next time you run the localize command.
148+
If you want to remove those keys that are not present in your files anymore you can append the --remove-missing option to the localize command.
149+
``` bash
150+
php artisan localize --remove-missing
151+
```
145152
### Key Sorting
146153

147154
By default, the strings generated inside those JSON files will be sorted alphabetically by their keys.

Diff for: src/Collections/DefaultKeyCollection.php

+9
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ public function merge($items): self
2424
{
2525
return parent::merge(Arr::dot($items));
2626
}
27+
28+
/**
29+
* @param mixed $items
30+
* @return static
31+
*/
32+
public function intersectByKeys($items): self
33+
{
34+
return new self(collect(Arr::dot($this))->intersectByKeys($items));
35+
}
2736
}

Diff for: src/Commands/LocalizeCommand.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class LocalizeCommand extends Command
1616
*
1717
* @var string
1818
*/
19-
protected $signature = 'localize {lang?}';
19+
protected $signature = 'localize {lang?} {--remove-missing}';
2020

2121
/**
2222
* The console command description.
@@ -53,7 +53,12 @@ public function handle(Localizator $localizator, Parser $parser): int
5353
$progressBar->setMessage("Localizing {$locale}...");
5454

5555
foreach ($this->getTypes() as $type) {
56-
$localizator->localize($parser->getKeys($locale, $type), $type, $locale);
56+
$localizator->localize(
57+
$parser->getKeys($locale, $type),
58+
$type,
59+
$locale,
60+
$this->option('remove-missing')
61+
);
5762
}
5863

5964
$progressBar->advance();

Diff for: src/Services/Localizator.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class Localizator
1414
* @param string $locale
1515
* @return void
1616
*/
17-
public function localize(Translatable $keys, string $type, string $locale): void
17+
public function localize(Translatable $keys, string $type, string $locale, bool $removeMissing): void
1818
{
19-
$this->getWriter($type)->put($locale, $this->collect($keys, $type, $locale));
19+
$this->getWriter($type)->put($locale, $this->collect($keys, $type, $locale, $removeMissing));
2020
}
2121

2222
/**
@@ -25,13 +25,15 @@ public function localize(Translatable $keys, string $type, string $locale): void
2525
* @param string $locale
2626
* @return Translatable
2727
*/
28-
protected function collect(Translatable $keys, string $type, string $locale): Translatable
28+
protected function collect(Translatable $keys, string $type, string $locale, bool $removeMissing): Translatable
2929
{
3030
return $keys
31-
->merge($this->getCollector($type)->getTranslated($locale))
32-
->when(config('localizator.sort'), function (Translatable $keyCollection) {
33-
return $keyCollection->sortAlphabetically();
34-
});
31+
->merge($this->getCollector($type)->getTranslated($locale)
32+
->when($removeMissing, function (Translatable $keyCollection) use ($keys) {
33+
return $keyCollection->intersectByKeys($keys);
34+
}))->when(config('localizator.sort'), function (Translatable $keyCollection) {
35+
return $keyCollection->sortAlphabetically();
36+
});
3537
}
3638

3739
/**

Diff for: tests/LocalizatorTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,42 @@ public function testIntTranslationNestedKeysAreBeingSavedAsStrings(): void
295295

296296
$this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents);
297297
}
298+
299+
public function testRemoveMissingKeys(): void
300+
{
301+
self::flushDirectories('lang', 'views');
302+
303+
$this->createTestDefaultLangFile([
304+
'missingstring' => 'Missing',
305+
'name' => 'Name',
306+
], 'app', 'en');
307+
$this->createTestJsonLangFile([
308+
'Login' => 'Login',
309+
'Missing' => 'Missing',
310+
], 'en');
311+
312+
$enDefaultContents = $this->getDefaultLangContents('en', 'app');
313+
$enJsonContents = $this->getJsonLangContents('en');
314+
self::assertSame(['missingstring' => 'Missing', 'name' => 'Name'], $enDefaultContents);
315+
self::assertSame(['Login' => 'Login', 'Missing' => 'Missing'], $enJsonContents);
316+
317+
$this->createTestView("{{ __('Login') }} {{ __('app.name') }}");
318+
319+
// Run the command with the option to remove keys/strings that are not present anymore
320+
$this->artisan('localize en --remove-missing')
321+
->assertExitCode(0);
322+
323+
// Do created locale files exist?
324+
self::assertDefaultLangFilesExist('en', ['app']);
325+
self::assertJsonLangFilesExist('en');
326+
327+
// Do their contents match the expected results?
328+
$enDefaultContents = $this->getDefaultLangContents('en', 'app');
329+
$enJsonContents = $this->getJsonLangContents('en');
330+
self::assertSame(['name' => 'Name'], $enDefaultContents);
331+
self::assertSame(['Login' => 'Login'], $enJsonContents);
332+
333+
// Cleanup.
334+
self::flushDirectories('lang', 'views');
335+
}
298336
}

Diff for: tests/TestCase.php

-11
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ public function setUp(): void
2323
parent::setUp();
2424
}
2525

26-
/**
27-
* This method is called after the last test of this test class is run.
28-
*
29-
* @return void
30-
*/
31-
public static function tearDownAfterClass(): void
32-
{
33-
// Flush one last time after all tests have finished running.
34-
self::flushDirectories('lang', 'views');
35-
}
36-
3726
/**
3827
* Get package providers.
3928
*

0 commit comments

Comments
 (0)