Google Cloud Translate API client library for translating text between thousands of language pairs with support for adaptive MT, AutoML, and glossaries
—
Core translation functionality in the V3 API, including text translation, document translation, batch operations, language detection, romanization, and supported language queries. This is the recommended API version for new applications.
from google.cloud import translate_v3
from google.cloud.translate_v3 import TranslationServiceClientclass TranslationServiceClient:
"""
Synchronous client for Translation Service V3 API.
Args:
credentials: OAuth2 credentials for authentication
transport: Transport implementation ('grpc', 'grpc_asyncio', 'rest')
client_options: Client configuration options
client_info: Client info for user-agent string
"""
def __init__(
self,
*,
credentials=None,
transport=None,
client_options=None,
client_info=None,
): ...
class TranslationServiceAsyncClient:
"""
Asynchronous client for Translation Service V3 API.
"""
def __init__(
self,
*,
credentials=None,
transport="grpc_asyncio",
client_options=None,
client_info=None,
): ...Translate text content with advanced options including glossaries, models, and format specifications.
def translate_text(
self,
request=None,
*,
parent=None,
contents=None,
mime_type=None,
source_language_code=None,
target_language_code=None,
retry=None,
timeout=None,
metadata=()
):
"""
Translates input text and returns translated text.
Args:
request (TranslateTextRequest): The request object
parent (str): Project/location resource name (projects/{project}/locations/{location})
contents (list): Text content to translate
mime_type (str): MIME type of content ('text/plain' or 'text/html')
source_language_code (str): Source language code (BCP-47)
target_language_code (str): Target language code (BCP-47)
retry: Retry configuration
timeout (float): Request timeout in seconds
metadata: Additional metadata
Returns:
TranslateTextResponse: Translation results with detected language info
"""Translate entire documents while preserving formatting and structure.
def translate_document(
self,
request=None,
*,
parent=None,
source_language_code=None,
target_language_code=None,
document_input_config=None,
document_output_config=None,
model=None,
glossary_config=None,
retry=None,
timeout=None,
metadata=()
):
"""
Translates documents in synchronous mode.
Args:
request (TranslateDocumentRequest): The request object
parent (str): Project/location resource name
source_language_code (str): Source language code
target_language_code (str): Target language code
document_input_config (DocumentInputConfig): Document input configuration
document_output_config (DocumentOutputConfig): Document output configuration
model (str): Custom model to use for translation
glossary_config (TranslateTextGlossaryConfig): Glossary configuration
retry: Retry configuration
timeout (float): Request timeout in seconds
metadata: Additional metadata
Returns:
TranslateDocumentResponse: Document translation result
"""Translate large volumes of text content asynchronously with Cloud Storage integration.
def batch_translate_text(
self,
request=None,
*,
parent=None,
source_language_code=None,
target_language_codes=None,
input_configs=None,
output_config=None,
retry=None,
timeout=None,
metadata=()
):
"""
Translates a large volume of text in asynchronous batch mode.
Args:
request (BatchTranslateTextRequest): The request object
parent (str): Project/location resource name
source_language_code (str): Source language code
target_language_codes (list): List of target language codes
input_configs (list): Input file configurations
output_config (OutputConfig): Output configuration
retry: Retry configuration
timeout (float): Request timeout in seconds
metadata: Additional metadata
Returns:
Operation: Long-running operation for batch translation
"""Translate multiple documents asynchronously while preserving formatting.
def batch_translate_document(
self,
request=None,
*,
parent=None,
source_language_code=None,
target_language_codes=None,
input_configs=None,
output_config=None,
retry=None,
timeout=None,
metadata=()
):
"""
Translates a large volume of documents in asynchronous batch mode.
Args:
request (BatchTranslateDocumentRequest): The request object
parent (str): Project/location resource name
source_language_code (str): Source language code
target_language_codes (list): List of target language codes
input_configs (list): Input configuration for documents
output_config (BatchDocumentOutputConfig): Output configuration
retry: Retry configuration
timeout (float): Request timeout in seconds
metadata: Additional metadata
Returns:
Operation: Long-running operation for batch document translation
"""Detect the language of input text with confidence scores and multiple language detection.
def detect_language(
self,
request=None,
*,
parent=None,
content=None,
mime_type=None,
model=None,
retry=None,
timeout=None,
metadata=()
):
"""
Detects the language of text within a request.
Args:
request (DetectLanguageRequest): The request object
parent (str): Project/location resource name
content (str): Text content to analyze
mime_type (str): MIME type of content
model (str): Optional model to use for detection
retry: Retry configuration
timeout (float): Request timeout in seconds
metadata: Additional metadata
Returns:
DetectLanguageResponse: Language detection results with confidence scores
"""Query available translation languages and their capabilities.
def get_supported_languages(
self,
request=None,
*,
parent=None,
display_language_code=None,
model=None,
retry=None,
timeout=None,
metadata=()
):
"""
Returns a list of supported languages for translation.
Args:
request (GetSupportedLanguagesRequest): The request object
parent (str): Project/location resource name
display_language_code (str): Language code for language names
model (str): Optional model to get languages for
retry: Retry configuration
timeout (float): Request timeout in seconds
metadata: Additional metadata
Returns:
SupportedLanguages: List of supported languages with metadata
"""Convert text to Latin script for languages that use non-Latin writing systems.
def romanize_text(
self,
request=None,
*,
parent=None,
contents=None,
source_language_code=None,
retry=None,
timeout=None,
metadata=()
):
"""
Romanize input text to Latin script.
Args:
request (RomanizeTextRequest): The request object
parent (str): Project/location resource name
contents (list): Text content to romanize
source_language_code (str): Source language code
retry: Retry configuration
timeout (float): Request timeout in seconds
metadata: Additional metadata
Returns:
RomanizeTextResponse: Romanized text results
"""from google.cloud import translate_v3
client = translate_v3.TranslationServiceClient()
parent = "projects/my-project/locations/us-central1"
response = client.translate_text(
request={
"parent": parent,
"contents": ["Hello, world!", "How are you?"],
"mime_type": "text/plain",
"source_language_code": "en",
"target_language_code": "es",
}
)
for translation in response.translations:
print(f"Translated: {translation.translated_text}")from google.cloud import translate_v3
client = translate_v3.TranslationServiceClient()
parent = "projects/my-project/locations/us-central1"
# Read document content
with open("document.txt", "rb") as f:
document_content = f.read()
document_input_config = {
"content": document_content,
"mime_type": "text/plain"
}
response = client.translate_document(
request={
"parent": parent,
"source_language_code": "en",
"target_language_code": "fr",
"document_input_config": document_input_config,
}
)
print(f"Translated document: {response.document_translation.byte_stream_outputs}")from google.cloud import translate_v3
client = translate_v3.TranslationServiceClient()
parent = "projects/my-project/locations/us-central1"
response = client.detect_language(
request={
"parent": parent,
"content": "Bonjour le monde",
"mime_type": "text/plain",
}
)
for language in response.languages:
print(f"Language: {language.language_code}")
print(f"Confidence: {language.confidence}")from google.cloud import translate_v3
client = translate_v3.TranslationServiceClient()
parent = "projects/my-project/locations/us-central1"
input_config = {
"gcs_source": {
"input_uri": "gs://my-bucket/input/"
},
"mime_type": "text/plain"
}
output_config = {
"gcs_destination": {
"output_uri_prefix": "gs://my-bucket/output/"
}
}
operation = client.batch_translate_text(
request={
"parent": parent,
"source_language_code": "en",
"target_language_codes": ["es", "fr"],
"input_configs": [input_config],
"output_config": output_config,
}
)
print(f"Operation name: {operation.name}")
# Wait for operation to complete
result = operation.result(timeout=3600)
print(f"Total characters: {result.total_characters}")
print(f"Translated characters: {result.translated_characters}")Install with Tessl CLI
npx tessl i tessl/pypi-google-cloud-translate