3

I tried to create a Form and do away with Ajax. Yes, the form at least does send the parameter down to the Controller as I var_dumped it and exited in order to see if it had gotten the new value, and yes it did, but guess what? and here is the question:

Even though the Input::get('locale') received from the Controller is the one I sent from the Form, the following code can't get to change the Session.

Controller:

public function languagechooser()
    {   
         $session = \Input::get('language');
         var_dump($session);exit;
         \Session::set('locale',$session);

         return\Redirect::back();
    }

The only way to change the session is hardcoding it, like this (notice the 'en':

public function languagechooser()
        {   
             $session = \Input::get('language');
             var_dump($session);exit;
             \Session::set('en');

             return\Redirect::back();
        }

but I dont understand why. Once it receives it from the variable, it should stay in there, but it looks that it does not. Is it a variable after all? But on youtube phpacademy does the same thing (just using Laravel 4) while I use 5

and the Form, just a Form

<form action="{!!URL::route('languagechooser')!!}" method  ="post">

<select class="form-control" name="language">
<option value="fr">fr</option>
<option value=en">en</option>
<option value="es">es</option>  
<option value="ru">ru</option>
<option value="it">it</option>
<option value="de">de</option>
</select>   
<button class="btn btn-primary pull-right" type="submit">Search</button>
    {!!Form::token()!!}
</form>
patricio
  • 350
  • 2
  • 5
  • 13
  • If you refresh the page after submitting the form does it give the correct locale. You did also try doing the `\Session::set('locale',$session);` before calling `exit` right? – haakym Jun 06 '15 at 19:33
  • I refresh the page but no change, that is the weird thing. Separately I added another var_dump on my home page, and it does show me correctly the changes in the session. Yet, the controller does not implement them on the fly. The thing is: the Form sends the Parameter to a separate File where I only have that Controller. This controller is hosted on a page where it never stays (thus the redirect::('back') it redirects to the Frontpage from where I clicked the choice of language. This Frontpage does have the var_dump too and it shows the changes any time I select a different language. – patricio Jun 06 '15 at 19:46
  • I'm going to try your code out now and see if I can get it working. BTW you do know laravel has its own way of dealing with localisation - you could leverage that instead of storing it in the session, check it out: http://laravel.com/docs/5.0/localization#introduction – haakym Jun 06 '15 at 19:53
  • yes, sure I have used that, I have created all of the files in the Lang directory, added the code in the kernel.php page etc that works fine, but the sessions have to be set on the fly as the users select their language of choice. shouldn't it? – patricio Jun 06 '15 at 19:57
  • Not entirely sure what the best way to set it is but I thought perhaps you were doing your own implementation and were unaware of it. I got something working so will post that up now. – haakym Jun 06 '15 at 19:59

1 Answers1

6

routes.php

Route::get('/', 'WelcomeController@index');

Route::post('languagechooser', [
    'as' => 'languagechooser',
    'uses' => 'WelcomeController@changeLanguage'
]);

view - welcome.blade.php

<!-- I think this bit should help you out! -->
<p>
    @if( Session::has('locale') )
        Locale: {{ Session::get('locale') }} <br>
        Message: {{ Lang::get('test.message') }}
    @else
        no session locale set
    @endif
</p>


<form action="{!! route('languagechooser') !!}" method = "post">

<select class="form-control" name="language">
    <option value="en">en</option>
    <option value="es">es</option>  
</select>

<button class="btn btn-primary pull-right" type="submit">Search</button>
    {!!Form::token()!!}
</form>

Controller - WelcomeController.php

public function changeLanguage()
{
    $lang = \Input::get('language');

    \Session::put('locale', $lang);

    return \Redirect::back();
}

Create middleware: php artisan make:middleware Locale

Middleware Locale.php

<?php namespace App\Http\Middleware;

use Closure;
use Session;
use App;
use Config;

class Locale {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $language = Session::get('locale', Config::get('app.locale'));

        App::setLocale($language);

        return $next($request);
    }

}

Added this: 'App\Http\Middleware\Locale' to $middleware array in the Http\Kernel.php file so it's loaded on each request.

protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        'App\Http\Middleware\Locale',
    ];

resources/lang/en/test.php

return [
    'message' => 'hello'
];

resources/lang/es/test.ph`

return [
    'message' => 'hola'
];

Credit to this link: https://laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app

haakym
  • 12,050
  • 12
  • 70
  • 98
  • alright, I see, I am going to try one second (شكرا جزيلا) – patricio Jun 06 '15 at 20:15
  • I can see that the session changes, just as the result of the var_dump did, but, have you tried to actually see a change in a text? – patricio Jun 06 '15 at 20:18
  • ليست مشكلة Oooh you mean the text (one that is effected by the locale) isn't changing! Sorry I'd misunderstood. I'm guessing it's because you need to do something like this: `App::setLocale('es');`, no? – haakym Jun 06 '15 at 20:36
  • it is done at the Controller, like this: \Session::set('locale', $session); isn't it? – patricio Jun 06 '15 at 20:39
  • Looking into this now. I am echoing out `{{ Lang::get('test.message') }}` to see if it changes on language change. Is this what I should be testing for, right? – haakym Jun 06 '15 at 21:24
  • yes, this is what I mean, in the Lang folder you create subdirectories for each language and inside there a file with the same name for each language and inside that file you creat one associative array where you write like a dictionary. Then you put that code you have written on your blade page and by magic, every time the Locale changes, the text also changes in the interface. I have not solved the problem yet but you have worked so hard that you deserve it by far, that is why I gave you the right answer. – patricio Jun 06 '15 at 21:32
  • Wowow you shouldn't give me the correct answer lol I don't deserve it yet what's more the answer isn't correct as even I can't get it working!!! Everytime I submit the form there's no change in the text at all - very odd! Please leave a comment if you find a solution and I'll be sure to update if I find one too then I'll update the answer. – haakym Jun 06 '15 at 21:37
  • FYI, I'm looking at this: https://laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app – haakym Jun 06 '15 at 21:38
  • Cisgo is probably some naming thing in their application I doubt it would effect it working or not. I tried the code from the link I posted and for some odd reason it won't work! Really strange.. perhaps it's too late for me. – haakym Jun 06 '15 at 21:56
  • Yeah thanks, I did add it to the middleware that gets called on each request, but I'll double check it! – haakym Jun 06 '15 at 21:56
  • btw you should mark the question as unanswered otherwise no one will pay attention to it, if you want to reward me for some effort you can vote the answer up – haakym Jun 06 '15 at 21:57
  • I think there is not even a need neither for the Ajax nor for the Form. I believe the process would be like this: You click on an href which takes to this:www.website.com/ru/ so then, the code in the Middleware will get invoked by a $request, will extract it and will shovel it over to the session. That is the most direct way I think. – patricio Jun 06 '15 at 21:59
  • If that fits to your application's structure go for it. Did you try out the code and get it working? – haakym Jun 06 '15 at 22:02
  • Working fine for me now, I'll update my question to the way it's working for me – haakym Jun 06 '15 at 22:07
  • 1
    If you want, tomorrow we can so some video conferencing where it is possible to see the code of the other person without any risk of messing accidentally. I use webex I will try the code tomorrow as now it is too late. You got it working, that is great! – patricio Jun 06 '15 at 22:11
  • Sure. No worries. Just keep me posted with how you're getting on. Hope you can get it working. – haakym Jun 06 '15 at 22:13
  • Yes, anytime I change the value and click submit the text for message will change to hello or hola – haakym Jun 06 '15 at 22:26
  • do you get the changes back and forth as you select es and en and then back to es? My emails is: vervideos@list.ru so that we I ll show you what I have and will see if if the other way also works out. I give you now the correct answer as it works for you. – patricio Jun 06 '15 at 22:29
  • Okay I'll zip the file and email it to you – haakym Jun 06 '15 at 22:31