Getting Started with Translation
Source notebook
Repo path:
translation/intro_translation.ipynb· Open on GitHub · intro
Translates English text to Spanish with Cloud Translation API and a Cloud Storage-backed glossary.
Summary
This notebook teaches basic use of the Google Cloud Translation API for translating text from English to Spanish. It sets project, region, and authentication, creates a TranslationServiceClient, defines helpers for text translation and glossary creation, then demonstrates default translation versus glossary-based translation for Google Cloud product names.
Key code patterns
Create Translation client
from google.cloud import translate
client = translate.TranslationServiceClient()Creates the Cloud Translation API client used for translate and glossary requests.
Translate text
response = client.translate_text(
request=translate.TranslateTextRequest(
parent=client.common_location_path(project_id, location),
contents=[text],
source_language_code="en",
target_language_code="es",
)
)Shows the core request shape for translating English input text to Spanish.
Optional glossary config
glossary_config=(
translate.TranslateTextGlossaryConfig(glossary=glossary)
if glossary
else None
)Adds a glossary only when supplied, allowing the same helper to handle default and glossary translations.
Create glossary from GCS
glossary = translate.Glossary(
name=client.glossary_path(project_id, location, glossary_id),
language_pair=translate.Glossary.LanguageCodePair(
source_language_code="en", target_language_code="es"
),
input_config=translate.GlossaryInputConfig(
gcs_source=translate.GcsSource(input_uri=input_uri)
),
)
operation = client.create_glossary(
parent=client.common_location_path(project_id, location), glossary=glossary
)Defines a unidirectional glossary sourced from a Cloud Storage TSV file.
Models & APIs used
- APIs / services: Cloud Translation API, Cloud Storage
- SDKs / libraries:
google-cloud-translate
When to use this
Use this pattern when translating text with Cloud Translation API and enforcing domain-specific terminology through glossaries.
Gotchas & caveats
- Cloud Translation and Cloud Storage are billable components.
- Translation has documented available regions and optional advanced endpoints.
- Colab requires user authentication, project configuration, and application-default login.
- Glossary source data is read from Cloud Storage, such as a TSV file in a bucket.
- Glossaries are for words or short phrases, usually fewer than five words.
Best practices
- Use environment variables for project ID and region when notebook parameters are not provided.
- Set source_language_code and target_language_code explicitly.
- Use client.common_location_path for regional Translation API resource paths.
- Use glossaries to consistently translate domain-specific words and phrases.
- Check supported language codes and supported content formats in the Translation API documentation.
Related
- Concepts: Getting Started · Translation
- Entities: Cloud Storage
- Area: Translation Notebooks
- Best practices: Getting Started - Best Practices · Translation - Best Practices