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.
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
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');
}
Now, add the below route in your routes/web.php file.
Route::get('{locale}/index','LocalizationController@index');
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.
Run this example in your browser and get output like the below screenshot.
You might also Like :