how to define the last translation used by the user on the platform

Asked

Viewed 23 times

-2

I have a platform with 6 different languages and I need to keep the last language selected for the user on the platform, but I don’t know how to do it, how can I do it?

for example:

if the user uses the platform in French last, when he logs in again, the platform should remain in French

5.8 washable

1 answer

1

Saves a cache with the user’s login when he changes the language, for example

Cache::put(auth()->user()->username.'_lang', $linguaEscolhida);

Then it implements a middleware that defines the language for each user request, for example the Handle method of middleware would be:

public function handle($request, Closure $next)
{
    if (auth()->check()) {
        $lang = Cache::get(auth()->user()->username.'_lang', config('app.locale'));
        App::setLocale($lang);
    }

    return $next($request);
}

In the example if the user is authenticated then retrieves from the cache his input to the chosen language, or returns the default locale configured in the application if there is no cache for the user.

Next set the locale with App::setLocale

Browser other questions tagged

You are not signed in. Login or sign up in order to post.