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.
Create a Multilingual Website Using Laravel Localization
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-
- We will create different languages folder into
resources/lang
likefr/en
. - Create middleware to inject locale information into each request.
- Set user selected locale option into the laravel session.
- Display selected language message into blade view file.
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.
Create Languages folder in Laravel Application
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'];
How to set and get Locale Into the Laravel Application
The laravel have an in-built helper method to get and set locale information in the application.
How to set Locale Language Into Laravel
I can use set locale language using the below method –
$locale ='fr'; App::setLocale($locale);
How to get Locale Language Into Laravel
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(); }
Create Middleware to set Localization in Laravel For Request
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); } }
How To Register Middleware in Laravel application
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, ],
How To Display Locale Message In Laravel
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.