jeudi 18 août 2016

setting locale in Laravel to multilingual feature

I'm working on Laravel 5.1.40 LTS, I'm trying to implement the multilingual function in my site. So, in the resources/lang I have a folder for english and another for spanish translations, until here everything is ok. So, in the view I have a combobox to select which language want the user, this is my code:

{!! Form::open(array('url' => 'changelanguage'))  !!}
{!!Form::select('lang',['en'=>'en','es'=>'es'],Session::get('language',Config::get('app.locale')),['onchange'=>'submit()']) !!}
{!! Form::close() !!}

Then, in the controller I have:

public function postChangeLanguage()
    {
        $rules = [
            'language' => 'in:en,es' 
        ];

        $language = Input::get('lang'); //lang is name of form select field.

        $validator = Validator::make(compact($language),$rules);

        if($validator->passes())
        {
            Session::put('language',$language);
            App::setLocale($language);

            return Redirect::back();
        }
        else
        {   return Redirect::back();}
    }

So, I checked and here I have correctly the choosed language, and just after set the locale to check if the variable has change I put:

App::getLocale()

and this print correctly the value. But, in the view nothing changes, I put this in the view:

<h1>the locale</h1>

And always it prints 'en' for english. So it seems like in the controller the value is changed but it the view nothing changes. What should I check? what I'm missing? Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire