Skip to content

Commit

Permalink
Added an option to remove missing keys (#42)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Oct 19, 2022
1 parent 3696901 commit 6126e0f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 24 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ jobs:
matrix:
os: [ ubuntu-latest, windows-latest ]
php: [ 7.2.5, 8.0 ]
laravel: [ 6.*, 8.*, 9.0 ]
laravel: [ 6.*, 8.*, ^9.33 ]
testbench: [ ^4.0, ^6.6, ^7.0 ]
stability: [ prefer-lowest, prefer-stable ]
exclude:
- php: 7.2.5
laravel: 8.*
- php: 7.2.5
laravel: 9.0
laravel: ^9.33
- php: 7.2.5
testbench: ^6.6
- php: 7.2.5
Expand All @@ -30,9 +30,9 @@ jobs:
testbench: ^4.0
- laravel: 8.*
testbench: ^7.0
- laravel: 9.0
- laravel: ^9.33
testbench: ^4.0
- laravel: 9.0
- laravel: ^9.33
testbench: ^6.6

name: php-${{ matrix.php }} - laravel-${{ matrix.laravel }} - testbench-${{ matrix.testbench }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ In this case translation strings will be generated for the language specified in

> Note: Strings you have already translated will not be overwritten.
### Remove Missing Keys

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.
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.
``` bash
php artisan localize --remove-missing
```
### Key Sorting

By default, the strings generated inside those JSON files will be sorted alphabetically by their keys.
Expand Down
9 changes: 9 additions & 0 deletions src/Collections/DefaultKeyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public function merge($items): self
{
return parent::merge(Arr::dot($items));
}

/**
* @param mixed $items
* @return static
*/
public function intersectByKeys($items): self
{
return new self(collect(Arr::dot($this))->intersectByKeys($items));
}
}
9 changes: 7 additions & 2 deletions src/Commands/LocalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LocalizeCommand extends Command
*
* @var string
*/
protected $signature = 'localize {lang?}';
protected $signature = 'localize {lang?} {--remove-missing}';

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

foreach ($this->getTypes() as $type) {
$localizator->localize($parser->getKeys($locale, $type), $type, $locale);
$localizator->localize(
$parser->getKeys($locale, $type),
$type,
$locale,
$this->option('remove-missing')
);
}

$progressBar->advance();
Expand Down
16 changes: 9 additions & 7 deletions src/Services/Localizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Localizator
* @param string $locale
* @return void
*/
public function localize(Translatable $keys, string $type, string $locale): void
public function localize(Translatable $keys, string $type, string $locale, bool $removeMissing): void
{
$this->getWriter($type)->put($locale, $this->collect($keys, $type, $locale));
$this->getWriter($type)->put($locale, $this->collect($keys, $type, $locale, $removeMissing));
}

/**
Expand All @@ -25,13 +25,15 @@ public function localize(Translatable $keys, string $type, string $locale): void
* @param string $locale
* @return Translatable
*/
protected function collect(Translatable $keys, string $type, string $locale): Translatable
protected function collect(Translatable $keys, string $type, string $locale, bool $removeMissing): Translatable
{
return $keys
->merge($this->getCollector($type)->getTranslated($locale))
->when(config('localizator.sort'), function (Translatable $keyCollection) {
return $keyCollection->sortAlphabetically();
});
->merge($this->getCollector($type)->getTranslated($locale)
->when($removeMissing, function (Translatable $keyCollection) use ($keys) {
return $keyCollection->intersectByKeys($keys);
}))->when(config('localizator.sort'), function (Translatable $keyCollection) {
return $keyCollection->sortAlphabetically();
});
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/LocalizatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,42 @@ public function testIntTranslationNestedKeysAreBeingSavedAsStrings(): void

$this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents);
}

public function testRemoveMissingKeys(): void
{
self::flushDirectories('lang', 'views');

$this->createTestDefaultLangFile([
'missingstring' => 'Missing',
'name' => 'Name',
], 'app', 'en');
$this->createTestJsonLangFile([
'Login' => 'Login',
'Missing' => 'Missing',
], 'en');

$enDefaultContents = $this->getDefaultLangContents('en', 'app');
$enJsonContents = $this->getJsonLangContents('en');
self::assertSame(['missingstring' => 'Missing', 'name' => 'Name'], $enDefaultContents);
self::assertSame(['Login' => 'Login', 'Missing' => 'Missing'], $enJsonContents);

$this->createTestView("{{ __('Login') }} {{ __('app.name') }}");

// Run the command with the option to remove keys/strings that are not present anymore
$this->artisan('localize en --remove-missing')
->assertExitCode(0);

// Do created locale files exist?
self::assertDefaultLangFilesExist('en', ['app']);
self::assertJsonLangFilesExist('en');

// Do their contents match the expected results?
$enDefaultContents = $this->getDefaultLangContents('en', 'app');
$enJsonContents = $this->getJsonLangContents('en');
self::assertSame(['name' => 'Name'], $enDefaultContents);
self::assertSame(['Login' => 'Login'], $enJsonContents);

// Cleanup.
self::flushDirectories('lang', 'views');
}
}
11 changes: 0 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ public function setUp(): void
parent::setUp();
}

/**
* This method is called after the last test of this test class is run.
*
* @return void
*/
public static function tearDownAfterClass(): void
{
// Flush one last time after all tests have finished running.
self::flushDirectories('lang', 'views');
}

/**
* Get package providers.
*
Expand Down

0 comments on commit 6126e0f

Please sign in to comment.