Laravel Get Translation For Specific Language
Oct 13, 2022 . Admin
Hello Friends,
Now, let's see an example of laravel getting translation for a specific language. you'll learn how to set localization in laravel. I explained simply step-by-step laravel localization: a step-by-step guide with examples. We will look at examples of how to translate language in laravel. You just need to do some steps to laravel to get a translation for a specific language code example.
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 Methodfirst we will create fr/messages.php file in lang
lang/fr/messages.php<?php return [ 'welcome' => 'Bienvenue sur Mywebtuts.com !', ];
then, we will create TranslationController with Lang::get method. so you can see the below code with output:
php artisan make:controller TranslationControllerApp\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/translationOutput:
Bienvenue sur Mywebtuts.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/translationOutput:
Bienvenue sur Mywebtuts.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/translationOutput:
Bienvenue sur Mywebtuts.com !
I hope it can help you...