In this tutorial, we will learn by example what localization is in Laravel. We will learn Laravel Localization with an example in this guide.

Displaying a webpage or app in the user’s local language helps to target audiences from different regions and cultures across the globe. In Laravel, you can implement language translation through Laravel Localization. Laravel localization allows us to translate an app or site into multiple languages.

Let’s get more idea about Laravel localization in detail with example.

Steps to Implement Localization in Laravel:

Step 1: First, create a locale file in the resources/lang directory of the Laravel project. You must create files according to the number of languages you are using. Here we are creating two files in the resources/lang directory for English and French language with the content below.

resources/lang/en.json 

resources/lang/fr.json

Step 2: Now, create a route in the route/web.php file for changing/switching language for the app. We store selected language in session.

route/web.php

Step 3: Now, you need to create one middleware to set the app/site locale language. In Laravel, php artisan make:middleware {name} command is used to create middleware.

Run the following command at the root of your project

Step 4: See the app/Http/Middleware/LocaLocalization.php file and add the following code to set the app locale language. We are getting the current locale from the session that we store in above.

Your app/Http/Middleware/LocaLocalization.php looks like this.

Step 5: Add that middleware into your app/Http/Kernel.php to web middleware allies.

Step 6: Lastly, create one view file and route for it to see the final output.

resources/views/locale.blade.php

Output:

Now see the beautiful magic at http://127.0.0.1:8000/ 

English

 

English

French

French

Conclusion:

This way, you can implement localization in Laravel. To customize your Laravel project, Hire a Dedicated Laravel Developer. If you have any doubts, let me know through the comment box. Share the article with your friends and stay in touch with us.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]