Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a group based middleware to add the app locale as a content-lan… #21

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions src/Http/Middleware/LocalResponseHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace WebWhales\LaravelMultilingual\Http\Middleware;

use Closure;

class LocalResponseHeader
{
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Content-Language', app()->getLocale());

return $response;
}
}
15 changes: 15 additions & 0 deletions src/LaravelMultilingualServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,32 @@
namespace WebWhales\LaravelMultilingual;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Routing\Router;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use WebWhales\LaravelMultilingual\Models\Locale;

class LaravelMultilingualServiceProvider extends PackageServiceProvider
{
public function register()
{
$this->app->register(ViewServiceProvider::class);
$this->app->register(MiddlewareServiceProvider::class);
}

public function boot()
{
Blueprint::macro('multilingual', function (Blueprint $table) {
$table->foreignIdFor(Locale::class);
});

//$this->headerWebMiddleware();
}

private function headerWebMiddleware(): void
{
$router = $this->app->make(Router::class);
$router->pushMiddlewareToGroup('web');
}

public function configurePackage(Package $package): void
Expand Down
18 changes: 18 additions & 0 deletions src/MiddlewareServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace WebWhales\LaravelMultilingual;

use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use WebWhales\LaravelMultilingual\Http\Middleware\LocalResponseHeader;

class MiddlewareServiceProvider extends ServiceProvider
{
public function boot()
{
$router = $this->app->make(Router::class);
$router->pushMiddlewareToGroup('web', LocalResponseHeader::class);
}
}
11 changes: 11 additions & 0 deletions src/ViewServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace WebWhales\LaravelMultilingual;

use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
}
21 changes: 21 additions & 0 deletions tests/LocalResponseHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

//declare(strict_types=1);
//
//use Illuminate\Support\Facades\Route;
//use function Pest\Laravel\get;
//
//it('should receive a dutch response header when having set a dutch locale', function () {
// app()->setLocale('nl');
//
// Route::group(['middleware' => ['web']], function () {
// Route::get('/test-url' );
// });
//
// $response = get( '/test-url' );
//
// dump( $response->headers);
// expect( response()->headers )->toContain( 'Content-Language' );
//} );