DEV Community

code lover
code lover

Posted on

17

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.

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (1)

Collapse
 
dandanbohdan profile image
Dan

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

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay