I have tried several tutorials in the internet but it doesn’t work or the language not changing, I already stored the translation into \lang
directory, and the session is already set Localization Session
here is my source code:
This is my controller :
class LocalizationController extends Controller
{
public function setLang($locale)
{
App::setLocale($locale);
Session::put("locale", $locale);
return redirect()->back();
}
}
This is my Middleware :
class LocalizationMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (Session::get("locale") != null) {
App::setLocale(Session::get("locale"));
} else {
Session::put("locale", "en");
App::setLocale(Session::get("locale"));
}
return $next($request);
}
}
This is the route :
Route::get("locale/{lang}", [LocalizationController::class, 'setLang']);
Language Switcher :
<a href="{{ url('locale/id') }}"><img class="h-5 mt-4" src="{{ asset('images/flag/id_flag.png') }}"
alt=""></a>
<a href="{{ url('locale/en') }}"><img class="h-5 mt-4" src="{{ asset('images/flag/uk_flag.png') }}"
alt=""></a>
Translated Element :
<h1 class="font-black lg:text-5xl md:text-4xl text-3xl md:w-[500px] mx-auto">{{ __('home.hero.header') }}</h1>
I have tried several tutorials in the internet, please help.