Localization - Laravel Localization Example

In this example, I will show you the localization - laravel localization example. Laravel's localization features provide a convenient way to retrieve text in different languages, allowing you to easily support multiple languages within your application. So, here I will show you how to create localization in laravel 6/7/8 or laravel multi-language and laravel 8 localization examples.

Read More : Laravel 8 Localization Documentation

Let's see localization in laravel 7/8 example.

Step 1 : Create Localization File

In this laravel localization example, we will create localization files language-wise. here I create two files first one for English and the second one is Chinese.

1. resources/lang/en/message.php
2. resources/lang/zhh/message.php

 

 

Step 2 : Create Controller

Copy the below code in your terminal to create a new controller in your laravel localization example

php artisan make:controller LocalizationController

Now, add the below code in your LocalizationController.

public function index(Request $request,$locale) 
{ 
  app()->setLocale($locale);           
  return view('welcome');
}

 

Step 3 : Add Route in web.php

Now, add the below route in your routes/web.php file.

Route::get('{locale}/index','LocalizationController@index');

 

Note :  We are passing {locale} argument. which is used to seeing the output in a different language.

 

Step 4 : Create Blade File

In this step, we will create a blade file to view our output.

<div class="content" style="text-align: justify;">                
    
    <h1>Localization - Laravel Localization Example - Techsolutionstuff</h1>

    <h2>The Message Display Using trans() : {{trans('message.welcome')}}</h2>

    <h2>The Message Display Using lang() : @lang('message.welcome')</h2>

    <h2>The Message Display Using __ : {{__('message.welcome')}}</h2><br>

    <h3>The Message Display Using json and it's useful in <p style="color: red;"> 
       "toastr message" or "notification" and "required validation message" </p> 
       @json(__('message.welcome'))</h3><br>

    <h2>Example of label and placeholder localization</h2>

    <label>@lang('message.label')</label>:

    <input class="form-control" placeholder="@lang('message.placeholder')" id="title" 
      name="title" type="text" />

</div>

 

 

Here we are using a different way to display localization string in laravel.

    1)  trans()
    2)  @lang()
    3)  __()
    4) @json(__())

 

Run this example in your browser and get output like the below screenshot.
  

http://localhost:8000/en/index

 

laravel_localization_eng

 

 http://localhost:8000/zhh/index

 

laravel_localization_cn

 


You might also Like :

RECOMMENDED POSTS

FEATURE POSTS