Laravel Trans Specific Language Example

10-Apr-2023

.

Admin

Laravel Trans Specific Language Example

Hello Friends,

In this example, you will learn laravel trans-specific language example. we will help you to give an example of laravel localization: a step-by-step guide with examples. This tutorial will provide you with a simple example of laravel getting a translation for a language. you will learn laravel to get a translation for a specific language code example. So, let's follow a few steps to create an example of how to translate language in laravel.

Localization is the second phase of the Laravel internationalization process. In Laravel, an application is designed to fit various languages and cultures. Localization is adapting said internationalized applications to a specific language through translation.

You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.

Example 1: Solve By Lang::get Method


first we will create fr/messages.php file in lang

lang/fr/messages.php

<?php

return [

'welcome' => 'Bienvenue sur NiceSnippets.com !',

];

then, we will create TranslationController with Lang::get method. so you can see the below code with output:

php artisan make:controller TranslationController

App\Http\Controllers\TranslationController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Lang;

class TranslationController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function translation(Request $request)

{

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

dd($message);

}

}

now, we add route for controller

web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\TranslationController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

http://localhost:8000/translation

Output:

Bienvenue sur NiceSnippets.com !

Example 2: Solve By __() Method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Lang;

class TranslationController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function translation(Request $request)

{

$message = __('messages.welcome', [], 'fr');

dd($message);

}

}

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

http://localhost:8000/translation

Output:

Bienvenue sur NiceSnippets.com !

Example 3: Solve By trans() Method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Lang;

class TranslationController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function translation(Request $request)

{

$message = trans('messages.welcome', [], 'fr');

dd($message);

}

}

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

http://localhost:8000/translation

Output:

Bienvenue sur NiceSnippets.com !

I hope it can help you...

#Laravel