Enable language switching for guest users
Changes: - Move locale.update route outside auth middleware - Update LocaleController to support both authenticated and guest users - Guest users: Save locale preference to session only - Authenticated users: Save to both session and database - Add language switcher dropdown to header for all users - Display current language with globe icon - Show all 8 supported languages in dropdown - Highlight currently selected language with checkmark This allows non-logged-in users to change the interface language, improving accessibility for international visitors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ class LocaleController extends Controller
|
||||
{
|
||||
/**
|
||||
* Update the user's locale preference.
|
||||
* Works for both authenticated and guest users.
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
@@ -19,12 +20,14 @@ public function update(Request $request)
|
||||
|
||||
$locale = $validated['locale'];
|
||||
|
||||
// Save to user record
|
||||
Auth::user()->update(['locale' => $locale]);
|
||||
|
||||
// Also save to session for immediate effect
|
||||
// Save to session (works for both authenticated and guest users)
|
||||
$request->session()->put('locale', $locale);
|
||||
|
||||
return redirect()->route('profile.edit')->with('success', __('messages.settings.language_updated'));
|
||||
// If authenticated, also save to user record for persistence
|
||||
if (Auth::check()) {
|
||||
Auth::user()->update(['locale' => $locale]);
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', __('messages.settings.language_updated'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,51 @@ class="inline-flex items-center px-3 py-2 border border-gray-300 shadow-sm text-
|
||||
</kbd>
|
||||
</button>
|
||||
|
||||
<!-- Language Switcher (for all users) -->
|
||||
<div x-data="{ open: false }" @click.away="open = false" class="relative">
|
||||
<button
|
||||
@click="open = !open"
|
||||
class="flex items-center px-3 py-2 text-sm font-medium text-gray-700 hover:text-gray-900 focus:outline-none"
|
||||
title="{{ __('messages.settings.change_language') }}"
|
||||
>
|
||||
<svg class="w-5 h-5 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5h12M9 3v2m1.048 9.5A18.022 18.022 0 016.412 9m6.088 9h7M11 21l5-10 5 10M12.751 5C11.783 10.77 8.07 15.61 3 18.129"></path>
|
||||
</svg>
|
||||
@php
|
||||
$currentLocale = app()->getLocale();
|
||||
$locales = \App\Http\Middleware\SetLocale::SUPPORTED_LOCALES;
|
||||
@endphp
|
||||
<span class="hidden sm:inline">{{ $locales[$currentLocale] ?? 'English' }}</span>
|
||||
<svg class="ml-1 h-4 w-4" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path>
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<div
|
||||
x-show="open"
|
||||
x-transition
|
||||
class="absolute right-0 mt-2 w-48 bg-white rounded-md shadow-lg py-1 ring-1 ring-black ring-opacity-5 max-h-96 overflow-y-auto"
|
||||
>
|
||||
@foreach($locales as $code => $name)
|
||||
<form method="POST" action="{{ route('locale.update') }}" class="inline-block w-full">
|
||||
@csrf
|
||||
<input type="hidden" name="locale" value="{{ $code }}">
|
||||
<button
|
||||
type="submit"
|
||||
class="w-full text-left px-4 py-2 text-sm hover:bg-gray-100 {{ $currentLocale === $code ? 'bg-indigo-50 text-indigo-700 font-semibold' : 'text-gray-700' }}"
|
||||
>
|
||||
{{ $name }}
|
||||
@if($currentLocale === $code)
|
||||
<svg class="inline-block w-4 h-4 ml-2" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path>
|
||||
</svg>
|
||||
@endif
|
||||
</button>
|
||||
</form>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@auth
|
||||
<!-- User Dropdown -->
|
||||
<div x-data="{ open: false }" @click.away="open = false" class="relative">
|
||||
|
||||
@@ -22,11 +22,13 @@
|
||||
return view('dashboard');
|
||||
})->middleware(['auth', 'verified'])->name('dashboard');
|
||||
|
||||
// Locale switcher - available for all users (both authenticated and guest)
|
||||
Route::post('/locale', [LocaleController::class, 'update'])->name('locale.update');
|
||||
|
||||
Route::middleware('auth')->group(function () {
|
||||
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
||||
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
||||
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
||||
Route::post('/locale', [LocaleController::class, 'update'])->name('locale.update');
|
||||
});
|
||||
|
||||
// Admin routes
|
||||
|
||||
Reference in New Issue
Block a user