Localization - Laravel Localization Example

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

In this article, we will see 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.

Laravel localization may help to create a multi-language website in different languages using multiple methods of strings. And also, we will not use a package for string translation. we will use laravel trans() and @lang() function to convert string in multi-language. So, you can create a laravel 7/8/9 localization example within the lang directory.

let's see, how to create localization in laravel 7/8/9, how to create dynamic language in laravel 7/8/9, and language translation in laravel.

Step 1: Create a Localization File

In this laravel localization example, we will create a localization file language-wise. Here we will create two files First one is for English and the second one is for Chinese.

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

 

Step 2: Create Controller

Add 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 to the web.php file.

routes/web.php

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

 

Note:  We are passing {locale} argument. which is used to see 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>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 different ways to display localization strings 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 english

 

 

 http://localhost:8000/zhh/index

laravel localization cn

 


You might also like:

Recommended Post
Featured Post
How To Setup 404 Page In Angular 12
How To Setup 404 Page In Angul...

In this article, we will see how to set up a 404 page in angular 12.  To set up a 404 page in the angular...

Read More

May-11-2022

Node.js Express CRUD Example with MySQL
Node.js Express CRUD Example w...

Hello Guys, In this tutorial we will see how to perform Node js Express CRUD example with mysql. Node js Express ...

Read More

Aug-02-2021

How to Filter Datatable using Dropdown in Laravel 10
How to Filter Datatable using...

Hello developers! 👋 Ever found yourself dealing with a DataTable in Laravel and wished for a nifty way to filter th...

Read More

Feb-07-2024

Datatables Show And Hide Columns Dynamically In jQuery
Datatables Show And Hide Colum...

In this article, we will see how to hide and show columns in datatable in jquery. This example shows how you can ma...

Read More

Jun-07-2022