How to Create Multi Language Website in Laravel

WebSolutionStuff | Nov-09-2020 | Categories : Laravel jQuery

Hello friends,

Today I will give you example of how to create multi language website in laravel. In this example or tutorial you can understand a concept of laravel multilingual website example demo. Also gain knowledge or learn about how to add multiple languages in laravel. it's very simple example of laravel multi language with different list of language dropdown.

So let's start and follow steps and perform example of multi language website in laravel project. Also you can implement this example in laravel 6, laravel 7 and laravel 8.

If you are looking for multi language support website in laravel then I will help you how to add multiple language in laravel. we will use laravel trans() to create Multilingual language website. I already written post on laravel localization.

 

 

In this example I will show you localization - laravel localization example.

Laravel's localization features provide a convenient way to retrieve text in diffrent languages, allowing you to easily support multiple languages within your application. So here i will show you how to create localization or laravel dynamic language.

 

So, let's follow steps to adding multi languages website support in laravel.

Step 1: Install Laravel

We are creating new project setup for this example , So create new project using below command.

composer create-project --prefer-dist laravel/laravel blog

Laravel's localization features provide a convenient way to retrieve text in diffrent languages, allowing you to easily support multiple languages within your application. So here i will show you how to create localization or laravel dynamic language.

Step 2 : Create Localization File

In this step we will Create localization file in language wise. here I create two file First one for English and Second one is Chinese. And just copy and paste below code in given path and write down your custom message or value.

 

resources/lang/en/message.php

<?php

return [
    'welcome' => 'This is a welcome message !',
];

 

resources/lang/zhh/message.php

<?php

return [
    'welcome' => '这是一个欢迎信息 !',
];

 

Step 3 : Add Route in web.php

In this step, we will create two routes one for display view page with language dropdown and message and another for change languages using jquery.

Now, add below route in your routes/web.php file.

Route::get('index', '[email protected]');
Route::get('change/lang', '[email protected]_change')->name('LangChange');

 

 

Step 4 : Create Controller

In this step, now we create new controller as a LocalizationController. This controller help to manage layouts and change language dynamically using dropdown.

Copy below code in your terminal to create new controller.

php artisan make:controller LocalizationController

 

Now, add below code in your LocalizationController.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App;

class LocalizationController extends Controller
{
    public function index()
    {
        return view('welcome');
    }
    public function lang_change(Request $request)
    {
        App::setLocale($request->lang);
        session()->put('locale', $request->lang);  
        return view('welcome');
    }
}

 

Step 5 : Create Blade File

In this step we will creat blade file for view our output. Copy below code and paste in blade file.

<!DOCTYPE html>
<html>
<head>
    <title>How to Create Multi Language Website in Laravel - websolutionstuff.com</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <div class="container">          
        <div class="row" style="text-align: center;margin-top: 40px;">
            <h2>How to Create Multi Language Website in Laravel - websolutionstuff.com</h2><br>
            <div class="col-md-2 col-md-offset-3 text-right">
                <strong>Select Language: </strong>
            </div>
            <div class="col-md-4">
                <select class="form-control Langchange">
                    <option value="en" {{ session()->get('locale') == 'en' ? 'selected' : '' }}>English</option>
                    <option value="zhh" {{ session()->get('locale') == 'zhh' ? 'selected' : '' }}>Chinese</option>                    
                </select>
            </div>
            <h1 style="margin-top: 80px;">{{ __('message.welcome') }}</h1>
        </div>             
    </div>
</body>
  
<script type="text/javascript">  
    var url = "{{ route('LangChange') }}";
    $(".Langchange").change(function(){
        window.location.href = url + "?lang="+ $(this).val();
    });  
</script>
</html>

 

Here you can display string or message using diffrent way in laravel.

trans()

@lang()
 __()
@json(__())

 

Run this example in your browser and get output like below screenshot.
  

http://localhost:8000/index

 

English Language

How to Create Multi Language Website English

 

Chinese Language

How To Create Multi Language Website Chinese

 

Featured Post



Follow us
facebooklogo github instagram twitter