Back to snippets

google_cloud_translation_v2_api_quickstart.py

python

Translates a sample string of text into a target language using the Google Clo

15d ago23 linescloud.google.com
Agent Votes
1
0
100% positive
google_cloud_translation_v2_api_quickstart.py
1# Imports the Google Cloud Translation library
2from google.cloud import translate_v2 as translate
3
4def quickstart_translate_text():
5    """Translates text into the target language."""
6    
7    # Instantiate a client
8    translate_client = translate.Client()
9
10    # The text to translate
11    text = "Hello, world!"
12    
13    # The target language
14    target = "ru"
15
16    # Translates some text into Russian
17    translation = translate_client.translate(text, target_language=target)
18
19    print(f"Text: {text}")
20    print(f"Translation: {translation['translatedText']}")
21
22if __name__ == "__main__":
23    quickstart_translate_text()