0
votes

Am working on multi-lingual support for my laravel app, all the materials have seen online say that passing the locale to the App facade and calling the setLocale method translates the website, I have tried it but no luck.

In my routes file, I have this

Route::group(['middleware' => ['web']], function () {
  Route::get('change-locale', ['uses' => 'HomeController@changeLocale', 'as' => 'locale.change']);
});

HomeController@changeLocale I have...

namespace Gudaapp\Http\Controllers;
use Illuminate\Http\Request;

use Gudaapp\Http\Requests;
use Gudaapp\Http\Controllers\Controller;
class HomeController extends Controller
{
public function changeLocale(Request $request) {
    if(empty($request->locale)) { redirect()->back()->withMessage('Unknown locale, please, if problem persists, contact admin.'); }
    session()->put('locale', $request->locale);
    return redirect()->back()->withMessage('Your locale has now been changed to <b>'.$request->locale.'</b>')->withMessageType('success');
}
}

and in the view that sends the request..

<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
   @foreach(config('app.languages') as $locale => $lang)
       <li class="{{ session('locale') == $locale ? 'active' : '' }}">
           <a href="{!! route('locale.change', ['locale'=> $locale]) !!}" ><img src='{!! asset($lang["flag"]) !!}' alt="" />&nbsp;{{ $lang['language'] }}</a>
        </li>
  @endforeach
</ul>

I always get the notification that it was succesful, but App::getLocale() still remains the default and therefore the website doesn't get translated. I need help.

1

1 Answers

0
votes

Are you also configuring the locale from the session when the Application is booting? You could do this in the boot method of a Service Provider, e.g:

// app/Providers/AppServiceProvider.php
public function boot()
{
    $locale = $this->app['session']->get('locale'); // This would be the same as session()->get(... and Session::get(...
    $this->app->setLocale($locale); // This would also equal app()->setLocale(... and App::setLocale(...
}

You might want to do some extra here to assure the locale is valid.