Skip to content

GCP Translate Connector

GcpTranslate

Instantiate a GCP Translate object.

Parameters:

Name Type Description Default
credential_file str

Credential json file

required
proxy str

Proxy address

''
Source code in honeydew/gcp_translate.py
Python
class GcpTranslate:
    """
    Instantiate a GCP Translate object.

    Args:
        credential_file (str): Credential json file
        proxy (str): Proxy address
    """
    def __init__(self, credential_file, proxy=''):
        self.credential_file = credential_file
        self.proxy = proxy
        os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credential_file
        if proxy != '':
            os.environ['HTTP_PROXY'] = proxy
            os.environ['HTTPS_PROXY'] = proxy

    # Translate text from one language to another language
    def translate_text(self, text, target_language='en'):
        """
        Translate text from one language to another language.

        Args:
            text (str): Text to be translated
            target_language (str): Target language. Default: 'en'

        Returns:
            result (str): Translated text
        """
        translate_client = translate.Client()
        result = translate_client.translate(text, target_language=target_language)
        return result['translatedText']

translate_text(text, target_language='en')

Translate text from one language to another language.

Parameters:

Name Type Description Default
text str

Text to be translated

required
target_language str

Target language. Default: 'en'

'en'

Returns:

Name Type Description
result str

Translated text

Source code in honeydew/gcp_translate.py
Python
def translate_text(self, text, target_language='en'):
    """
    Translate text from one language to another language.

    Args:
        text (str): Text to be translated
        target_language (str): Target language. Default: 'en'

    Returns:
        result (str): Translated text
    """
    translate_client = translate.Client()
    result = translate_client.translate(text, target_language=target_language)
    return result['translatedText']