. Hello, world!

Step 1:

you need to publish lang directory in laravel

php artisan lang:publish

then make folder in your laravel lang folder and inside them you can make .php files or json but for the larger data its recommended to user json files

now make a files in your resourse folder like below

and in your welcome.blade file we will write our logic like below

<!DOCTYPE html>
<html lang="{{ config('localization.locale_lang.' . app()->getLocale()) }}">
<head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>Laravel</title>
   <link href="{{ asset('styles.css') }}" rel="stylesheet">
</head>
<body>
<nav>
   <div class="centered-text">
       <h1>Laravel Localization</h1>
       <p>{{ __('page.hello_world') }}</p>
   </div>
   <ul class="drop-down closed">
       <li class="cursor-pointer">
           <p class="nav-button">Change Language</p>
       </li>
       @foreach(config('localization.locales') as $locale)
           <li data-locale="{{ $locale }}" class="ss-change-locale">
               <a href="javascript:void(0)">
                   {{ __('page.locales.name.' . $locale) }}
               </a>
           </li>
       @endforeach
   </ul>
   <div class="page-content">
       @yield('content')
   </div>
</nav>
{{--<script src="{{ asset('scripts.js') }}"></script>--}}
<script>
   (function() {
       // Bind Click event to the dropdown navigation button
       document.querySelector('.nav-button').addEventListener('click', function() {
           // Toggle the CSS closed class which reduces the height of the UL thus hiding all LI apart from the first
           this.parentNode.parentNode.classList.toggle('closed')
       }, false);
   })();

   // SWITCH LANGUAGE
   let currentUri = window.location.pathname;
   let locales = "{{ implode('|', config('localization.locales')) }}";
   let currentLocale = "{{ app()->getLocale() }}";
   let elements = document.querySelectorAll('.ss-change-locale');
   for (let i = 0; i < elements.length; i++) {
       elements[i].addEventListener('click', function () {
           let locale = this.getAttribute('data-locale');
           if (locale === currentLocale) {
               return;
           }
           let newUri = currentUri.replace(new RegExp('^\/(' + locales + ')'), '/' + locale);
           let paramsIndex = window.location.href.indexOf('?');
           let params = (paramsIndex !== -1 ? window.location.href.slice(paramsIndex) : '')
           let port = window.location.port ? ':' + window.location.port : '';
           window.location.href = window.location.protocol + '//' + window.location.hostname + port + newUri + params;
       });
   }
</script>
</body>
</html>

 

now home.blade.php


@extends('welcome')
@section('content')
   <p class="centered-text">{{ __('page.home') }}</p>
   <div class="centered-text">
       <a href="{{ route('about') }}" class="button">{{ __('page.about') }}</a>
       <a href="{{ route('contact') }}" class="button">{{ __('page.contact') }}</a>
       <a href="{{ route('dashboard') }}" class="button">{{ __('page.dashboard') }}</a>
   </div>
@endsection

About.blade

@extends('welcome')
@section('content')
   <p class="centered-text">{{ __('page.about') }}</p>
   <div class="centered-text">
       <a href="{{ route('home') }}" class="button">{{ __('page.home') }}</a>
       <a href="{{ route('contact') }}" class="button">{{ __('page.contact') }}</a>
       <a href="{{ route('dashboard') }}" class="button">{{ __('page.dashboard') }}</a>
   </div>
@endsection

Contact.blade


@extends('welcome')
@section('content')
   <p class="centered-text">{{ __('page.contact') }}</p>
   <div class="centered-text">
       <a href="{{ route('home') }}" class="button">{{ __('page.home') }}</a>
       <a href="{{ route('about') }}" class="button">{{ __('page.about') }}</a>
       <a href="{{ route('dashboard') }}" class="button">{{ __('page.dashboard') }}</a>
   </div>
@endsection

dashboard.blade

@extends('welcome')
@section('content')
   <p class="centered-text">{{ __('page.dashboard') }}</p>
   <div class="centered-text">
       <a href="{{ route('home') }}" class="button">{{ __('page.home') }}</a>
       <a href="{{ route('about') }}" class="button">{{ __('page.about') }}</a>
       <a href="{{ route('contact') }}" class="button">{{ __('page.contact') }}</a>
   </div>
@endsection

 

Now make a localization.php file inside the config folder 


<?php
return [
   'locales' => [
       'en',
       'cn',
       'am',
   ],
   'locale_lang' => [
       'en' => 'en_US',
       'cn' => 'zh_CN',
       'am' => 'hy_AM',
   ],
];

now make a middleware 

php artisan make:middleware Localization

now in the middleware

$first_segment = $request->segment(1);
       if (in_array($first_segment, config('localization.locales'))) {

           App::setLocale($first_segment);
           URL::defaults(['locale' => $first_segment]);
//            dd($request->url());
           return $next($request);
       } else {
           $fallback_locale = config('app.fallback_locale');
           $segments = $request->segments();
           array_unshift($segments, $fallback_locale);
           return Redirect::to(implode('/', $segments));
       }

now in bootstrap→app.php register middleware 

->withMiddleware(function (Middleware $middleware) {
//        \App\Http\Middleware\Localization::class,
         $middleware->alias([
             'Localization' => \App\Http\Middleware\Localization::class,
         ]);
   })

The last step in web.php


<?php
use Illuminate\Support\Facades\Route;
Route::group([
   'prefix' => '{locale?}',
   // YOU CAN CUSTOMIZE THESE OPTIONS
   // 'as' => '',
    'middleware' => ['Localization'],
   // 'namespace' => 'App\Http\Controllers',
], function () {
   Route::get('/', function () {
       return view('pages.home');
   })->name('home');
   Route::get('/about', function () {
       return view('pages.about');
   })->name('about');
   Route::get('/contact', function () {
       return view('pages.contact');
   })->name('contact');
   // ...
});
Route::group([
   'prefix' => '{locale?}/dashboard',
   // YOU CAN CUSTOMIZE THESE OPTIONS
   // 'as' => 'dashboard.',
   // 'middleware' => ['web', 'admin'],
   // 'namespace' => 'App\Http\Controllers\Admin',
], function () {
   Route::get('/', function () {
       return view('pages.dashboard');
   })->name('dashboard');
   // ...
});