Hello Developers
In this brand new tutorial i am coming with a very helpful tutorial like how to create multilingual website in laravel 7.x. In this laravel multilingual example tutorial i will discuss from scratch so that you can understand better.
Now i am working on project name point of sale. In this project i have implemented multilingual system. This is very basic multilingual system. Now i am going to share with you laravel multilingual website example from scratch.
I will use two languages, one is default english and other is spanish. Just i would like to show you that how you can create it. In this example, I will simply use laravel default trans helper function to use multiple language in laravel. I will create three different language English and Spenish. then we manage it my one dropdown and middleware.
So, let's follow few step to create multi language support in your laravel application.
Step 1: Install Laravel 7.x
In this first step, if you haven't laravel application setup then we have to get fresh laravel 7.x application. So run bellow command and get clean fresh laravel 7.x application.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Lang Files
In this step, we simply need to create following folders and files for english and spanish language files in lang folder located in the following directory.
let's create files as like below listed:
resources/lang/en/sentence.php
return [
'dashboard' => 'Dashboard',
'manage_users' => 'Manage Users',
'user_list' => "User List",
'manage_supplier' => 'Manage Supplier',
'supplier_list' => 'Supplier List',
'manage_customer' => 'Manage Customer',
'customer_list' => 'Customer List',
'manage_unit' => 'Manage Unit',
'unit_list' => 'Unit List',
'category' => 'Category',
'category_list' => 'Category List',
'manage_purchase' => 'Manage Purchase',
'purchase_list' => 'Pending List',
'approve_purchase' => 'Approve List',
'add_purchase' => 'Add Purchase',
'manage_product' => 'Manage Product',
'product_list' => 'Product List',
'manage_invoice' => 'Manage Invoice',
'pending_invoice' => 'Pending Invoice',
'approve_invoice' => 'Approve Invoice',
'add_invoice' => 'Add Invoice',
'daily_invoice' => 'Daily Invoice',
'manage_stock' => 'Manage Stock',
'product_stock' => 'Product Stock',
'stock_details' => 'Stock Details',
'daily_purchase_report' => 'Daily Purchase Report',
'logout' => 'Logout',
'point_of_sale' => 'POINT OF SALE',
'customer_credit'=> 'Credit Report',
];
Now create another file for spanish language
resources/lang/es/sentence.php
return [
'dashboard' => 'Tablero',
'manage_users' => 'Administrar usuarios',
'user_list' => 'Lista de usuarios',
'manage_supplier' => 'Administrar prov',
'supplier_list' => 'Lista de proveedores',
'manage_customer' => 'Gestionar cliente',
'customer_list' => 'Lista de clientes',
'manage_unit' => 'Administrar unidad',
'unit_list' => 'Lista de unidades',
'category' => 'Categoría',
'category_list' => 'Lista de categorías',
'manage_purchase' => 'Administrar compra',
'purchase_list' => 'Lista de compras',
'approve_purchase' => 'Aprobar lista',
'add_purchase' => 'Añadir compra',
'manage_product' => 'Administrar producto',
'product_list' => 'Lista de productos',
'manage_invoice' => 'Administrar factura',
'pending_invoice' => 'Factura pendiente',
'approve_invoice' => 'Aprobar factura',
'add_invoice' => 'Añadir factura',
'daily_invoice' => 'Factura diaria',
'manage_stock' => 'Gestionar stock',
'product_stock' => 'Stock de productos',
'stock_details' => 'Detalles de stock',
'daily_purchase_report' => 'Informe diario de compras',
'logout' => 'Cerrar sesión',
'point_of_sale' => 'PUNTO DE VENTA',
'customer_credit'=> 'Informe de crédito de clientes',
];
Read also : Laravel 7.x Dynamically Add more Input Fields using Handlebars Js
In this step we need to create our routes to add laravel multi language with language dropdown system in our laravel app.
Step 3: Create Routes
In this step, we will create one routes one for display page with language dropdown and create our logic.
so add bellow routes.
routes/web.php
if ( file_exists( app_path( 'Http/Controllers/LocalizationController.php') ) )
{
Route::get('lang/{locale}', 'LocalizationController@lang');
}
Step 4: Create LocalizationController Controller
In this point, now we should create new controller as LocalizationController. this controller will manage all the logic, so put bellow content in controller file:
app/Http/Controllers/LocalizationController.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class LocalizationController extends Controller
{
public function lang($locale)
{
App::setLocale($locale);
session()->put('locale', $locale);
return redirect()->back();
}
}
Step 5: Create View
In this we need to create our view for language dropdown list. So paste this code whre you want to display.
Step 6: Create Middleware
In this file we need to create one middleware that will manage dynamic language that we selected on dropdown. so let's create middleware using bellow language.
php artisan make:middleware Localization
Now you have to update middleware file like bellow:
app/Http/Middleware/Localization.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\App;
class Localization
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (session()->has('locale')) {
App::setLocale(session()->get('locale'));
}
return $next($request);
}
}
Now we need to register it to kernel file in web routes.. Because we need to set it globally. so let's add it as bellow:
app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Localization::class,
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Read also : Dynamically Add or Remove Input Fields using JQuery in Laravel 7.x
Now we have to print our selected language. So paste this below code in your blade file.
{{ trans('sentence.point_of_sale')}}
{{ trans('sentence.manage_users')}}
{{ trans('sentence.user_list')}}
{{ trans('sentence.manage_supplier')}}
.
.
....
Hope you got the point that how to print it where is your static words which you want to trnaslate into other language. Now everything is set to go. Hope it can help you.
#laravel #laravel-7x #laravel-7 #localizations #multilingual-site #multilingual #multi-language