Search code examples
phplaravellaravel-4

Laravel language settings per user


I know how to set default language in application by using App::setLocale('es');

I'm thinking about setting individual language per user after an user logs in.

Currently, the only way I have in mind is to set a general language and use variable inside a Lang::get() command:

$user_language = 'gr';

Lang::get('messages.welcome'.$user_language);

Is there any other way of setting language setting per user?


Solution

  • Why not just use App::setLocale() to set the language according to the user preferences if the user is logged in. According to the Laravel Docs:

    You may change the active language at any time using the App::setLocale method.

    So you could do that maybe like this:

    App::before(function($request)
    {
        // If user is logged in
        if (Auth::check())
        {
            // Get the user specific language
            $lang = Auth::user()->language;
    
            // Set the language
            App::setLocale($lang);
        }
    });