Sometimes it may happen your website is runnign worldwide globally and users coming from different language and different region. So they do not understand the same language of the website that's why we need make site localization so users can see and understand in their language with better understanding.
Because of that laravel provides a localization facility, using this localization you can view your front in a different language.
The laravel localization features provide a convenient way to retrieve a string in different various languages and it's fully customized. That will allow you to fully support multiple language within your laravel application.
Laravel provides two ways to manage translation strings. First language string may store in files within lang directory. and second, translation string may be defined in JSON file when the taking of this approach, each language supported by your application would have corresponding JSON file within this directory.
Simple Localization
This is our English language file where I define an index with the name of the intro. So when i called this intro index in my blade file it will render Welcome from the en/text.php file when the language code selected will "en".
resources/lang/en/text.php
<?php return [ 'intro' => 'Welcome' ];
This is our Gujarati language file where I define an index with the name of the intro. So when i called this intro index in my blade file it will render સ્વાગત છે from the guj/text.php file when the language code selected will "guj".
resources/lang/guj/text.php
<?php return [ 'intro' => 'સ્વાગત છે' ];
This is our Hindi language file where I define an index with the name of the intro. So when i called this intro index in my blade file it will render स्वागत हे from the hi/text.php file when the language code selected will "hi".
resources/lang/hi/text.php
<?php return [ 'intro' => 'स्वागत हे' ];
Blade View File
This is our blade file here we will print that localization output.
You can see in this blade file example I called in the "h3" tag __('text.intro') Here text means our file name inside the resources/lang/ of different language folders.
If you will select the "English" language it will render from en/text.php. As it is if you select "Hindi" it will random from the hi/text.php.
Also, you will see in the next how to select different dynamic languages to form the app.
resources/views/home.blade.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Webappfix</title> </head> <body> <h3>{{ __('text.intro') }}</h3> </body> </html>
Routes
Here you can select a language from the language code. But this is the static language selected.
Using App::setLocale('en') you can select a different language from that code.
Here language code and folder name were in save our text.php file in the lang folder inside the resources.
There were many different ways to define which language user come from using IP Address, or by asking and by selecting the language from the user.
routes/web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('home',function(){ App::setLocale('en'); // IF YOUR LANGUAGE IS ENGLISH // App::setLocale('hi'); // IF YOUR LANGUAGE IS HINDI // App::setLocale('guj'); // IF YOUR LANGUAGE IS GUJARATI return view('home'); });
Dynamic Localization With Routes
This is one example of how you can dynamic user language select from the URL. Just simple pass a param. and then set.
You can also here use middleware for the user language set accordingly user country or region.
routes/web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('{lang}/home',function($lang){ App::setLocale($lang); return view('home'); });
http://post.dv/guj/home http://post.dv/en/home http://post.dv/hi/home
Instead of this, you can still change another way to language from config.php
Here laravel already default giving us "en" Means English lang.
config/app.php
<?php /* |-------------------------------------------------------------------------- | Application Locale Configuration |-------------------------------------------------------------------------- | | The application locale determines the default locale that will be used | by the translation service provider. You are free to set this value | to any of the locales which will be supported by the application. | */ 'locale' => 'en', // FOR ENGLISH LANGUAGE // 'locale' => 'hi', // FOR HINDI LANGUAGE // 'locale' => 'guj', // FOR GUJARATI LANGUAGE
Advance Localization
If you wants to learn more about laravel localization you can continue reading this post.
This is advance of laravel localization where you can use the plural form of the given language.
The plural form of the work you can define in same indexing in the language file there not need to extra define or extra long process.
You can just define after pipeline notation in the same string. You can see in this example how I define the plural form of each language.
This is the same file of English language as above given but the difference is define the plural form of the same index "intro" using pipe notation.
resources/lang/en/text.php
<?php return [ 'intro' => 'Welcome | Welcomes' // plural form using pipe notation ];
This is the Gujarati language file where I define the plural form of the same intro. as before given example.
resources/lang/guj/text.php
<?php return [ 'intro' => 'સ્વાગત છે | સ્વાગત કરે છે' // plural form using pipe notation ];
And this is Hindi language file also same define plural form as before define both that language.
resources/lang/hi/text.php
<?php return [ 'intro' => 'स्वागत हे | का स्वागत करते हैं' // plural form using pipe notation ];
Blade View File
This is my blade view file. Here little difference is callign a function for the render different languages.
Using trans_choice() method will give us that both plural form wich we define in our language file.
This method will accept two params one for path and the second for (plural,2) and (singular,1).
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Webappfix</title> </head> <body> <h3>{{ trans_choice('text.intro',1) }}</h3> // OUTPUT FOR ENGLISH : Welcome <h3>{{ trans_choice('text.intro',2) }}</h3> // OUTPUT FOR ENGLISH : Welcomes </body> </html>
You can see in this above blade file how I have defined different forms of different languages in the blade file.
Routes
As it is before same routes and same method to set local.
routes/web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('home',function(){ App::setLocale('en'); // IF YOUR LANGUAGE IS ENGLISH // App::setLocale('hi'); // IF YOUR LANGUAGE IS HINDI // App::setLocale('guj'); // IF YOUR LANGUAGE IS GUJARATI return view('home'); });
Dynamic Localization With Routes
Here we can also make it dynamic from the URL. You can see this in the below-given route example.
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('{lang}/home',function($lang){ App::setLocale($lang); return view('home'); });
This is my different URL path from calling different languages, This URL is just for example purposes so you can better understand.
http://post.dv/guj/home http://post.dv/en/home http://post.dv/hi/home
Dynamic Wildcard Localization
This is wildcard dynamic localization where you can print dynamic user data as you store it in a database same print it in different language.
For example, the name of the user always be English but I want to hospitality them in their language. So you can pass the argument index key with data and you can print.
This is my English language file where I define the intro index with the value "Welcome! {username}" where username I will pass from blade file and it will show.
resources/lang/en/text.php
<?php return[ 'intro' => 'Welcome! :user' ];
This is my Gujarati language file. If any Gujarati user comes to my site so I will wise him/her like સ્વાગત છે! {username}. and the username I will pass from the lade file.This is my Gujarati language file. If any Gujarati user comes to my site so I will wise him/her like સ્વાગત છે! {username}. and the username I will pass from the lade file.
resources/lang/guj/text.php
<?php return [ 'intro' => 'સ્વાગત છે! :user' ];
The same process i followed in the Hindi language file.
resources/lang/hi/text.php
<?php return [ 'intro' => 'स्वागत हे! :user' ];
Blade View File
You can see in the blade file I canned that index from the language file and then passed the "user" argument from this blade file.
Here all these three language examples are given in this below blade file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Webappfix</title> </head> <body> <h3>{{ __('text.intro',['user' => 'Webappfix']) }}</h3> <h3>{{ __('text.intro',['user' => 'वेबएपफिक्स']) }}</h3> <h3>{{ __('text.intro',['user' => 'વેબએપફિક્સ']) }}</h3> </body> </html>
Routes
This is my route file where I will define my chosen language.
routes/web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('home',function(){ App::setLocale('en'); // IF YOUR LANGUAGE IS ENGLISH // App::setLocale('hi'); // IF YOUR LANGUAGE IS HINDI // App::setLocale('guj'); // IF YOUR LANGUAGE IS GUJARATI return view('home'); });
Dynamic Localization With Routes
This is my route file where received language code from the URL route param.
routes/web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('{lang}/home',function($lang){ App::setLocale($lang); return view('home'); });
This is a different URL language path, This each URL will define each language.
http://post.dv/guj/home http://post.dv/en/home http://post.dv/hi/home
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How To Calculate Distance Between Two Latitude And Longitude In Laravel
- [issue] Possible All Way To Change Asset Path And Add Public To Asset Path In Laravel
- How to use Date Format Validation in Laravel?
- Laravel 9 Sanctum API Authentication Tutorial
- Laravel Livewire CRUD Using Bootstrap Modal Full Example Guideline And Demo Source Code
- Laravel 10 Create Controller Example
- How to Generate PDF in Laravel 6 Example
- Route Resource Controller Laravel
- db raw multiple when case sum laravel
- Parent OrderBy From Many Child's Laravel Example