F
Filamentβ€’7mo ago
Soundmit

Please help with frontend translation

I've decided to quit my 17 years job as developer because i can't find a way to translate a laravel frontend πŸ™‚ Joke aside, I'm able (80%) to set the language in the session with a language switcher but i can't redirect to the translated page... i use spatie/translatable and i have a page with en slug: test-en it slug: test-it When in the english page i try to translate in italian but the slug doesn't change the language switcher
@foreach (config('app.supported_locales') as $locale => $language)
<a href="{{ route('locale', ['locale' => $locale, 'slug' => request()->path()]) }}">
{{ $language['name'] }}&nbsp;
</a>
@endforeach
@foreach (config('app.supported_locales') as $locale => $language)
<a href="{{ route('locale', ['locale' => $locale, 'slug' => request()->path()]) }}">
{{ $language['name'] }}&nbsp;
</a>
@endforeach
the link produced https://test.ddev.site/language/en/test-en https://test.ddev.site/language/it/test-en i think that the links need to be https://test.ddev.site/test-en https://test.ddev.site/test-it
1 Reply
Soundmit
SoundmitOPβ€’7mo ago
The route

Route::get('/language/{locale}/{slug?}', function ($locale, $slug = '') {
Log::info('Session locale before: ' . session('locale'));
if (array_key_exists($locale, config('app.supported_locales'))) {
session()->put('locale', $locale);

//find the page in the new locale
if ($slug) {
$page = Page::where("slug->" . session('locale'), $slug)->first();
if ($page) {
$newSlug = $page->slug[$locale];
dd($newSlug);
return redirect()->route('page.default', ['slug' => $newSlug]);
}
}
}
Log::info('Locale set to ' . $locale);
Log::info('Session locale after: ' . session('locale'));
return redirect()->route('page.default', ['slug' => $slug]);
})->middleware(setLocale::class)->name('locale');

Route::get('/language/{locale}/{slug?}', function ($locale, $slug = '') {
Log::info('Session locale before: ' . session('locale'));
if (array_key_exists($locale, config('app.supported_locales'))) {
session()->put('locale', $locale);

//find the page in the new locale
if ($slug) {
$page = Page::where("slug->" . session('locale'), $slug)->first();
if ($page) {
$newSlug = $page->slug[$locale];
dd($newSlug);
return redirect()->route('page.default', ['slug' => $newSlug]);
}
}
}
Log::info('Locale set to ' . $locale);
Log::info('Session locale after: ' . session('locale'));
return redirect()->route('page.default', ['slug' => $slug]);
})->middleware(setLocale::class)->name('locale');
The middlewere

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SetLocale
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (session()->has('locale') && array_key_exists(session('locale'), config('app.supported_locales'))) {
app()->setLocale(session('locale'));
}
return $next($request);
}
}

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SetLocale
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (session()->has('locale') && array_key_exists(session('locale'), config('app.supported_locales'))) {
app()->setLocale(session('locale'));
}
return $next($request);
}
}
the Page controller class PageController extends Controller { public function show($slug = '') { try { // Recupera la lingua corrente dalla sessione, se disponibile $locale = session('locale', app()->getLocale()); // Cerca la pagina in base allo slug nella lingua corrente $page = Page::where("slug->{$locale}", $slug)->firstOrFail(); return view('pages.default', compact('page')); } catch (ModelNotFoundException $e) { // Se la pagina non viene trovata, controlla se esiste in altre lingue foreach (config('app.supported_locales') as $locale => $language) { $page = Page::where("slug->{$locale}", $slug)->first(); if ($page) { // Reindirizza alla pagina nella lingua corretta return redirect()->route('page.default', ['slug' => $page->slug[$locale]]); } } abort(404); } catch (\Exception $e) { abort(500); } }
Want results from more Discord servers?
Add your server