Laravel 10 - Google translate text
Laravel 10 - Google translate text
In this tutorial, we will discuss how to translate text using Google Translate, useful for multilingual applications and reaching a global audience with localized content.
If you're a video person, feel free to skip the post and check out the video instead!
Quick Overview
In this guide, we walk you through adding basic translation functionality to a Laravel application using the stichoza/google-translate-php package. You’ll begin by creating or using an existing Laravel project, then installing the required Node.js and Composer dependencies. Next, you’ll install the Google Translate PHP package and create a TranslateController to handle the translation logic. You'll define a simple route that points to the controller method, which uses the package to translate a sample English sentence into Spanish. Finally, you'll start the Laravel development server and test the functionality in your browser. By the end of this guide, you’ll have a working example of real-time translation in Laravel, making it easier to localize content or experiment with multilingual features. This approach is ideal for quick prototypes, language toggles, or any project that needs basic on-the-fly translation without a complex setup.
Step # 1 : Set Up Your Laravel 10 Project.
To get started, you can either create a fresh Laravel 10 project or use an existing one. If Laravel is installed globally on your system, use.
laravel new google-translate
If not, you can create the project using Composer instead.
composer create-project laravel/laravel --prefer-dist google-translate
Whether you're starting fresh or building on an existing codebase, having a working Laravel 10 project is essential. This ensures you're ready to install the Google Translate package, create the controller, and test translation features directly within your application. Getting this setup right from the start helps avoid configuration issues later in the process.
Step # 2 : Accessing Your Laravel Project.
Open a terminal (like Git Bash) and navigate to your Laravel project directory. For example.
cd c:xampp/htdocs/google-translate
Once inside the project, install the front-end dependencies and start the Vite development server by running.
npm install && npm run dev
Keep the Vite server running in that window. Then, open a new terminal tab or window, navigate to the same project folder, and use it to continue running Laravel commands. This setup ensures that your assets are being compiled properly while you work on the backend functionality.
Step # 3 : Install the Google Translate Package.
To bring translation features into your Laravel project, install the stichoza/google-translate-php package by running the following command in your project’s root directory.
composer require stichoza/google-translate-php
This package is a lightweight, unofficial wrapper around Google Translate. Unlike the official Google Translate API, which requires an API key and billing setup, this package lets you perform translations without any authentication or configuration, making it perfect for quick prototypes, testing, or simple use cases. It supports a wide range of languages and allows you to define source and target languages manually or let the system auto-detect the input language. With just a few lines of code, you can translate any string from your Laravel application without needing to build a full translation system or rely on static translation files..
Step # 4 : Create the Translate Controller.
Generate a new controller by running the following Artisan command.
php artisan make:controller TranslateController
Once created, open the TranslateController.php file 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 = 'Hello, This is just a basic example of translation.';
$tr = new GoogleTranslate();
$tr->setTarget('es'); // Translate to spanish in this case.
$translation = $tr->translate($text);
dd($translation);
}
}
This controller is responsible for translating a hardcoded English sentence into Spanish using the stichoza/google-translate-php package. Inside the index method, an instance of the GoogleTranslate class is created. The target language is set to 'es' (Spanish), and the translate() method is called with the sample text. The result is immediately dumped to the browser using Laravel’s dd() (dump and die) function, which is useful for quickly testing and inspecting the output. This simple setup confirms that the translation functionality is working correctly before you move on to more dynamic use cases.
Step # 5 : Define a Route for Translation.
To trigger the translation functionality through a browser, you need to define a route that maps to your controller method. Start by importing the controller at the top of your routes/web.php file.
use App\Http\Controllers\TranslateController;
Then, define a route that points to the index method of the controller.
Route::get('/', [TranslateController::class, 'index']);
This route links the homepage (/) of your Laravel app to the index method in the TranslateController. When you visit http://127.0.0.1:8000 in your browser, it triggers the translation logic and displays the output. It’s a simple way to test and confirm that the controller and translation setup are working as expected.
Step # 6 : Test the Translation Functionality.
With everything in place, it’s time to test if the translation is working. Start the Laravel development server by running.
php artisan serve
Once the server is running, open your browser and visit: http://127.0.0.1:8000. You should see the translated output displayed in your browser, confirming that the TranslateController is working as expected and the package is functioning properly.
If you prefer a cleaner syntax, you can simplify the translation logic using the static trans method. Just update the index method in your controller like this.
public function index()
{
// Translate to Georgian
dd(GoogleTranslate::trans('Hello, This is just a basic example of translation.', 'ka', 'en'));
}
This one-liner performs the same translation but avoids creating an object manually. It translates the text from English (en) to Georgian (ka) using the static method, making your code more concise while achieving the same result. It’s a great option for quick, straightforward translations.
Conclusion
By following this guide, you’ve successfully added basic translation functionality to your Laravel 10 application using the stichoza/google-translate-php package. You created a controller, defined a route, and tested real-time translation of text directly in your browser. Along the way, you learned how to use both the class-based and static approaches for translating content into different languages. This setup is a simple yet effective way to introduce multilingual support or experiment with localization features in your app.
For more flexibility and advanced usage, you can explore the package documentation to handle dynamic input, support multiple languages, or even build a full translation interface. Whether you're building a blog, a dashboard, or a global-facing platform, this gives you a solid starting point for adding language support. It’s lightweight, fast to set up, and easy to extend as your project grows.
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