Laravel 11 - Google Translate
Laravel 11 - Google Translate
In this tutorial, we will discuss how to translate text in Laravel 11 using the Google Translate package, which simplifies dynamic language translations.
If you're a video person, feel free to skip the post and check out the video instead!
Quick Overview
This guide takes you through integrating Google Translate functionality into a Laravel application using the stichoza/google-translate-php package. You’ll start by setting up a new Laravel project, either from scratch or using an existing application, with configurations for MySQL and Pest for testing. After installing the necessary package, you’ll create a controller to handle the translation logic and update the default route to make it accessible from the browser. The tutorial walks you through the translation setup, demonstrating how to translate text from Spanish to English using the GoogleTranslate class. Additionally, you’ll explore the shorthand static method for translation, which simplifies the process by directly translating text without creating an instance of the GoogleTranslate class. Finally, you’ll test the translation functionality by running the Laravel development server and viewing the results in your browser.
Step # 1 : Set Up a New Laravel Project or Use an Existing One.
You can either start with an existing Laravel application or create a new one. If Laravel is globally installed on your system, simply run the following command to set up a new project.
laravel new translate
Alternatively, if you prefer using Composer, you can execute this command.
composer create-project laravel/laravel --prefer-dist translate
During the installation process, you’ll be prompted to configure a few options.
- Starter Kit: Choose None to set up Laravel without any built-in authentication.
- Testing Framework: Select Pest for a clean, expressive testing environment.
- Database Driver: Opt for MySQL as your database.
- Run Migrations: Confirm with yes to automatically run default migrations and create the necessary tables.
This will generate a fresh Laravel project named translate, with Pest configured for testing, MySQL set up as the database, and the initial migrations applied, leaving you with a fully prepared Laravel installation.
Step # 2 : Access the project.
Open a terminal window (such as Git Bash) and navigate to the root directory of your Laravel project. To do this, run the following command.
cd c:xampp/htdocs/translate
This command will take you to the translate project folder, allowing you to execute Artisan commands and manage your Laravel application from the terminal.
Step # 3 : Install Google Translate package.
To integrate Google Translate functionality into your Laravel project, you need to install the stichoza/google-translate-php package. This can be done by running the following command in your terminal.
composer require stichoza/google-translate-php
The command will download and install the stichoza/google-translate-php package along with its dependencies. This package enables seamless integration of Google Translate's service, allowing you to easily translate text in your Laravel application. With this package, you gain access to a wide array of features, such as automatic language detection, translation between over 100 languages, and the ability to set both source and target languages. Additionally, the package provides a shorthand static method for quick translations, making the integration process even more efficient. By using this package, you can offer multilingual support within your application, creating a more inclusive experience for users across different language preferences.
Step # 4 : Create a Controller.
To handle translation functionality in your Laravel project, you need to create a controller. You can do this by running the following Artisan command.
php artisan make:controller TranslateController
After generating the controller, open TranslateController.php located in the app/Http/Controllers directory and update it with the following code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stichoza\GoogleTranslate\GoogleTranslate;
class TranslateController extends Controller
{
public function index()
{
// Text to be translated.
$text = 'Traduce este texto al idioma inglés';
// Initialize GoogleTranslate instance.
$tr = new GoogleTranslate();
// Set the target language to English ('en').
$tr->setTarget('en');
// Perform the translation.
$translation = $tr->translate($text);
// Output the translation result for debugging.
dd($translation);
}
}
The GoogleTranslate class is used to interact with Google's translation service, while the setTarget('en') method sets the target language to English ('en'). The translate($text) method performs the translation and stores the result in the $translation variable, and the dd($translation) method is used for debugging by dumping the translation result to the browser. This completes the initial setup of the translation logic in the TranslateController.
Step # 5 : Update the Route.
To ensure that the translation functionality is accessible from the browser, update the default route in your routes/web.php file with the following.
Route::get('/', [App\Http\Controllers\TranslateController::class, 'index']);
This line configures the route so that when someone visits the root URL (/), Laravel will call the index method from the TranslateController. As a result, the translation logic you set up will run, and the translation result will be displayed in the browser.
Step # 6 : It's time to test.
Now it's time to test the translation functionality. To do this, start the Laravel development server by running the following command in your terminal.
php artisan serve
After the server is running, open your browser and navigate to the following URL: http://127.0.0.1:8000.
You should see the translation result displayed in your browser, confirming that everything is working as expected.
Shorthand Static Method for Translation :
You can simplify the translation process by using the shorthand static method provided by the GoogleTranslate package. This method allows you to directly translate text from one language to another without needing to create an instance of the GoogleTranslate class. To implement this, update the index method in your TranslateController like this.
public function index()
{
// Translate the given text from Georgian ('ka') to English ('en').
dd(GoogleTranslate::trans('მოდით ვცადოთ თარგმნა ინგლისურად', 'en', 'ka'));
}
In this example, the text მოდით ვცადოთ თარგმნა ინგლისურად (which is Georgian) will be translated to English. The static method GoogleTranslate::trans() takes the text, the target language ('en' for English), and the source language ('ka' for Georgian) as its arguments. When you test this, you should see the translated result in the browser.
Conclusion
By following this guide, you’ve successfully integrated Google Translate functionality into your Laravel application using the stichoza/google-translate-php package. Your application can now translate text from one language to another directly within the controller, with the option to use the shorthand static method for even simpler implementation. You've also learned how to set up a translation route, test the translation functionality in the browser, and output the translated result. This integration adds multilingual support to your application, providing a more inclusive and user-friendly experience for your global audience.
For more details, please refer to the stichoza/google-translate-php package documentation.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments