The Translation API provides a simple, programmatic interface for dynamically translating an arbitrary string into any supported language using state-of-the-art Neural Machine Translation. It can also be used to detect a language in cases where the source language is unknown.
In this tutorial, you'll use the Translation API with Python. Concepts covered include how to list available languages, translate text, and detect the language of a given text.
By using a kiosk at Google I/O, a test project has been created and can be accessed by using going to: https://console.cloud.google.com/.
These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.
Note that all these accounts will be disabled soon after the codelab is over.
Use these credentials to log into the machine or to open a new Google Cloud Console window https://console.cloud.google.com/. Accept the new account Terms of Service and any updates to Terms of Service.
When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :
While Google Cloud can be operated remotely from your laptop, in this tutorial you will be using Cloud Shell, a command line environment running in the Cloud.
While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Google Cloud Shell, a command line environment running in the Cloud.
From the GCP Console click the Cloud Shell icon on the top right toolbar:
It should only take a few moments to provision and connect to the environment. When it is finished, you should see something like this:
This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on Google Cloud, greatly enhancing network performance and authentication. All of your work in this lab can be done with simply a browser.
Before using the Translation API you must enable it. Enter the following command in the Cloud Shell:
gcloud services enable translate.googleapis.com
In order to make requests to the Translation API you need to use a Service Account. A service account belongs to your project. Service accounts allow the Google Client Python library to make Translation API requests. Like any other user account, a service account is represented by an email address. In this section you'll use the Cloud SDK to create and authenticate a service account.
First, set an environment variable with your PROJECT_ID
which you'll use throughout this tutorial:
export PROJECT_ID=$(gcloud config get-value core/project)
Test that it was set correctly:
echo $PROJECT_ID yourproject-XXXX
Create a new service account to access the Translation API:
gcloud iam service-accounts create my-translation-sa \ --display-name "my translation service account"
Grant your service account the Cloud Translation API User role.
gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member serviceAccount:my-translation-sa@${PROJECT_ID}.iam.gserviceaccount.com \ --role roles/cloudtranslate.user
Create credentials that your Python code will use to log in as your new service account. The credentials are saved as a JSON file ~/key.json
:
gcloud iam service-accounts keys create ~/key.json \ --iam-account my-translation-sa@${PROJECT_ID}.iam.gserviceaccount.com
Finally, set the GOOGLE_APPLICATION_CREDENTIALS
environment variable. This variable will let the Translation API Python library find your credentials. The environment variable should be set to the full path of the credentials JSON file you` created:
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json
For more information, see the Authentication overview page.
Install the client library:
pip3 install --user --upgrade google-cloud-translate
You should see something like this:
... Installing collected packages: google-cloud-translate Successfully installed google-cloud-translate-2.0.1
Now, you're ready to use the Translation API!
In this tutorial, you'll use an interactive Python interpreter called IPython. Start a session by running ipython
in Cloud Shell. This command runs the Python interpreter in an interactive session.
ipython
You should see something like this:
Python 3.7.3 (default, Mar 31 2020, 14:50:17) Type 'copyright', 'credits' or 'license' for more information IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help. In [1]:
In this section, you'll list all available languages in the Translation API.
To list available languages, copy the following code into your IPython session:
from os import environ
from google.cloud import translate
project_id = environ.get('PROJECT_ID')
client = translate.TranslationServiceClient()
parent = client.location_path(project_id, 'global')
response = client.get_supported_languages(parent, 'en')
languages = response.languages
print(f' Languages: {len(languages)} '.center(60, '-'))
for language in languages:
language_code = language.language_code
display_name = language.display_name
print(f'{language_code}\t{display_name}')
Take a minute or two to study the code. Note that you're listing the language names in English but it can be listed in any language by swapping out "en"
with another language code.
You should see the following output:
---------------------- Languages: 109 ---------------------- af Afrikaans sq Albanian am Amharic ar Arabic hy Armenian az Azerbaijani eu Basque be Belarusian ... yi Yiddish yo Yoruba zu Zulu
In this step, you were able to list all available languages in the Translation API. You can find the complete list of supported languages on the Language Support page.
You can use the Translate API to translate text from one language to another. Text is translated using the Neural Machine Translation (NMT) model. If the NMT model is not supported for the requested language translation pair, the Phrase-Based Machine Translation (PBMT) model is used. For more info on Google Translate and its translation models, see the NMT announcement post.
To translate text, copy the following code into your IPython session:
from os import environ
from google.cloud import translate
project_id = environ.get('PROJECT_ID')
client = translate.TranslationServiceClient()
parent = client.location_path(project_id, 'global')
sample_text = 'Hello world!'
response = client.translate_text(
contents=[sample_text],
target_language_code='tr',
parent=parent,
)
for translation in response.translations:
print(translation.translated_text)
Take a minute or two to study the code. It translates the text "Hello World" from English to Turkish.
You should see the following output:
Selam Dünya!
In this step, you were able to use the Translation API to translate text from English to Turkish. Read more about Translating text.
You can use the Translate API to also detect the language of a text string.
To detect language, copy the following code into your IPython session:
from os import environ
from google.cloud import translate
project_id = environ.get('PROJECT_ID')
client = translate.TranslationServiceClient()
parent = client.location_path(project_id, 'global')
def detect_language(text):
response = client.detect_language(parent=parent, content=text)
for languages in response.languages:
confidence = languages.confidence
language_code = languages.language_code
print(
f'Confidence: {confidence:4.0%}',
f'Language: {language_code}',
text,
sep=' | '
)
sentences = [
'Hola Mundo!',
'Hallo Welt!',
'Bonjour le Monde !',
]
for sentence in sentences:
detect_language(sentence)
Take a minute or two to study the code. It detects the language of the text Hola Mundo!" which happens to be a Spanish phrase.
You should see the following output:
Confidence: 100% | Language: es | Hola Mundo! Confidence: 100% | Language: de | Hallo Welt! Confidence: 100% | Language: fr | Bonjour le Monde !
In this step, you were able to detect the language of a piece of text using the Translation API. Read more about Detecting language.
You learned how to use the Translation API using Python!
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial:
This work is licensed under a Creative Commons Attribution 2.0 Generic License.