Laravel 8 Tutorial - Localization
In this tutorial, we are going to learn about localization in Laravel 8.
Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.
Language strings are stored in files within the lang
directory inside the resources
folder. Within this directory, there should be a subdirectory for each language supported by the application.
So, let's create a language file. Go to the resources
folder and open the lang
folder. You can see that inside the lang
folder, there is an en
folder, which is for English.
Here, inside the en
folder, create a new file. Let's say the file name is message.php
.
Now, inside the message.php
file, simply return an array. So, just type here:
<?php
return [
"welcome"=>"Welcome to the laravel",
"language"=>"English"
]
Now, go back to the lang
folder and create another folder. Let's say the folder name is hi
, which is for Hindi.
Create a file named message.php
inside the hi
directory. Inside the message.php
file, just return an array, so write the following code:
<?php
return [
"welcome"=>" laravel में आपका स्वागत है।",
"language"=>"हिन्दी"
]
Now, save the file and go to the welcome.blade.php
file. Here, just use the language. So, just type here:
{{__('message.welcome')}}
{{__('message.language')}}
Now, let's modify the welcome route. So, go to the web.php
file:
Route::get(/{locale}', function($locale){
App::setLocale($locale);
return view('welcome');
});
Now, save all the files and let's check. Switch to the browser and go to the URL /en
. You can see the welcome message and language in English.
Now, change the URL to /hi
. Now, you can see the message and language are showing in Hindi.
So, in this way, you can use localization in Laravel 8.