Skip to content

Commit

Permalink
Add is_filament_livewire_route helper and check it in the SetLocale m…
Browse files Browse the repository at this point in the history
…iddleware
  • Loading branch information
jyrkidn committed Sep 28, 2023
1 parent 951908d commit 6eb7c0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Http/Middleware/SetLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Closure;
use Codedor\LocaleCollection\Facades\LocaleCollection;
use Filament\Facades\Filament;
use Filament\Panel;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class SetLocale
{
Expand All @@ -16,7 +19,21 @@ class SetLocale
*/
public function handle(Request $request, Closure $next)
{
LocaleCollection::setCurrent($request->segment(1), $request->root());
if (is_filament_livewire_route($request)) {
return $next($request);
}

$locale = $request->segment(1);

if ($request->headers->has('X-LIVEWIRE')) {
$snapshot = json_decode($request->json('components.0.snapshot', []), true);

if (isset($snapshot['memo']['locale'])) {
$locale = $snapshot['memo']['locale'];
}
}

LocaleCollection::setCurrent($locale, $request->root());

return $next($request);
}
Expand Down
20 changes: 20 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use Codedor\LocaleCollection\LocaleCollection;
use Codedor\TranslatableRoutes\TranslateRoute;
use Filament\Facades\Filament;
use Filament\Panel;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

if (! function_exists('translate_route')) {
function translate_route(string $routeName, string $locale = null, array|Collection $parameters = []): ?string
Expand All @@ -17,3 +20,20 @@ function translated_routes(string $routeName = null, array $parameters = [], str
return TranslateRoute::getAllForNameOrCurrent($routeName, $parameters, $fallbackRoute);
}
}

if (! function_exists('is_filament_livewire_route')) {
function is_filament_livewire_route($request): bool
{
if ($request->headers->has('X-LIVEWIRE') && $request->server('HTTP_REFERER')) {
$referer = $request->server('HTTP_REFERER');
$isFilament = collect(Filament::getPanels())

Check failure on line 29 in src/helpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method getPanels() on an unknown class Filament\Facades\Filament.

Check failure on line 29 in src/helpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method getPanels() on an unknown class Filament\Facades\Filament.
->contains(fn (Panel $panel) => Str::startsWith($referer, $panel->getUrl()));

Check failure on line 30 in src/helpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getUrl() on an unknown class Filament\Panel.

Check failure on line 30 in src/helpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $panel of anonymous function has invalid type Filament\Panel.

Check failure on line 30 in src/helpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getUrl() on an unknown class Filament\Panel.

Check failure on line 30 in src/helpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $panel of anonymous function has invalid type Filament\Panel.

if ($isFilament) {
return true;
}
}

return false;
}
}

0 comments on commit 6eb7c0c

Please sign in to comment.