-
-
Save yabasha/258354a4aac238288fa584566edfdeb1 to your computer and use it in GitHub Desktop.
Maintain localization / locale when using Laravel Livewire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http; | |
use Illuminate\Foundation\Http\Kernel as HttpKernel; | |
class Kernel extends HttpKernel | |
{ | |
//---- | |
/** | |
* The application's route middleware groups. | |
* | |
* @var array | |
*/ | |
protected $middlewareGroups = [ | |
'web' => [ | |
/* | |
* Current middleware | |
*/ | |
\App\Http\Middleware\LivewireLocaleMiddleware::class, | |
], | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#config/languages.php | |
return [ | |
'allowed_languages' => [ | |
'en', | |
'fr' | |
] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
class LivewireLocaleMiddleware | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$livewireLocale = $request->get('data')['livewireLocale'] ?? null; | |
if($livewireLocale){ | |
if (in_array($livewireLocale, config('languages.allowed_languages'))) { | |
app()->setLocale($livewireLocale); | |
session()->put('locale', $livewireLocale); | |
} | |
} | |
return $next($request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use App\Http\Middleware\LivewireLocaleMiddleware; | |
use Illuminate\Support\Facades\Route; | |
use Tests\TestCase; | |
class LivewireLocaleMiddlewareTest extends TestCase | |
{ | |
/** @test */ | |
public function it_sets_the_proper_locale() | |
{ | |
config()->set('languages.allowed_languages', ['en', 'fr']); | |
Route::post('/', function (){})->middleware(LivewireLocaleMiddleware::class); | |
app()->setLocale('fr'); | |
$payload = [ | |
'data' => [ | |
'livewireLocale' => 'en' | |
] | |
]; | |
$this->postJson("/", $payload); | |
$this->assertSame('en', app()->getLocale()); | |
} | |
/** @test */ | |
public function it_it_only_changes_when_requested() | |
{ | |
config()->set('languages.allowed_languages', ['en', 'fr']); | |
Route::post('/', function (){})->middleware(LivewireLocaleMiddleware::class); | |
app()->setLocale('fr'); | |
$payload = [ | |
'data' => [ | |
'notAValidKey' => 'en' | |
] | |
]; | |
$this->postJson("/", $payload); | |
$this->assertNotSame('en', app()->getLocale()); | |
} | |
/** @test */ | |
public function it_only_sets_allowable_languages() | |
{ | |
config()->set('languages.allowed_languages', ['en', 'fr']); | |
Route::post('/', function (){})->middleware(LivewireLocaleMiddleware::class); | |
app()->setLocale('en'); | |
$payload = [ | |
'data' => [ | |
'livewireLocale' => 'rs' | |
] | |
]; | |
$this->postJson("/", $payload); | |
$this->assertSame('en', app()->getLocale()); //the locale does not change since it's not part of the allowed_languages | |
$payload = [ | |
'data' => [ | |
'livewireLocale' => 'fr' | |
] | |
]; | |
$this->postJson("/", $payload); | |
$this->assertSame('fr', app()->getLocale()); //the locale does change since it's part of the allowed_languages | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Livewire; | |
trait LivewireLocaleTrait | |
{ | |
public $livewireLocale; | |
public function mount() | |
{ | |
$this->livewireLocale = app()->getLocale(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Livewire; | |
use Livewire\Component; | |
class MyLivewireComponent extends Component | |
{ | |
use LivewireLocaleTrait { | |
mount as localeMount; | |
} | |
public function mount() | |
{ | |
$this->localeMount(); | |
//local mount() code | |
} | |
public function render() | |
{ | |
return view('livewire.my_component'); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment