-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtranslate.py
80 lines (62 loc) · 2.11 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
@author Emad Ehsan
1. Picks up a line from sourceLang file, e.g. './en/strings.txt'
2. Request google to translate the query
3. Creates a file for targetLang, e.g. './ur/strings.txt'
4. Places translation at exact line number to targetLang file
'''
from html.parser import HTMLParser
import requests
from requests.utils import quote
import binascii
counter = 1
# Locale code of source and target languages
sourceLang = 'en'
targetLang = 'ur'
# Put file name here
filename = 'strings.txt'
'''
For each line in sourceLang file, getTranslation
and put translated line targetLang file
'''
def translate(infile, outfile):
with open(infile) as fin:
with open(outfile, 'wb') as fout:
for line in fin:
outline = bytes(line, 'utf-8')
# print(line)
line = line.strip()
if len(line) == 0:
continue
# now line is prepared to be translated
translation = getTranslation(line)
# add new line at the end
outline = translation + bytes('\n', 'utf-8')
# save
fout.write(outline)
'''
Translates via google translate as described here
https://ctrlq.org/code/19909-google-translate-api
'''
def getTranslation(sentence):
global counter, sourceLang, targetLang
url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + sourceLang
url = url + "&tl=" + targetLang + "&dt=t&q=" + quote(sentence);
print('Request# ' + str(counter) + ': ' + url)
counter += 1
page = requests.get(url)
# strip the response to extract urdu text along with quotes
translation = page.content
translation = translation[3:]
removeLast = 16 + len(sentence)
translation = translation[:-removeLast]
# still has a trailing comma
if (translation[-1] == ','):
translation = translation[:-1]
return translation
def main():
global filename
infile = './' + sourceLang + '/' + filename
outfile = './' + targetLang + '/' + filename
translate(infile, outfile)
main()