Skip to content

Instantly share code, notes, and snippets.

@furkanacaryes
Created January 5, 2021 20:15
Show Gist options
  • Save furkanacaryes/c05a3793712658056bfd2b79e68924cb to your computer and use it in GitHub Desktop.
Save furkanacaryes/c05a3793712658056bfd2b79e68924cb to your computer and use it in GitHub Desktop.
Google Translate Demo
import os
from google.cloud import translate
from decouple import config
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-account.json"
def translate_text(text):
client = translate.TranslationServiceClient()
parent = f"projects/{config('PROJECT_ID')}/locations/global"
response = client.translate_text(
request={
"parent": parent,
"contents": [text],
"mime_type": "text/plain",
"source_language_code": "en-US",
"target_language_code": "tr",
}
)
for translation in response.translations:
print(f"Translated text: {translation.translated_text}")
translate_text('could have been easier')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment