-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslationExp.py
More file actions
70 lines (52 loc) · 2.28 KB
/
TranslationExp.py
File metadata and controls
70 lines (52 loc) · 2.28 KB
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
# Import required libs
import re
import json
import http.client
# Simple script config
toLang = 'sv'
conn = http.client.HTTPSConnection('google-translate1.p.rapidapi.com')
file_path = 'to_file_path'
rapidapi_key = 'your_key_here'
# Main func
def main():
counter = 1
with open(file_path, 'r', encoding='utf-8-sig') as file:
print('Parsing & caching json file...')
data = json.load(file)
with open(file_path, 'w', encoding='utf-8-sig') as file:
print('Truncating old json file.')
file.truncate()
with open(file_path, 'r', encoding='utf-8-sig') as file:
for string in data:
if len(string) > 2:
data[string] = handleString(translate(data[string], string))
print('string ' + string + ' was translated successfully [' + data[string] + '] (' + str(counter) + '/' + str(len(data)) + ').')
counter += 1
with open(file_path, 'w') as file:
print('Writing & closing replaced file...')
file.write(json.dumps(data))
file.close()
print('New json file saved.')
# Translate Func
def translate(input, label):
payload = 'q=' + input + '&target=' + toLang + '&source=en'
try:
headers = {'content-type': 'application/x-www-form-urlencoded','accept-encoding': 'application/gzip','x-rapidapi-host': 'google-translate1.p.rapidapi.com','x-rapidapi-key': rapidapi_key }
conn.request('POST', '/language/translate/v2', payload, headers)
res = conn.getresponse()
data = res.read()
data = data.decode('utf-8')
data = json.loads(data)
data = data['data']['translations'][0]['translatedText']
except:
data = ':<?>: An exception occured for label ' + label + ' :<?>: '
return data
# handling the string to make it "correct" in terms of GTA, ~ ? ~ space handling.
def handleString(str):
str = re.sub(r'~ ([A-Z\n_]*) ~', r'~\1~', str) # only uppercased for control displaying
str = re.sub(r'~ (.) ~', r'~\1~', str) # one character only, used for coloring
str = re.sub(r' ~(.)~ ', r' ~\1~', str) # used to place coloring at the right spot to avoid double spaces
str = re.sub(r' ~ ', r' ', str) # remove random flawed stuff just causing double spaces to occur
return str
# Run function tree
main()