In this tutorial, we are going to learn how to implement a multilingual website using Laravel. This localization feature provide facility to retrieve strings in various languages by allowing us to support the multiple languages within our application.
Prerequisites
Laravel
To Continue with this tutorial, you should have installed Laravel 8 in your pc. If you are still not installed Laravel in your machine you can configure it from here.
Setting up the Project
First you need to create a new laravel project by running below command in your terminal
Then navigate to your project directory by using below command in your terminal
1
cd laravel-localization
This command will create a fresh laravel project for us. In this tutorail, we don’t need to connect our application to Database because we work with the front-end. By default, Laravel’s local language is en. You can find it on the 'locale' => 'en' section in the config->app.php file.
Creating Translation Files
In this tutorial, I am going to add four more languages apart from English.
Spanish
French
Japanese
Sinhala
Now you should need to create a seperate folder for each langauge inside the resources -> lang folder. To find naming conventions for your folders,please refer this.
Example: Spanish -> es
Now, inside all of the five folders, create a file called message.php and add the following code.
en -> message.php file:
1 2 3 4 5
<?php
return [ 'welcome' => 'Welcome to Laravel Localization' ];
es -> message.php file:
1 2 3 4 5
<?php
return [ 'welcome' => 'Bienvenido a Laravel Localization' ];
fr -> message.php file:
1 2 3 4 5
<?php
return [ 'welcome' => 'Bienvenue dans la localisation de Laravel' ];
You can put many texts as you want. I have just taken one text for demostration. Now we have already placed all thetranslations in place, let’s start working on the views and make our application load above translations.
Setup the view
Inside resources -> views folder create, folder called layouts. Then create an app.blade.php file inside i and add the following code.
function minimizeMenu() { if (window.matchMedia('(min-width: 992px)').matches && window.matchMedia('(max-width: 1299px)') .matches) { // show only the icons and hide left menu label by default $('.menu-item-label,.menu-item-arrow').addClass('op-lg-0-force d-lg-none'); $('body').addClass('collapsed-menu'); $('.show-sub + .br-menu-sub').slideUp(); } else if (window.matchMedia('(min-width: 1300px)').matches && !$('body').hasClass( 'collapsed-menu')) { $('.menu-item-label,.menu-item-arrow').removeClass('op-lg-0-force d-lg-none'); $('body').removeClass('collapsed-menu'); $('.show-sub + .br-menu-sub').slideDown(); } } });
</script> </body> </html>
Note: Here I have used a premium and licensed template to get a better and interactive output from our application. If you need to use this template you need to purchase it from it's respective owners.
Next, you need to create a folder called inc in layouts directory and create two seperate file called left_nav.blade.php and top_navbar.blade.php and paste below codes:
Now, create a middleware for using the following command:
1
php artisan make:middleware Localization
It will create a file called Localization.php in app -> Http -> Middleware. then you should add below code inside it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
namespace App\Http\Middleware;
use Closure; use Illuminate\Http\Request; use App;
class Localization { public function handle($request, Closure $next) { if (session()->has('locale')) { App::setlocale(session()->get('locale')); } return $next($request); } }
Register the Localization Middleware
Now add the middleware in App\Http\Kernel‘s $middlewareGroup’s array like this:
Now we have already finished our application. Now we can run your application using below command:
1
php artisan serve
Now, navigate to http://localhost:8000 in your browser, and you will see the drop-down with the five languages.Select the language, and you will see the welcome message in a different languages.
Conclusion
In this tutorial, we implemented multilingual web application in Laravel. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail. You can obtain complete source code and asset files for this tutorial from this GitHub repository.