-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunMe.py
68 lines (48 loc) · 2.32 KB
/
runMe.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
import re
import requests
import os
from google.cloud import translate
translate_client = translate.Client()
translateAPI = "https://www.googleapis.com/language/translate/v2?key=ya29.c.EloOB_lJCN5tAneAm4meLkB-zLLMTcK9Oeen31lo3DeOnoVmf_wjfF0MwDtTYutNoQpOgEU3b-Ri8hbsMoeGyOP6Cipvg9VoRkqb8sWNqOtu7QyWaPNG518JSMQ"
def createDirectoryIfNotExists(fileName):
if not os.path.exists(os.path.dirname(fileName)):
os.makedirs(os.path.dirname(fileName))
def writeToFile(sourceText, translatedText, target):
fileName = "output/" + target + ".lproj/Localizable.strings"
with open(fileName, "a") as myfile:
createDirectoryIfNotExists(fileName)
contentToWrite = "\"" + sourceText + "\" = \"" + translatedText + "\";\n"
myfile.write(contentToWrite)
def translateSourceText(sourceText, target):
translation = translate_client.translate(sourceText,target_language=target)
return translation['translatedText']
#url = translateAPI + "&q=" + sourceText + "&source=en&target=" + target
#r = requests.get(url)
#jsonObject = jsonObject = r.json()
#return jsonObject['data']['translations'][0]['translatedText'].encode('utf-8')
def translateLineInFile(line, target, outputTarget):
matchSource = re.search(r'\"(.*)\"(.*)\"(.*)\"', line)
if matchSource:
stringName = matchSource.group(1)
sourceText = matchSource.group(3)
translation = translateSourceText(sourceText, target)
writeToFile(stringName, translation, outputTarget)
def clearContentsOfFile(target):
fileName = "output/" + target + ".lproj/Localizable.strings"
createDirectoryIfNotExists(fileName)
open(fileName, 'w').close()
def translateFile(translateName, target, outputTarget):
print("Translating for: " + translateName)
clearContentsOfFile(outputTarget)
with open('Localizable.strings', 'r') as stringsFile:
for line in stringsFile:
translateLineInFile(line, translateTarget, outputTarget)
with open('Google_IOS_Country_Codes.txt', 'r') as targetsFile:
for targetLine in targetsFile:
if "#" in targetLine:
continue
targetArray = targetLine.split()
translateName = targetArray[0]
translateTarget = targetArray[1]
outputTarget = targetArray[2]
translateFile(translateName, translateTarget, outputTarget)