Watch Youtube Video (HINDI) :
How to Use Google Translate Multi-Language In Laravel, In this article using stichoza/google-translate-php laravel composer package we will translate our laravel application based on the user's chosen language.
Google translate in different languages laravel, we can translate all google provided translate language in laravel, For that, we just need to pass the language code, and based on it the google translate package will translate accordingly language code in your laravel application.
To translate using google Translate in different multi-language, we will also create a middleware for the application of all web routes. also, you can specify in specific route to translate. So in this post, we will create a controller and two routes. next, we will make a blade file and will use select dropdown for the get language code then after a base on the language code google will translate it.
Preview :
So let's start integrating google translate multi-language in the laravel application.
Step 1. Install Composer
Install composer using the below-suggested command in your project terminal. The Command will install the google Translate dependency library in your application.
composer require stichoza/google-translate-php
Add global namespace in the app.php file. The file you will find in the config folder from the root. Then you will find "aliases" array attribute. just add in this "aliases" array at the end.
config/app.php'aliases' => [ ...... 'GoogleTranslate' => Stichoza\GoogleTranslate\GoogleTranslate::class, ]
Step 2. Routes
Create a controller. I am creating a GoogleTranslateController controller using the below-provided command.
php artisan make:controller GoogleTranslateController
HTTP Client Guzzle Package With Basic Usage In Laravel
Goto your project web.php file. In the file, you will find the routes folder from the root. First import use App\Http\Controllers\GoogleTranslateController. Create two routes. The first route for the view blade file. The second route for the added selected language in the locale.
routes/web.php<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\GoogleTranslateController; /* |-------------------------------------------------------------------------- | 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('google/translate',[GoogleTranslateController::class,'googleTranslate'])->name('google.translate'); Route::get('google/translate/change',[GoogleTranslateController::class,'googleTranslateChange'])->name('google.translate.change');
Step 3. Controller
In the controller firstly we will view the translate blade file. The second method for the set session on the change language. We will also set it as app language and add in session for the check-in middleware also, then we will redirect back once select choose language.
app/Http/Controllers/GoogleTranslateController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App; use Session; class GoogleTranslateController extends Controller { public function googleTranslate() { return view('translate'); } public function googleTranslateChange(Request $request) { App::setLocale($request->lang); Session::put('locale',$request->lang); return redirect()->back(); } }
Step 4. Blade File
This is a blade file for the change or choosing another language, In this file, we made a language choice dropdown. As the user chooses language it will set to the local app language in the application.
To change the dropdown language we will use little jquery, As you change your language the selected language code will send to the controller method and set to the App::setLocale(), which also will add to the session. So we can check in middleware too.
resources/views/translate.blade.php<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <title>Laravel Google Translate - Webappfix!</title> </head> <body> <div class="container mt-5"> <div class="card"> <div class="card-header"> <h1>Laravel Google Translate Example - Webappfix</h1> </div> <div class="card-body"> <div class="row"> <div class="col-md-2"> <strong>Select Language</strong> </div> <div class="col-md-4"> <select class="form-select changeLanguage"> <option value="en" {{ session()->get('locale') == 'en' ? 'selected' : ''}}>English</option> <option value="fr" {{ session()->get('locale') == 'fr' ? 'selected' : ''}}>France</option> <option value="es" {{ session()->get('locale') == 'es' ? 'selected' : ''}}>Spanish</option> </select> </div> </div> <h3>{{ GoogleTranslate::trans('Welcome To Webappfix',\App::getLocale()) }}</h3> <h3>{{ GoogleTranslate::trans('Hello World!',\App::getLocale()) }}</h3> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script> $('.changeLanguage').change(function(event){ var url = "{{ route('google.translate.change') }}"; window.location.href = url+"?lang="+$(this).val() }) </script> </body> </html>
Step 5. Middleware
Create middleware using the below-provided command in your application.
php artisan make:middleware GoogleTranslate
Here in this middleware, we will do little coding for the check session's current language chosen to check.
app/Http/Middleware/GoogleTranslate.php<?php namespace App\Http\Middleware; use Closure; use App; use Session; class GoogleTranslate { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (Session::has('locale')) { App::setLocale(Session::get('locale')); } return $next($request); } }
Register the created middleware in the kernel.php file. In the web routes. add the middleware as per the below example given.
app/Http/kernel.phpprotected $middlewareGroups = [ 'web' => [ ....... \App\Http\Middleware\GoogleTranslate::class, // REGISTER MIDDLEWARE HERE ], ];
Step 6. Start Development Server
Start your application development server with the suggested command in your terminal.
php artisan serve
Open your browser, and run the below-given URL in your browser.
http://127.0.0.1:8000/google/translate
Microsoft Authentication Implement on Laravel App
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How to Get Browser Name and Version in Laravel
- Laravel 10 Breeze Integration Tutorial Example
- Laravel 6 - Preview and Crop Image Before Upload using Ajax
- Laravel 9 CRUD Operation From Scratch Step By Step Guideline
- How To Filter In Laravel Query Example
- Arr::crossJoin() | Laravel Helper Function
- Laravel 10 CRUD Operation With YouTube Video Tutorial
- How To Exchange Currency Rates In Laravel Example
- Laravel 10 PDF Export Example With DomPDF
- Error : Array to string conversion Laravel