for first step you should publish the language
php artisan lang:publish
then for step 2 create the language middleware
class Language
{
public function handle($request, Closure $next)
{
App::setLocale(session()->get('language') ?? 'en');
return $next($request);
}
}
for step 3 register you middleware to the kernel.php file
protected $middlewareGroups = [
'web' => [
...other middelwares
Language::class, //add your language middleware here
],
//...
];
for step 4 create your route
Route::get('/change-language/{lang}', 'HomeController@changeLanguage');
and then for last step set your controller func
public function changeLanguage($lang)
{
if (!in_array($lang, ['en', 'de'])) {
abort(400);
}
session(['language' => $lang]);
return back();
}
and now in your lang directory you can create anoter directory with symbol name of language you want, like (de) for german.
note: in the en or de directory you should create the PHP file like (messages.php) and set your keys and value.
in the blade file you must point to the desired key in the messages file:
<div>{{ __('messages.hello-world') }}</div>
done.
Top comments (0)