Created
January 5, 2021 20:15
-
-
Save furkanacaryes/c05a3793712658056bfd2b79e68924cb to your computer and use it in GitHub Desktop.
Google Translate Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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