Localization - Laravel Localization Example

WebSolutionStuff | Nov-06-2020 | Categories : Laravel PHP

In this example i will show you localization - laravel localization example.

Laravel's localization features provide a convenient way to retrieve text in diffrent languages, allowing you to easily support multiple languages within your application. So here i will show you how to create localization or laravel dynamic language.

Step 1 : Create Localization File

In this Laravel Localization Example we will Create localization file in language wise. here i create two file First one for English and second one is Chinese.

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

 

Step 2 : Create Controller

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

php artisan make:controller LocalizationController

Now, add 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 below route in your routes/web.php file.

Route::get('{locale}/index','[email protected]');

 

Note :  We are passing {locale} argument. which is use to see output in different language.

 

 

Step 4 : Create Blade File

In this step we will creat blade file for view our output.

 

<div class="content" style="text-align: justify;">                
                <h1>Laravel 8 Localization - websolutionstuff.com</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 diffrent way to display locaization string in laravel.

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

 

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

http://localhost:8000/en/index

 

laravel localization english

 http://localhost:8000/zhh/index

 

laravel localization cn

Featured Post



Follow us
facebooklogo github instagram twitter