Filament php 4 Language switcher
By: Arthur Boyko
- Published: 17 Nov 2025 Updated: 17 Nov 2025
Add language buttons as theme switcher
In Providers/Filament/*PanelRpovider.php add render Hook:
1->renderHook(
2 PanelsRenderHook::USER_MENU_PROFILE_AFTER,
3 fn (): View => view('filament.hooks.lang-switcher'),
4 )
5
6 ->tenantMiddleware([
7 SetLocale::class
8 ])
Create new views filament/hooks/lang-switcher:
1<div
2 x-data="{ locale: null }"
3 x-init="
4 locale = localStorage.getItem('locale') || '{{ app()->getLocale() }}';
5 "
6 class="fi-theme-switcher flex items-center gap-1 me-2"
7>
8 <template x-for="(lang, code) in {
9 en: { label: ' EN', flag: '🇺🇸 ' },
10 uk: { label: ' UA', flag: '🇺🇦 ' },
11 }" :key="code">
12 <button
13 type="button"
14 class="fi-theme-switcher-btn text-sm flex items-center justify-center gap-1 px-2 py-1 rounded-lg border border-transparent transition"
15 :class="{
16 'bg-primary-500/10 text-primary-600 dark:text-primary-400 border-primary-500/30': locale === code,
17 'text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-800': locale !== code
18 }"
19 x-on:click="
20 locale = code;
21 localStorage.setItem('locale', code);
22 window.location.href = `/lang/${code}`;
23 "
24 {{-- x-tooltip="{
25 content: lang.label,
26 theme: $store.theme,
27 }" --}}
28 >
29 <span class="text-base" x-text="lang.flag"></span>
30 <span class="font-medium" x-text="lang.label"></span>
31 </button>
32 </template>
33</div>
Create new middleware for switch App\Http\Middleware\SetLocale:
1namespace App\Http\Middleware;
2
3use Illuminate\Http\Response;
4use Illuminate\Http\RedirectResponse;
5use Closure;
6use Illuminate\Http\Request;
7use Illuminate\Support\Facades\App;
8
9class SetLocale
10{
11 /**
12 * Handle an incoming request.
13 *
14 * @param Request $request
15 * @param Closure(Request):((Response|RedirectResponse)) $next
16 * @return Response|RedirectResponse
17 */
18 public function handle($request, Closure $next)
19 {
20 if (Session()->has('applocale') and array_key_exists(Session()->get('applocale'), config('app.languages'))) {
21 App::setLocale(Session()->get('applocale'));
22 } else { // This is optional as Laravel will automatically set the fallback language if there is none specified
23 App::setLocale(config('app.crm_locale'));
24 }
25
26 return $next($request);
27 }
28}
Related Tricks:
Filament php 4 Language switcher
Add language buttons as theme switcher
Access the current component in a render hook
when you don't have access to the current resource or page, `current` for the help!
Disable Breadcrumbs on specific page
you can disable breadcrumbs inside a resource class