Using the Translation API with Python

    1. Overview

    1e2217da0416d129.png

    TheTranslation 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.

    What you'll learn

    • How to set up your environment
    • How to list available languages
    • How to translate text
    • How to detect languages

    What you'll need

    • A Google Cloud project
    • A browser, such asChrome orFirefox
    • Familiarity using Python

    Survey

    How will you use this tutorial?

    Read it through onlyRead it and complete the exercises

    How would you rate your experience with Python?

    NoviceIntermediateProficient

    How would you rate your experience with Google Cloud services?

    NoviceIntermediateProficient

    2. Setup and requirements

    Self-paced environment setup

    1. Sign-in to theGoogle Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you mustcreate one.

    fbef9caa1602edd0.png

    a99b7ace416376c4.png

    5e3ff691252acf41.png

    • TheProject name is the display name for this project's participants. It is a character string not used by Google APIs. You can always update it.
    • TheProject ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference your Project ID (typically identified asPROJECT_ID). If you don't like the generated ID, you might generate another random one. Alternatively, you can try your own, and see if it's available. It can't be changed after this step and remains for the duration of the project.
    • For your information, there is a third value, aProject Number, which some APIs use. Learn more about all three of these values in thedocumentation.

    Caution: A project ID is globally unique and can't be used by anyone else after you've selected it. You are the only user of that ID. Even if a project is deleted, the ID can't be used again

    Note: If you use a Gmail account, you can leave the default location set toNo organization. If you use a Google Workspace account, choose a location that makes sense for your organization.

    1. Next, you'll need toenable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab won't cost much, if anything at all. To shut down resources to avoid incurring billing beyond this tutorial, you can delete the resources you created or delete the project. New Google Cloud users are eligible for the$300 USD Free Trial program.

    Start Cloud Shell

    While Google Cloud can be operated remotely from your laptop, in this codelab you will be usingCloud Shell, a command line environment running in the Cloud.

    Activate Cloud Shell

    1. From the Cloud Console, clickActivate Cloud Shell853e55310c205094.png.

    3c1dabeca90e44e5.png

    If this is your first time starting Cloud Shell, you're presented with an intermediate screen describing what it is. If you were presented with an intermediate screen, clickContinue.

    9c92662c6a846a5c.png

    It should only take a few moments to provision and connect to Cloud Shell.

    9f0e51b578fecce5.png

    This virtual machine is loaded with all the development tools needed. It offers a persistent 5 GB home directory and runs in Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this codelab can be done with a browser.

    Once connected to Cloud Shell, you should see that you are authenticated and that the project is set to your project ID.

    1. Run the following command in Cloud Shell to confirm that you are authenticated:
    gcloud auth list

    Command output

     Credentialed AccountsACTIVE  ACCOUNT*       <my_account>@<my_domain.com>To set the active account, run:    $ gcloud config set account `ACCOUNT`

    Note: Thegcloud command-line tool is the powerful and unified command-line tool in Google Cloud. It comes preinstalled in Cloud Shell. You will notice its support for tab completion.You may be prompted to authenticate the first time you run a command. For more information, seegcloud command-line tool overview.

    1. Run the following command in Cloud Shell to confirm that the gcloud command knows about your project:
    gcloud config list project

    Command output

    [core]project = <PROJECT_ID>

    If it is not, you can set it with this command:

    gcloud config set project <PROJECT_ID>

    Command output

    Updated property [core/project].

    Note: If you're completing this tutorial outside of Cloud Shell, followSet up Application Default Credentials.

    3. Environment setup

    Before you can begin using the Translation API, run the following command in Cloud Shell to enable the API:

    gcloudservicesenabletranslate.googleapis.com

    You should see something like this:

    Operation "operations/..." finished successfully.

    Now, you can use the Translation API!

    Set the following environment variable (to be used in your application):

    exportPROJECT_ID=$(gcloudconfigget-valuecore/project)echo"PROJECT_ID: $PROJECT_ID"

    Note: This environment variable only applies to your current shell session. Therefore, if you open a new session, set this variable again.

    Navigate to your home directory:

    cd~

    Create a Python virtual environment to isolate the dependencies:

    virtualenvvenv-translate

    Activate the virtual environment:

    sourcevenv-translate/bin/activate

    Note: To stop using the virtual environment and go back to your system Python version, you can use thedeactivate command.

    Install IPython and the Translation API client library:

    pipinstallipythongoogle-cloud-translate

    You should see something like this:

    ...Installing collected packages: ..., ipython, google-cloud-translateSuccessfully installed ... google-cloud-translate-3.16.0 ...

    Now, you're ready to use the Translation API client library!

    Note: If you're setting up your own Python development environment outside of Cloud Shell, you can follow theseguidelines.

    In the next steps, you'll use an interactive Python interpreter calledIPython, which you installed in the previous step. Start a session by runningipython in Cloud Shell:

    ipython

    You should see something like this:

    Python 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]Type 'copyright', 'credits' or 'license' for more informationIPython 8.27.0 -- An enhanced Interactive Python. Type '?' for help.In [1]:

    Note: If needed, you can quit your IPython session with theexit command.

    Copy the following code into your IPython session:

    fromosimportenvironfromgoogle.cloudimporttranslatePROJECT_ID=environ.get("PROJECT_ID","")assertPROJECT_IDPARENT=f"projects/{PROJECT_ID}"

    Notes:

    • This uses thePROJECT_ID environment variable defined earlier.
    • ThePARENT variable will be used for requests in the next steps. It refers to the caller's project.

    You're ready to make your first request and list the supported languages...

    4. List available languages

    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:

    defprint_supported_languages(display_language_code:str):client=translate.TranslationServiceClient()response=client.get_supported_languages(parent=PARENT,display_language_code=display_language_code,)languages=response.languagesprint(f" Languages:{len(languages)} ".center(60,"-"))forlanguageinlanguages:language_code=language.language_codedisplay_name=language.display_nameprint(f"{language_code:10}{display_name}")

    Call the function:

    print_supported_languages("en")

    You should get something like this:

    ---------------------- Languages: 137 ----------------------af        Afrikaanssq        Albanianam        Amharicar        Arabichy        Armenian...cy        Welshxh        Xhosayi        Yiddishyo        Yorubazu        Zulu

    If you get aPermissionDenied error, typeexit to quit IPython, and return to theSetup and requirements step.

    Check out what you get with the display language in French:

    print_supported_languages("fr")

    You should get the same list, sorted with the French names, similar to the following:

    ---------------------- Languages: 137 ----------------------af        Afrikaanssq        Albanaisde        Allemandam        Amhariqueen        Anglais...vi        Vietnamienxh        Xhosayi        Yiddishyo        Yorubazu        Zoulou

    You can try with anotherlanguage code.

    Summary

    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 thelanguage support page.

    5. Translate text

    You can use the Translation 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 on Google Translate and its translation models, see theNMT announcement post.

    To translate text, copy the following code into your IPython session:

    deftranslate_text(text:str,target_language_code:str)->translate.Translation:client=translate.TranslationServiceClient()response=client.translate_text(parent=PARENT,contents=[text],target_language_code=target_language_code,)returnresponse.translations[0]

    Call the function to translate the same text in different languages:

    text="Hello World!"target_languages=["tr","de","es","it","el","zh","ja","ko"]print(f"{text} ".center(50,"-"))fortarget_languageintarget_languages:translation=translate_text(text,target_language)source_language=translation.detected_language_codetranslated_text=translation.translated_textprint(f"{source_language}{target_language} :{translated_text}")

    You should get the following:

    ------------------ Hello World! ------------------en → tr : Selam Dünya!en → de : Hallo Welt!en → es : ¡Hola Mundo!en → it : Ciao mondo!en → el : Γεια σου Κόσμο!en → zh : 你好世界!en → ja : 「こんにちは世界」en → ko : 안녕하세요!

    Summary

    In this step, you were able to use the Translation API to translate text into multiple languages. Read more abouttranslating text.

    6. Detect languages

    You can also use the Translation API to detect the language of a text string.

    Copy the following code into your IPython session:

    defdetect_language(text:str)->translate.DetectedLanguage:client=translate.TranslationServiceClient()response=client.detect_language(parent=PARENT,content=text)returnresponse.languages[0]

    Call the function to detect the language of different sentences:

    sentences=["Selam Dünya!","Hallo Welt!","¡Hola Mundo!","Ciao mondo!","Γεια σου Κόσμο!","你好世界!","「こんにちは世界」","안녕하세요!",]forsentenceinsentences:language=detect_language(sentence)confidence=language.confidencelanguage_code=language.language_codeprint(f"Confidence:{confidence:4.0%}",f"Language:{language_code:5}",sentence,sep=" | ",)

    You should get the following:

    Confidence: 100% | Language: tr    | Selam Dünya!Confidence:  81% | Language: de    | Hallo Welt!Confidence: 100% | Language: es    | ¡Hola Mundo!Confidence: 100% | Language: it    | Ciao mondo!Confidence: 100% | Language: el    | Γεια σου Κόσμο!Confidence: 100% | Language: zh-CN | 你好世界!Confidence: 100% | Language: ja    | 「こんにちは世界」Confidence: 100% | Language: ko    | 안녕하세요!

    Summary

    In this step, you were able to detect the language of a piece of text using the Translation API. Read more aboutdetecting languages.

    7. Congratulations!

    1e2217da0416d129.png

    You learned how to use the Translation API using Python!

    Clean up

    To clean up your development environment, from Cloud Shell:

    • If you're still in your IPython session, go back to the shell:exit
    • Stop using the Python virtual environment:deactivate
    • Delete your virtual environment folder:cd ~ ; rm -rf ./venv-translate

    To delete your Google Cloud project, from Cloud Shell:

    • Retrieve your current project ID:PROJECT_ID=$(gcloud config get-value core/project)
    • Make sure this is the project you want to delete:echo $PROJECT_ID
    • Delete the project:gcloud projects delete $PROJECT_ID

    Learn more

    License

    This work is licensed under a Creative Commons Attribution 2.0 Generic License.

    Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.