How To Translate English To Marathi In Php

As a web developer, I have often come across the need to translate text from one language to another in my projects. Recently, I had a requirement to translate English text to Marathi using PHP. In this article, I will share with you the steps I followed and the code snippets I used to achieve this task.

Understanding the Translation Process

Before we dive into the code, let’s take a moment to understand the translation process. In order to translate text from English to Marathi, we need to leverage an external translation service or library. There are several options available, but for this article, I will be using the Google Translate API.

Setting Up the Google Translate API

In order to use the Google Translate API, you will need to have a valid API key. If you don’t have one already, you can follow these steps:

  1. Go to the Google Cloud Console.
  2. Create a new project or select an existing one.
  3. Enable the Google Translate API for your project.
  4. Generate an API key for your project.

Once you have obtained your API key, you are ready to start coding!

Installing the Required Dependencies

In order to interact with the Google Translate API, we will be using the Guzzle HTTP client library for PHP. To install Guzzle, you can use Composer by running the following command in your project directory:

composer require guzzlehttp/guzzle

Writing the Translation Code

Now that we have our API key and the required dependencies installed, we can start writing the code to translate English text to Marathi. Here’s a simple function that encapsulates the translation logic:

function translateToMarathi($text) {
$apiKey = 'your_api_key_here';

$client = new \GuzzleHttp\Client();
$response = $client->get('', [
'query' => [
'key' => $apiKey,
'source' => 'en',
'target' => 'mr',
'q' => $text,

$data = json_decode($response->getBody(), true);

if (isset($data['data']['translations'][0]['translatedText'])) {
return $data['data']['translations'][0]['translatedText'];
} else {
return false;

In the above code, we make a GET request to the Google Translate API endpoint with the necessary parameters, including our API key, the source language (English), the target language (Marathi), and the text to be translated. We then decode the response JSON and return the translated text.

Using the Translation Function

Now that we have our translation function ready, let’s see how we can use it in our PHP project:

$text = "Hello, world!";
$translatedText = translateToMarathi($text);

echo "English Text: " . $text . "
echo "Marathi Translation: " . $translatedText;

In the above code, we specify the English text we want to translate, pass it to our translation function, and then display the original English text and the translated Marathi text on the screen. You can replace the sample text with your own text to see the translation in action.


Translating text from one language to another is an interesting and useful feature to have in your PHP projects. In this article, we explored how to translate English text to Marathi using the Google Translate API in PHP. We learned about the translation process, set up the API, installed the required dependencies, and wrote the necessary code. Now, you can easily integrate translation capabilities into your own PHP applications.