30

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?

lowitty
  • 904
  • 1
  • 8
  • 13
  • What do you need to be better? What is wrong with 1. and 2 ? – TessellatingHeckler Mar 23 '14 at 04:34
  • 1
    Both of the 2 solution have a really low efficiency. And I want to know if Google have an official API or not. – lowitty Mar 23 '14 at 04:39
  • Possible duplicate of [Python script to translate via google translate](http://stackoverflow.com/questions/9404628/python-script-to-translate-via-google-translate) – eiTan LaVi Jan 18 '17 at 13:29
  • The question is 6 years old now. So I ll post this suggestion in case anyone is looking for such information at the mean time. You may want to take a look at this tool: https://github.com/nidhaloff/deep_translator – basilisk Aug 05 '20 at 18:57

7 Answers7

26

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

Arnaud Aliès
  • 1,079
  • 13
  • 26
17

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.

Madison May
  • 2,723
  • 3
  • 22
  • 32
  • 1
    Yes, it is only available as a paid service. Any other methods of hitting the Google Translate API are likely not legal. – Madison May Mar 23 '14 at 04:59
  • So what do you think about the google translate API goslate? Is it illegal? – lowitty Mar 23 '14 at 05:02
  • Most likely its not entirely moral/ethical -- I just took a look at their source and it looks like goslate is faking a User-Agent string in order to scrape the page results. Google blocks python User-Agents from accessing the endpoint by default. – Madison May Mar 23 '14 at 05:09
  • 1
    I selected your answer. Just last question, I tried the goslate and it works, do you mean there is a risk that the goslate may not work for being blocked by Google? – lowitty Mar 23 '14 at 05:15
  • I also just tried goslate and got valid results, but there's simply no way that package can be complying with Google's terms of service for their Translate API. Especially if this is for commercial purposes, I'd steer clear of goslate for now. :( – Madison May Mar 23 '14 at 05:17
  • 1
    Google, after asking the world to improve its translate service by providing translations, now it sales it back! Go Google!! – Vassilis Jul 19 '17 at 16:17
10

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.

Demian Wolf
  • 1,698
  • 2
  • 14
  • 34
TBhavnani
  • 721
  • 7
  • 12
  • 1
    `googletrans` does not work sometimes. I am not sure it is good for batch translations. This issue is even mentioned in the website. https://pypi.org/project/googletrans/ Thanks & Best Regards – Alain Michael Janith Schroter May 02 '20 at 02:42
  • 1
    +1 on what was said above. I tried batch translations with googletrans. After roughly 800 translations the strings are returned without an actual translation and warning or exception is raised. This is likely a safety measure from google (as in to avoid misuse of the free number of translations), but I don't actually know – rob Sep 21 '20 at 13:51
1

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

0

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)
Machavity
  • 30,841
  • 27
  • 92
  • 100
Dhruv Arne
  • 113
  • 3
  • 14
0
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

Just Me
  • 864
  • 2
  • 18
  • 28
-4

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.

David Noah
  • 69
  • 3