-
-
Save edeoliv/38fd9931a34e775db190868f52114667 to your computer and use it in GitHub Desktop.
Seletor de idioma no Filament 3 com auto-detecção de idioma
This file contains hidden or 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 | |
// app/providers/Filament/AdminPanelProvider.php | |
->spa() | |
->spaUrlExceptions(['*/lang/*']) | |
->middleware([SetLang::class]) | |
->renderHook( | |
name: PanelsRenderHook::USER_MENU_PROFILE_AFTER, | |
hook: fn (): View => view(view: 'filament.hooks.lang-switcher'), | |
) | |
// resources/views/filament/hooks/lang-switcher.blade.php | |
@php | |
$locale = app()->getLocale(); | |
$flags = json_decode(env('APP_LOCALE_FLAGS'), true); | |
$labels = json_decode(env('APP_LOCALE_LABELS'), true); | |
@endphp | |
<x-filament::dropdown.list> | |
<div class="fi-lang-switcher grid grid-flow-col gap-x-1"> | |
@foreach(array_keys($labels) as $lang) | |
<a | |
aria-label="{{ $labels[$lang] }}" | |
type="button" | |
href="{{ url('lang/'.$lang) }}" | |
x-tooltip="{ content: '{{ $labels[$lang] }}' }" | |
class="fi-theme-switcher-btn flex justify-center rounded-md p-2 outline-none transition duration-75 | |
hover:bg-gray-50 focus-visible:bg-gray-50 dark:hover:bg-white/5 dark:focus-visible:bg-white/5 | |
{{ ($locale === $lang) | |
? 'fi-active bg-gray-50 text-primary-500 dark:bg-white/5 dark:text-primary-400' | |
: 'text-gray-400 hover:text-gray-500 focus-visible:text-gray-500 dark:text-gray-500 | |
dark:hover:text-gray-400 dark:focus-visible:text-gray-400' }} | |
" | |
> | |
{{ $flags[$lang] }} | |
</a> | |
@endforeach | |
</div> | |
</x-filament::dropdown.list> | |
// app/Http/Middleware/SetLang.php | |
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Http\Request; | |
use Illuminate\Session\Middleware\StartSession; | |
use Symfony\Component\HttpFoundation\Response; | |
class SetLang | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle(Request $request, Closure $next): Response | |
{ | |
return app(StartSession::class) | |
->handle($request, function ($request) use ($next) { | |
if (!session()->has('current_lang')) { | |
if ($languages = request()->header('accept-language')) { | |
foreach (explode(',', $languages) as $lang) { | |
$lang = str_replace('-', '_', $lang); | |
if (array_key_exists($lang, json_decode(env('APP_LOCALE_LABELS'), true))) { | |
session()->put('current_lang', $lang); | |
break; | |
} | |
} | |
} | |
} | |
app()->setLocale(session('current_lang', 'en')); | |
return $next($request); | |
}); | |
} | |
} | |
// .env | |
APP_LOCALE_FLAGS='{"en":"🇺🇸","pt_BR":"🇧🇷","es":"🇲🇽"}' | |
APP_LOCALE_LABELS='{"en":"English","pt_BR":"Português","es":"Spanish"}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment