Adding multi-language functionality in a website developed in laravel.

Hello Everyone,
Today I will share with you how do add multi language to a website that was developed in laravel, my version is 5.6 but it will work on 5.7 as well.

So…here we go!

First of all, I assume that you already have a working version of laravel. Navigate to that particular folder and open your terminal or command prompt.

Run the following command in order to create the required middleware.

 

Then open it. The file should be in “project_name/app/Http/Middleware/Localization.php

Update the code that is present with the one below :

Lets say you want your website to be in English and French.

  1. Go to “project_name/resources/lang/en/
  2. Create a file name “messages.php
  3. Create folder “fr” in “project_name/resources/lang/
  4. Create a file name “messages.php” in “project_name/resources/lang/fr/

Open “messages.php” in both folder and insert the following codes.

messages.php” in ‘en‘ folder.

messages.php” in ‘fr‘ folder.

Most important…

Add the below codes in “project_name/app/Http/Kernel.php” in “protected $middlewareGroups section”.

So your codes should be like this :

 

Now all this code in the route, “project_name/routes/web.php” on top of other defined routes.

 

So in order to know the language that is being use, you can simply use :

 

Changing language en/fr

To change the language, just create an anchor tag in html like the following :

 

To call the desired text :

 

 

 

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Gửi email cho tôi

Hotline
0396.504.701
GÓP Ý
CỦA BẠN
Hotline
0396.504.701
ĐĂNG KÝ
NHẬN THÔNG TIN