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.
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
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');
}
Now, add the below route to the web.php file.
routes/web.php
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>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.
Run this example in your browser and get output like the below screenshot.
You might also like:
In this article, we will see how to increase session timeout in laravel. In this example, we can see how to se...
Jun-28-2020
In this tutorial, I will explain the laravel 9 image upload example. image or file upload is the most common task in web...
Feb-28-2022
In this tutorial we will learn about laravel 8 inner join query example. Also see how to join two tables in laravel 8. I...
Nov-24-2021
Welcome to this step-by-step guide on installing Moment.js in your Angular 15 project. As an Angular developer, I unders...
Jun-26-2023