Laravel 10 Language Translation Methods Tutorial

Reading Time: 6 minutes
216 Views

Language translation is an essential part of web development, especially when creating a multilingual web application. Laravel, a strong PHP framework, provides robust language translation capabilities, making it easier to provide information in your users’ preferred languages.

This tutorial will walk you through numerous language translation methods in Laravel 10, allowing you to construct dynamic and multilingual web apps.

Multilingual web applications are critical for reaching a larger audience, increasing user experience, and expanding the worldwide impact of your application.

Read More: Laravel 10 How To Create PDF Files Using TCPDF Library

Let’s get started.

Laravel Methods for Language Translation

In Laravel, there are following ways to do language translation:

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

To understand about these i.e How to use?

Let’s do a laravel setup with language keys.

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.

Settings Up Language Files

Here, we will assume two languages: French (fr) and English (en).

You need to create language keys into language respective files inside lang folder.

To create lang folder, open project terminal and run this command.

php artisan lang:publish

It will create lang folder into your application root. Next, we need to add language files into it.

Create Language Files

Create two folders say fr and en inside lang folder.

Next, create language keys file with name messages.php inside these folders.

Folders and file be like,

  • /lang/fr/messages.php
  • /lang/en/messages.php

French (fr) Language keys Settings,

<?php

return [
    "welcome" => "Bienvenue sur le tuteur Web en ligne",
    "introduction" => "L'équipe du tuteur Web en ligne vous aidera avec l'installation, la passerelle de paiement, les talons, les notifications push, les notifications jQuery, la requête AJAX, Livewire, les méthodes d'authentification et d'autres sujets."
];

Read More: How To Integrate Google Translator with Laravel 10 Tutorial

English (en) Language keys Settings,

<?php

return [
    "welcome" => "Welcome to the Online Web Tutor",
    "introduction" => "The Online Web Tutor Team will assist you with Installation, Payment Gateway, Stubs, Push Notifications, jQuery Notifications, AJAX Request, Livewire, Authentication Methods, and other topics."
];

Setup Translation Controller

Open project terminal and run this command,

php artisan make:controller TranslationController

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

Open file and write this code into it,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TranslationController extends Controller
{
    public function translation()
    {
        //
    }
}

Next, you will see how to use language translation in this controller.

Laravel Language Translation Methods

You can use Laravel facade and helpers for language translation.

Method #1: Using Lang Facade

Import Lang facade,

use Illuminate\Support\Facades\Lang;

Call language key using get() method,

$message = Lang::get('messages.welcome',[],'fr'); // French language

$message = Lang::get('messages.welcome',[],'en'); // French language

Here, calling welcome key from language files.

<?php

namespace App\Http\Controllers;

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

class TranslationController extends Controller
{
    public function translation()
    {
        $message1 = Lang::get('messages.welcome',[],'fr');
      
        $message2 = Lang::get('messages.welcome',[],'en');
     
        echo $message1;
      
        echo "<br/><br/>";
      
        echo $message2;
    }
}

Now, you need to setup a basic route to call this translation() method.

use App\Http\Controllers\TranslationController;

Route::get("/lang", [TranslationController::class, "translation"]);

Output,

Bienvenue sur le tuteur Web en ligne

Welcome to the Online Web Tutor

Next,

Read More: Laravel 10 How To Redirect with Form Inputs Tutorial

Method #2: Using __() Helper function

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TranslationController extends Controller
{
    public function translation()
    {
        $message1 = __('messages.welcome',[],'fr');
      
        $message2 = __('messages.welcome',[],'en');
     
        echo $message1;
      
        echo "<br/><br/>";
      
        echo $message2;
    }
}

Output,

Bienvenue sur le tuteur Web en ligne

Welcome to the Online Web Tutor

Method #3: Using trans() Helper function

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TranslationController extends Controller
{
    public function translation()
    {
        $message1 = trans('messages.welcome',[],'fr');
      
        $message2 = trans('messages.welcome',[],'en');
     
        echo $message1;
      
        echo "<br/><br/>";
      
        echo $message2;
    }
}

Output,

Bienvenue sur le tuteur Web en ligne

Welcome to the Online Web Tutor

That’s it.

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

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