Skip to content

Commit

Permalink
Updating the last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Mar 5, 2020
1 parent 28331ec commit c317e2e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
10 changes: 7 additions & 3 deletions _docs/2-Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
2. [Configuration](2-Configuration.md)
3. [Usage](3-Usage.md)
4. [FAQ](4-FAQ.md)

## Settings

```php
Expand Down Expand Up @@ -66,15 +66,19 @@ return [
| localized-routes | Allows to register all translatable routes. |
| translation-redirect | Allows to translate the route attributes by using the translation event. |

## Ignored URI
## Ignored URI / Route

```php
'ignored-uri' => [
//
],

'ignored-routes' => [
//
],
```

You can set a list of uris to ignore from localization checks.
You can set a list of uris or route names to ignore from localization checks / redirection.

## Locales

Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"ext-intl": "*",
"orchestra/testbench": "^4.0",
"mockery/mockery": "^1.3.1",
"phpunit/phpcov": "^6.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
Expand Down
6 changes: 1 addition & 5 deletions config/localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,14 @@
],

/* -----------------------------------------------------------------
| Ignored URI from localization
| Ignored URI/Routes from localization
| -----------------------------------------------------------------
*/

'ignored-uri' => [
//
],

/* -----------------------------------------------------------------
| Ignored routes from localization
| -----------------------------------------------------------------
*/
'ignored-routes' => [
//
],
Expand Down
31 changes: 17 additions & 14 deletions src/Middleware/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,12 @@ abstract class Middleware
protected $localization;

/**
* The URIs that should not be localized.
* The URIs or route names that should not be localized.
*
* @var array
*/
protected $except = [];

/**
* The routes that should not be localized.
*
* @var array
*/
protected $except_routes = [];

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
Expand All @@ -54,9 +47,8 @@ abstract class Middleware
*/
public function __construct(Localization $localization)
{
$this->localization = $localization;
$this->except = config('localization.ignored-uri', []);
$this->except_routes = config('localization.ignored-routes', []);
$this->localization = $localization;
$this->except = $this->getIgnoredRedirection();
}

/* -----------------------------------------------------------------
Expand Down Expand Up @@ -127,17 +119,15 @@ protected function isDefaultLocaleHidden($locale)
*
* @return bool
*/
protected function shouldIgnore(Request $request)
protected function shouldIgnore(Request $request): bool
{
foreach ($this->except as $except) {
if ($except !== '/')
$except = trim($except, '/');

if ($request->is($except))
return true;
}

foreach ($this->except_routes as $except) {
if ($request->routeIs($except))
return true;
}
Expand Down Expand Up @@ -176,4 +166,17 @@ protected function makeRedirectResponse($url, $code = null)
{
return new RedirectResponse($url, $code ?? config('localization.redirection-code', 302), ['Vary' => 'Accept-Language']);
}

/**
* The URIs or route names that should not be redirected.
*
* @return array
*/
protected function getIgnoredRedirection(): array
{
return array_merge(
config('localization.ignored-uri', []),
config('localization.ignored-routes', [])
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CreateTranslatableTable extends Migration
/**
* Migrate to database.
*/
public function up()
public function up(): void
{
$this->createSchema(function (Blueprint $table) {
$table->increments('id');
Expand Down

0 comments on commit c317e2e

Please sign in to comment.