CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-google-cloud-translate

Google Cloud Translate API client library for translating text between thousands of language pairs with support for adaptive MT, AutoML, and glossaries

Pending
Overview
Eval results
Files

v2-legacy-api.mddocs/

V2 Legacy API

The V2 API provides simple REST-based translation functionality with basic features for text translation, language detection, and supported language listing. This is the legacy API and users are encouraged to migrate to V3 for access to advanced features.

Core Import

from google.cloud import translate_v2 as translate

Client Class

class Client:
    """
    V2 Translation API client.
    
    Args:
        target_language (str, optional): Default target language for translations. Defaults to 'en'.
        credentials: OAuth2 Credentials for authentication
        _http: HTTP session object for requests
        client_info: Client info for user-agent string
        client_options: Client options including API endpoint
    """
    def __init__(
        self,
        target_language="en",
        credentials=None,
        _http=None,
        client_info=None,
        client_options=None,
    ): ...

Capabilities

Text Translation

Translate text strings from one language to another with optional source language detection and custom models.

def translate(
    self,
    values,
    target_language=None,
    format_=None,
    source_language=None,
    customization_ids=(),
    model=None
):
    """
    Translate a string or list of strings.
    
    Args:
        values (str or list): Text string(s) to translate
        target_language (str, optional): Target language code (ISO 639-1)
        format_ (str, optional): Format of input text ('text' or 'html')
        source_language (str, optional): Source language code for translation
        customization_ids (tuple, optional): Custom model IDs to use
        model (str, optional): Translation model ('base' or 'nmt')
    
    Returns:
        dict or list: Translation results with translated text and metadata
    """

Language Detection

Detect the language of input text strings with confidence scores.

def detect_language(self, values):
    """
    Detect the language of a string or list of strings.
    
    Args:
        values (str or list): Text string(s) to analyze
    
    Returns:
        dict or list: Detection results with language codes and confidence
    """

Supported Languages

Get a list of all languages supported by the Translation API.

def get_languages(self, target_language=None):
    """
    Get list of supported languages for translation.
    
    Args:
        target_language (str, optional): Language code for language names
    
    Returns:
        list: List of supported language objects with codes and names
    """

Constants

ENGLISH_ISO_639 = "en"  # ISO 639-1 language code for English
BASE = "base"  # Base translation model
NMT = "nmt"  # Neural Machine Translation model
SCOPE = ("https://www.googleapis.com/auth/cloud-platform",)  # OAuth2 scopes

Usage Examples

Basic Translation

from google.cloud import translate_v2 as translate

client = translate.Client()

# Translate a single string
result = client.translate(
    "Hello, world!",
    target_language="es"
)

print(f"Original: {result['input']}")
print(f"Translation: {result['translatedText']}")
print(f"Detected language: {result['detectedSourceLanguage']}")

Multiple Text Translation

from google.cloud import translate_v2 as translate

client = translate.Client()

# Translate multiple strings
texts = ["Hello", "How are you?", "Goodbye"]
results = client.translate(
    texts,
    target_language="fr",
    source_language="en"
)

for result in results:
    print(f"{result['input']} -> {result['translatedText']}")

Language Detection

from google.cloud import translate_v2 as translate

client = translate.Client()

# Detect language of text
result = client.detect_language("Bonjour le monde")

print(f"Language: {result['language']}")
print(f"Confidence: {result['confidence']}")

List Supported Languages

from google.cloud import translate_v2 as translate

client = translate.Client()

# Get all supported languages
languages = client.get_languages()

for language in languages:
    print(f"{language['language']}: {language['name']}")

# Get language names in Spanish
languages_es = client.get_languages(target_language="es")

for language in languages_es:
    print(f"{language['language']}: {language['name']}")

Response Format

Translation Response

{
    'translatedText': 'Hola, mundo!',
    'detectedSourceLanguage': 'en',
    'input': 'Hello, world!'
}

Detection Response

{
    'language': 'fr',
    'confidence': 1.0,
    'input': 'Bonjour le monde'
}

Languages Response

[
    {'language': 'en', 'name': 'English'},
    {'language': 'es', 'name': 'Spanish'},
    {'language': 'fr', 'name': 'French'},
    ...
]

Install with Tessl CLI

npx tessl i tessl/pypi-google-cloud-translate

docs

index.md

v2-legacy-api.md

v3-adaptive-mt.md

v3-automl-translation.md

v3-data-structures.md

v3-glossary-management.md

v3-request-response-types.md

v3-translation-services.md

tile.json