DEV Community

Utsav Mavani
Utsav Mavani

Posted on

16

Google Translate API in a React.js

Google Translate API in a React.js application to translate text between languages, you can follow these steps:

Set Up Google Cloud Project:

  • Go to the Google Cloud Console (https://console.cloud.google.com/).
  • Create a new project or use an existing one.
  • Enable the "Cloud Translation API" for your project.

Obtain API Key:

  • In the Cloud Console, navigate to "APIs & Services" > "Credentials."
  • Create a new API Key.
  • Keep this API Key handy as you'll need it to make API requests.

Install Dependencies:

  • Open your React.js project's terminal and install the necessary packages:
npm install axios
Enter fullscreen mode Exit fullscreen mode

Create API Request:

  • Create a new file, e.g., GoogleTranslate.js, to handle API requests.
  • Inside this file, you can use Axios to send requests to the Google Translate API:
import axios from 'axios';

const API_KEY = 'YOUR_GOOGLE_TRANSLATE_API_KEY';
const API_URL = 'https://translation.googleapis.com/language/translate/v2';

const translateText = async (text, targetLanguage) => {
  const response = await axios.post(
    `${API_URL}?key=${API_KEY}`,
    {
      q: text,
      target: targetLanguage,
    }
  );

  return response.data.data.translations[0].translatedText;
};

export default translateText;
Enter fullscreen mode Exit fullscreen mode

Integrate into React Component:

  • Now you can use the translateText function in your React components.
  • Let's assume you have an input field for the text and a dropdown to select the target language.
  • When the user enters text and selects a language, you can trigger the translation:
import React, { useState } from 'react';
import translateText from './GoogleTranslate';

function App() {
  const [inputText, setInputText] = useState('');
  const [targetLanguage, setTargetLanguage] = useState('es'); // Default: Spanish

  const handleTranslate = async () => {
    if (inputText) {
      const translatedText = await translateText(inputText, targetLanguage);
      // Do something with the translatedText, e.g., display it on the page.
    }
  };

  return (
    <div>
      <input
        type="text"
        value={inputText}
        onChange={(e) => setInputText(e.target.value)}
      />
      <select
        value={targetLanguage}
        onChange={(e) => setTargetLanguage(e.target.value)}
      >
        <option value="es">Spanish</option>
        <option value="fr">French</option>
        {/* Add more language options */}
      </select>
      <button onClick={handleTranslate}>Translate</button>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Remember to replace 'YOUR_GOOGLE_TRANSLATE_API_KEY' with your actual API Key in the GoogleTranslate.js file.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (1)

Collapse
 
dandanbohdan profile image
Dan

idk who copypasted this, saw the same article on Medium...
it doesn't work

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay