mardi 26 septembre 2017

Laravel 5.5 multi language application

I followed this article on how to make your site multi language. But I don't understand why I always get an error when I surf to http://ift.tt/2hwiXJt for example.

The error:

Sorry, the page that you're looking for does not exist

My LanguageController

use Config;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;


class LanguageController extends Controller
{
    public function switchLang($lang)
    {
        if (array_key_exists($lang, Config::get('languages'))) {
            Session::put('applocale', $lang);
        }
        return Redirect::back();
    }
}

My routes:

Route::get('lang/{lang}', ['as'=>'lang.switch', 'uses'=>'LanguageController@switchLang']);

My Language Middleware

use Closure;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;

class Language
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
            App::setLocale(Session::get('applocale'));
        }

        return $next($request);
    }
}

I've also added this middleware to my Kernel.php file

My Languages.php file in the config folder

return [
    'en' => 'English',
    'nl' => 'Nederlands',
    'fr' => 'Français',
];



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire