How to Add Google Translate in Laravel

Published: Feb 01, 2023

Hello Artisans, today I'll show you how to add google translate to your Laravel application. Google translate gives us a wide variety of languages for translations. We'll use stichoza/google-translate-php package to accomplish our result So, let's see how we can easily integrate google translate into our Laravel application.

Note: Tested on Laravel 9.19

Table of Contents

  1. Install and configure stichoza/google-translate-php package
  2. Create and Configure Middleware
  3. Create and Setup Controller
  4. Create Route
  5. Setup View
  6. Output

Install and configure stichoza/google-translate-php package

At first, we've to install stichoza/google-translate-php package. So, fire the below command to install

composer require stichoza/google-translate-php

Now we've to register its aliases in app.php. Put the below code inside aliases array like below.

'aliases' => Facade::defaultAliases()->merge([
        'GoogleTranslate' => Stichoza\GoogleTranslate\GoogleTranslate::class

Create and Configure Middleware

Now we'll create a middleware where we'll set our selected language as the app's default locale. Now fire the below command in the terminal.

php artisan make:middleware ChangeLocale

It'll create a file under app\Http\Middleware called ChangeLocale.php. Now open the file and replace it with the below codes.


namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class ChangeLocale
    public function handle(Request $request, Closure $next)
        if (session()->has('locale')) {

        return $next($request);

Now we need to register in $middlewareGroups array. See the below source code,

protected $middlewareGroups = [
        'web' => [

        'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,

Create and Setup Controller

Now we'll create a controller where we'll set our selected language in session. Now fire the below command in the terminal to create a controller.

php artisan make:controller GoogleTranslateController

It'll create a file under app\Http\Controllers called GoogleTranslateController.php. Now open the file and replace it with the below codes.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class GoogleTranslateController extends Controller
    public function change(Request $request): \Illuminate\Http\RedirectResponse
        session()->put('locale', $request->lang);

        return back();

Create Route

Now, we need to create a route so that when a user selects a language, it'll be redirected to our controller. So put this below route in web.php

Route::get('lang/change', [GoogleTranslateController::class, 'change'])->name('change.lang');

Setup View

Now we'll update our default blade file called welcome.blade.php which is come by default. Now open the file and replace it with the below codes.


<!DOCTYPE html>
    <title>Laravel 9 Google Translate Package Example -</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src=""></script>
<div class="container">
    <div class="card">
        <div class="card-header">
            <h1>Laravel Google Translate Example -</h1>
        <div class="card-body">
            <div class="row">
                <div class="col-md-2">
                    <strong>Select Language: </strong>
                <div class="col-md-4">
                    <select class="form-select change_lang">
                        <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>
            <h3>{{ GoogleTranslate::trans('Welcome to', app()->getLocale()) }}</h3>
            <h3>{{ GoogleTranslate::trans('Hello World', app()->getLocale()) }}</h3>

    var url = "{{ route('change.lang') }}";
    $(document).ready(function (){
        $(document).on('change','.change_lang',function (){
            window.location.href = url   "?lang="  $(this).val();


And finally, we're ready with our setup. It's time to check our output. Now go to, If everything goes well (hope so) we can see the below output.

That's it for today. I hope you've enjoyed this tutorial. You can also download this tutorial from GitHub. Thanks for reading. 🙂


No comments yet…