Skip to content

Commit

Permalink
Merge pull request #37 from etibarrustam/exclude_languages
Browse files Browse the repository at this point in the history
Exclude languages
  • Loading branch information
LarsWiegers authored Jun 6, 2023
2 parents a8e8363 + 73cddc0 commit 15c1232
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 11 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ Some packages have their own language files, it is probably smart to exclude the
```php
php artisan translations:check --excludedDirectories=lang/vendor
```
### Exclude languages
This section provides instructions on how to exclude specific languages from being checked.

To exclude languages, follow these steps:

1. Open the project's configuration file.

2. Locate the `translation-checker` file.

3. Add the language codes of the languages you want to exclude to the `exclude_languages` field.

For example:
```
exclude_languages = ["en", "fr", "es"]
```

### JSON support
The package supports both .php files and .json files for the translations.

Expand Down
8 changes: 0 additions & 8 deletions config/config.php

This file was deleted.

20 changes: 20 additions & 0 deletions config/translations-checker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Exclude Languages
|--------------------------------------------------------------------------
|
| This configuration option allows you to exclude specific languages from
| being processed or checked. The array contains the language codes that
| should be excluded from the language lists.
|
| Example: ['en-ca', 'en-us']
|
*/
'exclude_languages' => [],
];



14 changes: 13 additions & 1 deletion src/Console/Commands/CheckIfTranslationsAreAllThereCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public function handle()

public function handleFile($languageDir, $langFile): void
{
$excludedLangs = config('translations-checker.exclude_languages');

if (
$excludedLangs &&
(in_array(Str::afterLast($languageDir, '/'), $excludedLangs) ||
in_array(Str::afterLast(basename($languageDir, '.json'), '/'), $excludedLangs))
) {
return;
}

$fileName = basename($langFile);

if(Str::endsWith($fileName, '.json')) {
Expand Down Expand Up @@ -197,7 +207,9 @@ private function getLanguages(string $directory): array

closedir($handle);

return $languages;
return array_filter($languages, static function ($element) {
return ! in_array($element, config('translations-checker.exclude_languages'));
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/LaravelTranslationsCheckerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function boot()

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('laravel-translations-checker.php'),
__DIR__.'/../config/translations-checker.php' => config_path('translations-checker.php'),
], 'config');

// Publishing the views.
Expand Down Expand Up @@ -53,7 +53,7 @@ public function boot()
public function register()
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-translations-checker');
$this->mergeConfigFrom(__DIR__.'/../config/translations-checker.php', 'translations-checker');

// Register the main class to use with the facade
$this->app->singleton('laravel-translations-checker', function () {
Expand Down
42 changes: 42 additions & 0 deletions tests/Tests/Unit/Console/Commands/CheckExcludedLanguagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Larswiegers\LaravelTranslationsChecker\Tests\Tests\Unit\Console\Commands;

use Tests\TestCase;

class CheckExcludedLanguagesTest extends TestCase
{
private string $languagesDir = 'tests/resources/lang/exclude_langs';
public function setUp(): void
{
parent::setUp();
}

/**
* Test excluded languages.
* @return void
*/
public function testTheCommandShouldNotProduceAnErrorIfOtherLangsExcluded()
{
config()->set('translations-checker.exclude_languages', ['fr', 'ga']);

$command = $this->artisan('translations:check', [
'--directory' => $this->languagesDir
]);

$command->assertExitCode(0);
}

/**
* Test without exclusion.
* @return void
*/
public function testTheCommandShouldThrowAnErrorIfOtherLangsNotExcluded()
{
$command = $this->artisan('translations:check', [
'--directory' => $this->languagesDir
]);

$command->assertExitCode(1);
}
}
5 changes: 5 additions & 0 deletions tests/resources/lang/exclude_langs/en/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'test_key' => 'test_value',
];
5 changes: 5 additions & 0 deletions tests/resources/lang/exclude_langs/fr/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'test_key_fr' => 'test_value_fr',
];
5 changes: 5 additions & 0 deletions tests/resources/lang/exclude_langs/ga/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'test_key_ga' => 'test_value_ga',
];

0 comments on commit 15c1232

Please sign in to comment.