Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Translate API - Basic Version

Below is one of the python scripts recommended by google to translate text. I am using cloud translation basic version. I want to convert all the text strings to English but if it encounters an English text string it shouldn't translate and just return the initial text string as output. As per my understanding, first it detects the language and then it passes the text for translation. When this entire loop is completed I get charged per character. But when it detects English, does it translates it back to English or does it just detects and passes the string as it is. And do I get charged in the 2nd case?

def translate_text(target: str, text: str) -> dict:
    """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
    """
    from google.cloud import translate_v2 as translate

    translate_client = translate.Client()

    if isinstance(text, bytes):
        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=target)

    print("Text: {}".format(result["input"]))
    print("Translation: {}".format(result["translatedText"]))
    print("Detected source language: {}".format(result["detectedSourceLanguage"]))

    return result

output = translate_text('en', 'text')

 

 

def translate_text(target: str, text: str) -> dict:
    """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
    """
    from google.cloud import translate_v2 as translate

    translate_client = translate.Client()

    if isinstance(text, bytes):
        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=target)

    print("Text: {}".format(result["input"]))
    print("Translation: {}".format(result["translatedText"]))
    print("Detected source language: {}".format(result["detectedSourceLanguage"]))

    return result

 

the script works properly but I how do I ensure that I am not wasting any money to translate English to English or for that matter any language. Like German to German, French to French etc.

Thank You!

Thank you!

1 1 2,289
1 REPLY 1