Laravel 9 Language Translation Methods Example Tutorial

Reading Time: 5 minutes
840 Views

Inside this article we will see the concept i.e Laravel 9 Language Translation Methods Example Tutorial. Article contains the classified information about Laravel localization methods, A step-by-step guide with examples.

If you are looking for a solution i.e How to Translate text in a specific language in Laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Read More: JavaScript How To Scroll To Specific Element with Animation

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Laravel Language Translation Methods

These are the methods by the help of which you can easily translate a text into any language of your application:

  • Lang::get()
  • __()
  • trans()

Let’s understand about each method in detail.

Create Language File

We will consider a text in French language and will display using translation methods of laravel.

Language #1

Create a folder say fr inside lang folder available at application root. Inside fr folder, create a file say messages.php into it.

File path: /lang/fr/messages.php

Write this a sample translated language into it.

<?php

return [
    'welcome' => 'Bienvenue sur le tuteur Web en ligne',
];

Language #2

Create second file messages.php inside /lang/en folder.

Read More: CodeIgniter 4 How To Find Data Using Multiple Ids Tutorial

File path: /lang/en/messages.php

Write this a sample translated language into it.

<?php

return [
    'welcome' => 'Welcome To Online Web Tutor',
];

Language Translation Using Lang::get() Method

Open project into terminal and run this command.

$ php artisan make:controller TranslationController

It will create a TranslationController.php file inside /app/Http/Controllers folder.

Open TranslationController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class TranslationController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function translation()
    {
        // fr finds /lang/fr/messages.php
        $message = Lang::get('messages.welcome',[],'fr');
     
        dd($message);
    }
}

Output

Output will be in French.

Bienvenue sur le tuteur Web en ligne
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class TranslationController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function translation()
    {
        // fr finds /lang/fr/messages.php
        $message = Lang::get('messages.welcome',[],'en');
     
        dd($message);
    }
}

Output

Output will be in English.

Welcome To Online Web Tutor

Language Translation Using __() Method

Next,

Here, you will see the use of __() double underscore with parenthesis.

Read More: How To Use Codeigniter 4 in Web Development

Open TranslationController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class TranslationController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function translation()
    {
        // fr finds /lang/fr/messages.php
        $message = __('messages.welcome', [], 'fr');
     
        dd($message);
    }
}

Output

Output will be in French.

Bienvenue sur le tuteur Web en ligne

Language Translation Using trans() Method

Next,

Here, you will see the use of trans() helper function.

Open TranslationController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class TranslationController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function translation()
    {
        // fr finds /lang/fr/messages.php
        $message = trans('messages.welcome', [], 'fr');
     
        dd($message);
    }
}

Output

Output will be in French.

Bienvenue sur le tuteur Web en ligne

We hope this article helped you to learn Laravel 9 Language Translation Methods Example Tutorial in a very detailed way.

Read More: How To Work with External API in Laravel 9 Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more