rajakhoury
rajakhoury

I added a column to my User's table lang_code so the user can select from his account settings the default language.

Then added the locale picker to my layout and checked the Force Url Schema to prefix routes with the locale code. Instead of displaying a local picker, I want the User to select the language and save the lang code in the user's table not rely on the Session..

How do you extend the Translate plugin to look for this value ? I can see in the routes.php there is a loadLocaleFromRequest() and loadLocaleFromSession() what I need is something like loadLocaleFromUserSettings() - Check the user's locale with a fallback to the default one set in the backend.

rajakhoury
rajakhoury

I added this to my User plugin boot function

use RainLab\Translate\Classes\Translator;
public $require = ['RainLab.Translate'];
public function boot()
{
    /* Add User locale to session **/

    App::before(function($request) {
        if (App::runningInBackend()) {
            return;
        }
        if (!Auth::check()) {
            return ;
        }

        if($user = Auth::getUser()){
            $translator = Translator::instance();
            $translator->setLocale($user->lang_code);
        }
    });

}

It seems to work fine - Is there any downside of doing this ? Thank you

Last updated

1-2 of 2