2

I want to use deepl translate api for my university project, but I can't parse it. I want to use it wit PHP or with Python, because the argument I'll pass to a python script so it's indifferent to me which will be the end. I tried in php like this:

    $original =  $_GET['searchterm'];
    $deeplTranslateURL='https://api-free.deepl.com/v2/translate?auth_key=MYKEY&text='.urlencode($original).'&target_lang=EN';
    if (get_headers($deeplTranslateURL)[0]=='HTTP/1.1 200 OK') {
        $translated = str_replace(' ', '', json_decode(file_get_contents($deeplTranslateURL))["translations"][0]["text"]);
    }else{
        echo("translate error");
    }

    $output = passthru("python search.py $original $translated");

and I tried also in search.py based this answer:

#!/usr/bin/env python
import sys
import requests

r =  requests.post(url='https://api.deepl.com/v2/translate',
                          data = {
                            'target_lang' : 'EN',  
                            'auth_key' : 'MYKEY',
                            'text': str(sys.argv)[1]
                          })

print 'Argument:', sys.argv[1]
print 'Argument List:', str(sys.argv)
print 'translated to: ', str(r.json()["translations"][0]["text"])

But neither got me any answer, how can I do correctly? Also I know I can do it somehow in cURL but I didn't used that lib ever.

Joffrey Schmitz
  • 2,393
  • 3
  • 19
  • 28
alma korte
  • 135
  • 7

1 Answers1

1

DeepL now has a python library that makes translation with python much easier, and eliminates the need to use requests and parse a response.

Get started as such:

import deepl
translator = deepl.Translator(auth_key)
result = translator.translate_text(text_you_want_to_translate, target_lang="EN-US")
print(result)

Looking at your question, it looks like search.py might have a couple problems, namely that sys splits up every individual word into a single item in a list, so you're only passing a single word to DeepL. This is a problem because DeepL is a contextual translator: it builds a translation based on the words in a sentence - it doesn't simply act as a dictionary for individual words. If you want to translate single words, DeepL API probably isn't what you want to go with.

However, if you are actually trying to pass a sentence to DeepL, I have built out this new search.py that should work for you:

import sys
import deepl


auth_key="your_auth_key"
translator = deepl.Translator(auth_key)

"""
" ".join(sys.argv[1:]) converts all list items after item [0]
into a string separated by spaces
"""

result = translator.translate_text(" ".join(sys.argv[1:]), target_lang = "EN-US")

print('Argument:', sys.argv[1])
print('Argument List:', str(sys.argv))


print("String to translate: ", " ".join(sys.argv[1:]))
print("Translated String:", result)

I ran the program by entering this:

search.py Der Künstler wurde mit einem Preis ausgezeichnet.

and received this output:

Argument: Der
Argument List: ['search.py', 'Der', 'Künstler', 'wurde', 'mit', 'einem', 
'Preis', 'ausgezeichnet.']
String to translate:  Der Künstler wurde mit einem Preis ausgezeichnet.
Translated String: The artist was awarded a prize.

I hope this helps, and that it's not too far past the end of your University Project!

Thomas
  • 192
  • 2
  • 10