Skip to content

Commit

Permalink
feat: add a way to exclude some translations via the exclude config o…
Browse files Browse the repository at this point in the history
…ption.

Fixes #29
  • Loading branch information
nbourguig committed Apr 21, 2019
1 parent 7799f6f commit 89313fe
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.phar
composer.lock
service-account.json
.phpunit.result.cache
tests/fixtures/basepaths/00-simple/resources/lang/vendor/foo/en/messages.php
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ Open the spreadsheet in the browser
$ php artisan translation_sheet:open
```

## Excluding translations

Sometimes you might need to instruct the package to exclude some translations.
You can do so by specifying patterns in the `exclude` config option.
It accepts multiple patterns that target the full translation keys and that the [Str::is](https://laravel.com/docs/5.8/helpers#method-str-is) can understand.

```php
[
// ...
'exclude' => [
'validation*', // This will exclude all the `validation.php` translations.
'foo::*', // This will exclude all the `foo` namespace translations.
'foo::bar.*', // this will exclude the `bar` translations from the `foo` namespace.
],
// ...
]
```


## Changelog

Expand Down
16 changes: 16 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@
*/
'locales' => env('TS_LOCALES', ['en']),

/*
|--------------------------------------------------------------------------
| Exclude lang files or namespaces
|--------------------------------------------------------------------------
| List here files or namespaces that the package will exclude
| from all the operations. (prepare, push & pull).
|
| You can use wild card pattern that can be used with the Str::is()
| Laravel helper. (https://laravel.com/docs/5.8/helpers#method-str-is)
|
| Example:
| 'validation*'
| 'foo::*',
| 'foo::bar.*',
*/
'exclude' => [],
];
11 changes: 10 additions & 1 deletion src/Pusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Nikaia\TranslationSheet;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Nikaia\TranslationSheet\Commands\Output;
use Nikaia\TranslationSheet\Sheet\TranslationsSheet;
use Nikaia\TranslationSheet\Translation\Reader;
Expand Down Expand Up @@ -51,8 +53,15 @@ public function push()

public function getScannedAndTransformedTranslations()
{
$excludePatterns = config('translation_sheet.exclude');

return $this->transformer
->setLocales($this->translationsSheet->getSpreadsheet()->getLocales())
->transform($this->reader->scan());
->transform($this->reader->scan())
->when(is_array($excludePatterns) && !empty($excludePatterns), function (Collection $collection) use ($excludePatterns) {
return $collection->reject(function ($item) use ($excludePatterns) {
return Str::is($excludePatterns, $item[0] /* full key */);
});
});
}
}
33 changes: 33 additions & 0 deletions tests/Feature/ExcludePatternsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Nikaia\TranslationSheet\Pusher;
use Nikaia\TranslationSheet\Test\FeatureTestCase;

class ExcludeFilterTest extends FeatureTestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$this->setBasePathFromFixtureFolder($app, '00-exclude');
}

/** @test */
public function it_exludes_correctly_the_specified_patterns()
{
/** @var Pusher $pusher */
$pusher = resolve(Pusher::class);

config()->set('translation_sheet.exclude', [
'foo::*',
'validation*'
]);
$this->assertCount(0, $pusher->getScannedAndTransformedTranslations());


config()->set('translation_sheet.exclude', [
'foo::*',
]);
$this->assertCount(4, $pusher->getScannedAndTransformedTranslations());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
"accepted" => "The :attribute must be accepted.",
"active_url" => "The :attribute is not a valid URL.",
"between" => [
"file" => "The :attribute must be between :min and :max kilobytes.",
"numeric" => "The :attribute must be between :min and :max."
]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
"accepted" => "The :attribute must be accepted.",
"active_url" => "The :attribute is not a valid URL.",
"between" => [
"file" => "The :attribute must be between :min and :max kilobytes.",
"numeric" => "The :attribute must be between :min and :max."
]
];
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

return [
"next" => "Next",
"next" => "Next (edited)",
"previous" => "Previous"
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
"next" => "Suivant &raquo;",
"previous" => "&laquo; Précédent"
];

0 comments on commit 89313fe

Please sign in to comment.