Google Translate Unofficial API

1.0.4 · active · verified Tue Apr 21

The `translate-google-api` library provides a free and unofficial interface to Google Translate services, supporting both single and multi-segment text translation. As of version 1.0.4, it offers a programmatic way to interact with Google Translate without requiring official, paid Google Cloud Translation API tokens. A key differentiator is its ability to handle multi-segment text efficiently within a single request, addressing limitations found in some alternative unofficial libraries which might require multiple requests for segmented content. It aims to provide a reliable translation service by leveraging the same servers used by `translate.google.com` and includes comprehensive proxy support for complex network configurations. This package is compatible with ReactJS, React Native, and Node.js environments. While no explicit release cadence is stated, updates appear to be made as needed, primarily focusing on stability and feature enhancements. Being an unofficial client, its long-term stability is dependent on Google's public translation endpoint.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `translate-google-api` to translate multiple text segments simultaneously, specifying source and target languages, and illustrates error handling.

import translate from 'translate-google-api';

async function performTranslation() {
  const textToTranslate = [
    process.env.TEXT_SEGMENT_1 ?? 'Hello world!',
    process.env.TEXT_SEGMENT_2 ?? 'How are you today?',
    process.env.TEXT_SEGMENT_3 ?? 'This is a test of multi-segment translation.'
  ];
  const targetLanguage = process.env.TO_LANG ?? 'es'; // Translate to Spanish by default
  const fromLanguage = process.env.FROM_LANG ?? 'auto'; // Auto-detect source language

  try {
    const result = await translate(textToTranslate, {
      from: fromLanguage,
      to: targetLanguage,
      tld: process.env.GOOGLE_TLD ?? 'com' // Use 'cn' for China, 'com' for others
    });
    console.log(`Translated from '${fromLanguage}' to '${targetLanguage}':`);
    result.forEach((segment, index) => {
      console.log(`Segment ${index + 1}: ${textToTranslate[index]} -> ${segment}`);
    });
  } catch (error) {
    console.error('Translation failed:', error.message);
  }
}

performTranslation();

view raw JSON →