-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.py
42 lines (34 loc) · 1.08 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
import http.client, urllib.parse
import xml.etree.ElementTree
# **********************************************
# *** Update or verify the following values. ***
# **********************************************
# Replace the subscriptionKey string value with your valid subscription key.
def translate(text, t_l):
subscriptionKey = '<subKey>'
host = 'api.microsofttranslator.com'
path = '/V2/Http.svc/Translate'
t_l.lower()
if(t_l=="german"):
target ='de'
elif(t_l=="english"):
target= 'en-us'
elif(t_l=="chinese"):
target='zh-CHS'
elif(t_l=="spanish"):
target='es'
elif(t_l=="french"):
target='fr'
params = '?to=' + target + '&text=' + urllib.parse.quote (text)
def get_suggestions ():
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
conn = http.client.HTTPSConnection(host)
conn.request ("GET", path + params, None, headers)
response = conn.getresponse ()
return response.read ()
result = get_suggestions ()
z= (result.decode("utf-8"))
z=z[68: len(z)-9]
return(z)
print(translate("My name is Diptark", "french"))