This laravel 9 tutorial help to create a multilingual web application. We will create different language messages and display them to users based on selected locale information.
We will create an application that helps to serve fr(France) and en(English) languages message. The default laravel language is english(en), Although not everyone speaks or understands English and many countries have more than one official language.
There are many third-party packages available to create a multilingual website using laravel, But laravel provides inbuilt localization support to create a multilingual website.
The laravel provides localization features for multilingual applications. The language string message is stored into in the files resources/lang
directory. Within that directory, there should be a subdirectory for each language supported by the application.
We will gone through the following features in this tutorial-
resources/lang
like fr/en
.We will create a sample laravel application using the below command –
laravel new laravel_localization_example
Now we will cd laravel_localization_example
folder.By default, The Laravel application’s local language is English or en type.You can find it on the config/app.php
file.
'locale' => 'en',
You can change this default as per your locale or personalize your language.
There is en
type subdirectory already available in resources/lang
folder. The resources/lang/en folder has some files like auth.php
, pagination.php
, passwords.php
, validation.php
.
All the files return key:value
pair array data.
we will add French(fr) language support for laravel application, Now create a fr folder into resources/lang
folder, Also created a constant.php
file into en/
and fr/
folder, please make sure the key name will be same into across the different languages folder file.We just change the value of the constant.
//resources/lang/en/constant.php return ['welcome' :'Welcome Laravel Multilingual Application'];
Create the same constant into France language –
//resources/lang/fr/constant.php return ['welcome' :'Bienvenue application multilingue Laravel'];
The laravel have an in-built helper method to get and set locale information in the application.
I can use set locale language using the below method –
$locale ='fr'; App::setLocale($locale);
I can use get locale language using the below method –
App::getLocale($locale);
We can put Local information into session using the below controller method –
public function setLang($locale) { App::setLocale($locale); session()->put('locale', $locale); return redirect()->back(); }
We can use middleware to set localization for laravel application.Created a new middleware using the below command –php artisan make:middleware Localization
The above command will create a Localization.php
file into app/Http/Middleware
folder.
Create a new method inside app/Http/Middleware/Localization.php
file to set Locale lang of application, First we will check locale is set if not then set into session otherwise skip.
has('locale')) { App::setLocale(session()->get('locale')); } return $next($request); } }
After creation of middleware into laravel application, I need to register middleware with kernel as like below, I will make one entry of Localization into app/Http/Kernel.php
file.
/** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, ..... .... \App\Http\Middleware\Localization::class, ],
We will create a new /resources/views/hello.blade.php
file to test messages in different languages,
<div class="title m-b-md"> {{ trans('constant.welcome')}} </div>
Here, I have used the trans()
helper string function which will take one argument combination of filename and keyname, the params as like below – filename.array_key_name
I hope, You enjoyed Multilingual functionality integration using Laravel Localization.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More