You are on page 1of 12

Laravel Localizaton

लारवेल लोकललज़टन
Локализация Laravel
లారావెల్ స్థానికీకరణ

Akash Pundir
Localization

Localization aims to make the product or content


feel as native and familiar to the end-users in a
particular region or culture as possible. It goes
beyond simple translation and involves various
aspects, including language, date and time
formats, currency, graphics, and more.
In Config/App.php
This is your default language

'locale' => 'en'


Let’s add languages
In resources folder make lang/en/messages.php
<?
// resources/lang/en/messages.php
return [
'welcome' => 'How are you?',
];
?>
In resources folder make
lang/hi/messages.php

<?php
return [
'welcome' => 'आप कैसे हैं?'

];
?>
In resources folder make
lang/fr/messages.php
<?php

return [
'welcome'=>'Comment vas-tu?'
];

?>
In Web.php, let’s make a route for it

Route::get('/{lang?}', function () {
return view('welcome');
});
Do you remember how to make middleware?

php artisan make:middleware SetLocale


Are you forgetting something?
You have to register middleware in App/http/kernel.php

'web' => [
\App\Http\Middleware\SetLocale::class,
],
Now, let’s pass our request through
middleware before returning view
public function handle(Request $request, Closure $next): Response
{
$locale = $request->segment(1); // Get the first URL segment as
the locale

if (in_array($locale, ['en', 'fr', 'hi'])) {


app()->setLocale($locale); // Set the application locale
} else {
app()->setLocale('en'); // Default to English if the language
is not supported
}

return $next($request);
}
In welcome.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel Localization</title>
</head>
<body>
<h1>{{ trans('messages.welcome') }}</h1>
</body>
</html>
Basic Artisan Command to clear cache of
routes

php artisan route:clear

You might also like