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
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
The route
The middlewere
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);
}
}