Best way of using google translation by Python

Question:

I am trying to translate large number of text files from English to other several languages. And we use Python in our project, we try to use Google translation service to translate them first then we will correct the mistakes manually.

I have come up with two ways to translate:

  1. Use Python Google translation API. Here: goslate 1.1.2: Python Package

  2. Try to program with the google translation page, that is, feed in the text that we want to translate, simulate the HTTP request and process the response. Google Translation

Is anyone have a better offer?

Asked By: lowitty

||

Answers:

Google does in fact have an official translation API with a REST interface. You can check it out here. Note that it is a paid API with no free quota.

Answered By: Madison May

I made my own google translate function for python 😉
try it https://github.com/mouuff/Google-Translate-API

Answered By: Arnaud Aliès

Since the origin of this post, connecting to the Google Translate API has become a whole lot easier. That being said, I would still recommend connecting directly to the Google Translate API, but now through it’s RapidAPI page here.

You can find out how to obtain an API key here. Just input the API key into the API’s function page on Rapid API and click TEST Function. For example, that’s what a basic english to german translation will look like:

enter image description here

Just note that de is the language code for German. RapidAPI will generate a code snippet for you so you can just copy and paste the API call directly into your project.

Answered By: David Noah

One of the simplest ways is to use Selenium for getting the translations of the words and phrases.

Here is a piece of code that gets the word in English and returns the Persian (Farsi) translation. Everything is explained in the readme file on Github:

https://github.com/mnosrati/Google-Translate-Farsi

Answered By: user11620055

Try using the googletrans module. For example:

from googletrans import Translator


translator = Translator()  # initalize the Translator object
translations = translator.translate(['see if this helps', 'tarun'], dest='hi')  # translate two phrases to Hindi
for translation in translations:  # print every translation
    print(translation.text)

# Output:
# देखें कि इस मदद करता है
# तरुण

The dicts of the supported languages (106) and their ISO639-1 codes:

import googletrans


print(googletrans.LANGCODES)  # {language name: iso639-1 language code}
# or
print(googletrans.LANGUAGES)  # {iso639-1 language code: language name}

See the docs for more information.

Answered By: TBhavnani

Use this
This code is using google trans module which is free to use.

From this code you can convert any language to any language and also get pronunciation of it.

from googletrans import Translator, LANGUAGES
from googletrans.models import Translated

lang = list(LANGUAGES.values())
print("Welcome to Py_Guy Translate")
input_text = input("Please Enter Your Text in english:n")
out_lang = input("Please enter output language name (ex.-hindi,gujarati,japanese:n 
").lower()
if out_lang not in lang:
    print("Sorry This Language is not available to translate")
else:
    translator = Translator()
    translated = translator.translate(text=input_text, src="english",dest=out_lang)
    translated = str(translated).split(", ")
    converted = translated[2]
    pro = translated[3]
    print(converted)
    print(pro)
Answered By: Dhruv Arne
def translate_text(target, 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
    """

    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "secret.json"

    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=target)

    return result["translatedText"]

Check out the complete code for translate with google api:

https://neculaifantanaru.com/en/example-google-translate-api-key-python-code-beautifulsoup.html

Answered By: Just Me
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.