This is insane, never do that. URL should practically never change they should be static, not dynamic like you trying to do.
Translating URLs using array keys of lang ressource
Hello, I am trying to create dynamically localized and named routes in my Laravel application using translation keys as PHP arrays. But they are not working as expected.
Locales :
// In app.php
'locale' => 'en',
'available_locales' => ['en','fr','ar','de'],
'fallback_locale' => 'en',
// In web.php route
Route::prefix('/{locale}', function ($locale = null) {
if (isset($locale) && in_array($locale, config('app.available_locales'))) {
app()->setLocale($locale);
} else {
app()->setLocale(config('app.fallback_locale'));
}
Route::get('/'.__('url.home'), function () {
return ('url.home_message');
});
});
Route::get(__('url.home'), function () {
Route::get(__('url.home'), function () {
return ('url.home_message');
});
});
When i prefix the url with lang code laravel always return page not found, exemple url like site.com/en/homa
When i remove the language code site.com/homafrom the parameters laravel return blank page with favicon but only and only if the URI match with the locale in app.php
locale en:
site.com/home <- blank page
site.com/accueil <- 404
site.com/en/home <- 404
site.com/fr/accueil <- 404
lang/fr/url.php:
return [
"home"=> "accueil",
"home_message" => "Bienvenue :) ",
];
lang/en/url.php:
return [
"home"=> "home",
"home_message" => "Hi :) ",
];
How can I correctly set up route localization so that URLs change based on the user's locale like in CodeIgniter4 ?
Please or to participate in this conversation.