How to Use Google Translator in Laravel | websolutioncode.com
How to Use Google Translator in Laravel | websolutioncode.com

How to Use Google Translator in Laravel

Laravel, a widely-used PHP framework, simplifies the incorporation of diverse features into your web applications. Among these capabilities is language translation, which can significantly improve user experience. This tutorial will outline how to leverage Google Translator within Laravel to implement multilingual support for your application.

Step 1: Install Laravel

If you haven’t already set up a Laravel project, you can do so by following these steps:

  1. Open your terminal and navigate to the desired directory.
  2. Run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel your-project-name

Change into the project directory:

cd your-project-name

Step 2: Set Up Google Translate API

To use Google Translator in Laravel, you’ll need to enable the Google Translate API and obtain an API key. Follow these steps:

  1. Visit the Google Cloud Console.
  2. Create a new project or select an existing one.
  3. In the left sidebar, click on “APIs & Services” and then “Library.”
  4. Search for “Google Translate API” and enable it for your project.
  5. After enabling the API, go to “Credentials” and create a new API key.

Step 3: Install the Google Translate Package

In Laravel, you can use a convenient package called stichoza/google-translate-php. Install it using Composer:

composer require stichoza/google-translate-php

Step 4: Configure API Key

Open your Laravel project in a code editor and navigate to the .env file. Add your Google Translate API key like this:

GOOGLE_TRANSLATE_API_KEY=your-api-key

Step 5: Create Translation Service

Create a new service to handle translation. In the terminal, run:

php artisan make:service TranslateService

Open the generated TranslateService.php file in the app/Services directory and update it like this:

<?php

namespace App\Services;

use Stichoza\GoogleTranslate\GoogleTranslate;

class TranslateService
{
    protected $translator;

    public function __construct()
    {
        $this->translator = new GoogleTranslate();
        $this->translator->setApiKey(config('services.google_translate.api_key'));
    }

    public function translate($text, $targetLanguage)
    {
        $this->translator->setTarget($targetLanguage);

        return $this->translator->translate($text);
    }
}

Step 6: Use the Translation Service

Now, let’s use the translation service in a controller. Open a controller file (e.g., app/Http/Controllers/HomeController.php) and update it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\TranslateService;

class HomeController extends Controller
{
    protected $translateService;

    public function __construct(TranslateService $translateService)
    {
        $this->translateService = $translateService;
    }

    public function index()
    {
        $textToTranslate = "Hello, how are you?";
        $targetLanguage = "es"; // Change to your desired language code

        $translatedText = $this->translateService->translate($textToTranslate, $targetLanguage);

        return view('welcome', compact('translatedText'));
    }
}

Step 7: Display Translated Text

Open the welcome.blade.php file in the resources/views directory and update it to display the translated text:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Google Translator</title>
</head>
<body>
    <h1>Translated Text:</h1>
    <p>{{ $translatedText }}</p>
</body>
</html>

Step 8: Test Your Application

Run your Laravel development server:

php artisan serve

Visit http://localhost:8000 in your browser, and you should see the translated text based on the language you specified.

Congratulations!

You’ve successfully integrated Google Translator into your Laravel application. Feel free to explore more features and customize the implementation to suit your project’s needs.

Leave a Reply