IrfanMG avatar

Laravel Localization

irfanmg

Published: 28 Mar 2019 › Updated: 28 Mar 2019Laravel Localization

Laravel Localization

Laravel_Localization.jpg

  • Router
Route::get('/setting/lang/{locale}', function ($locale) {
    $validLocale = in_array($locale, ['id', 'en']);
    if ($validLocale) {
        Session::put('lang', $locale);
    }

    return back();
});
  • Middleware
$ php artisan make:middleware LanguangeMiddleware
  • Code LanguageMiddleware
<?php

namespace App\Http\Middleware;

use Closure;
use App, Session;

class LanguangeMiddleware
{
    public function handle($request, Closure $next)
    {
        App::setLocale(Session::get('lang'));
        return $next($request);
    }
}
  • open app/Http/Kernel.php
protected $routeMiddleware = [
        // ...
        'languange' => \App\Http\Middleware\LanguangeMiddleware::class,
    ];
  • use LanguangeMiddleware for router
Route::group(['middleware' => 'languange'], function()
{
     // your route
});
  • use in controller
public function store(Request $request)
    {
        // ...
        Session::flash('message', __('pos.item_saved'));
        // ..
    }
  • use in blade
<h1 class="h3 mb-0 text-gray-800">@lang('pos.create_item')</h1>

Laravel 5.8

Leave Laravel Localization to:

Written by

Special as my notes in building the application

Read more #laravel posts


Best Posts From IrfanMG

We have not curated any of irfanmg's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From IrfanMG