Today we are going to make our project multilanguage, that is, it is in several languages (Spanish and English). Although we will simply apply it to static texts, not to those that are stored in the database.
The strings that will be displayed according to the language we select are saved in resources/lang . There we will see a en folder, which is where the English translations are. Create a folder called es, in my case, if you want another language, simply, create the folder with the language you want.
Once this is done, create both in en folder and es folder a file called header.php and will contain:
1 2 3 4 5 6 7 8 9 10 |
<?php return [ 'posts' => 'Posts', 'login' => 'Login', 'register' => 'Register', 'administration' => 'Administration', 'notifications' => 'Notifications', 'hascommented' => '<i>:user</i> has commented in <b>:post</b>', ]; |
1 2 3 4 5 6 7 8 9 10 |
<?php return [ 'posts' => 'Entradas', 'login' => 'Iniciar sesión', 'register' => 'Registro', 'administration' => 'Administración', 'notifications' => 'Notificaciones', 'hascommented' => '<i>:user</i> ha comentado en <b>:post</b>', ]; |
These will be the strings that will translate into the views. To access to these strings we can do it in different ways, I have used this one:
1 |
@lang('header.administration') |
First we indicate the name of the file and then the name of the text string.
Modify the header in layouts/app.blade.php to make it multilanguage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<li><a href="{{ route('posts.index') }}">@lang('header.posts')</a></li> @if (Auth::guest()) <li><a href="{{ route('login') }}">@lang('header.login')</a></li> <li><a href="{{ route('register') }}">@lang('header.register')</a></li> @else ... <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> @lang('header.notifications') <span class="badge">{{count(Auth::user()->unreadNotifications)}}</span> </a> ... @forelse (Auth::user()->unreadNotifications as $notification) <a href="{{ route('posts.show', $notification->data['post']['id']) }}">@lang('header.hascommented', ['user' => $notification->data["user"]["name"], 'post' => $notification->data["post"]["title"]])</a> @empty ... <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> @lang('header.administration') <span class="caret"></span> </a> ... |
Now we are going to create a middleware that will determine the language:
1 |
php artisan make:middleware SetLang |
Open it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace App\Http\Middleware; use Closure; use Session; use App; use Config; class SetLang { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (Session::has("lang")) { $lang = Session::get("lang"); } else { // check browser lang - https://learninglaravel.net/detect-and-change-language-on-the-fly-with-laravel $lang = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2); if ($lang != 'es' && $lang != 'en') { $lang = 'en'; } } App::setLocale($lang); return $next($request); } } |
This file what it does is checking if we have a session called lang, if we don’t have it, will take the browser language and will set it as app language.
Now, to apply it, we go to app/Http/Kernel.php :
1 2 3 4 5 6 7 |
protected $middlewareGroups = [ 'web' => [ ... \App\Http\Middleware\SetLang::class, ], ... ]; |
The only thing we need is to create the route with the middleware web (previously modified):
1 2 3 4 |
Route::get('lang/{lang}', function($lang) { \Session::put('lang', $lang); return \Redirect::back(); })->middleware('web')->name('change_lang'); |
And we add the links to change the language where we want:
1 2 |
<a href="{{ route('change_lang', ['lang' => 'es']) }}">ES</a> <a href="{{ route('change_lang', ['lang' => 'en']) }}">EN</a> |
-