47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
import json
|
|
from dotenv import load_dotenv
|
|
|
|
import six
|
|
from google.cloud import translate_v2 as translate
|
|
|
|
|
|
def translate_text(text):
|
|
"""Translates text into the target language.
|
|
|
|
Target must be an ISO 639-1 language code.
|
|
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
|
|
"""
|
|
|
|
translate_client = translate.Client()
|
|
|
|
if isinstance(text, six.binary_type):
|
|
text = text.decode("utf-8")
|
|
|
|
# Text can also be a sequence of strings, in which case this method
|
|
# will return a sequence of results for each text.
|
|
result = translate_client.translate(text, target_language="sk")
|
|
|
|
# print(u"Text: {}".format(result["input"]))
|
|
# print(u"Translation: {}".format(result["translatedText"]))
|
|
# print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))
|
|
return result['translatedText']
|
|
|
|
|
|
def translate_test():
|
|
try:
|
|
translate_text("Hello")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
load_dotenv()
|
|
|
|
if translate_test():
|
|
print("Translate keys work")
|
|
|