-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation.py
204 lines (177 loc) · 4.91 KB
/
translation.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import requests
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
import os
import threading
import queue
import time
t1 = time.perf_counter()
que = queue.Queue()
def storage_queue(f):
def wrapper(*args):
que.put(f(*args))
return wrapper
# Define the source and target languages
source_language = 'en'
target_languages = ['ur']
# "af", # Afrikaans
# "am", # Amharic
# "ar", # Arabic
# "hy", # Armenian
# "az", # Azerbaijani
# "eu", # Basque
# "be", # Belarusian
# "bn", # Bengali
# "bs", # Bosnian
# "bg", # Bulgarian
# "ca", # Catalan
# "ceb", # Cebuano
# "zh-CN", # Chinese (Simplified)
# "zh-TW", # Chinese (Traditional)
# "co", # Corsican
# "hr", # Croatian
# "cs", # Czech
# "da", # Danish
# "nl", # Dutch
# "en", # English
# "eo", # Esperanto
# "et", # Estonian
# "fi", # Finnish
# "fr", # French
# "fy", # Frisian
# "gl", # Galician
# "ka", # Georgian
# "de", # German
# "el", # Greek
# "gu", # Gujarati
# "ht", # Haitian Creole
# "ha", # Hausa
# "haw", # Hawaiian
# "he", # Hebrew
# "hi", # Hindi
# "hmn", # Hmong
# "hu", # Hungarian
# "is", # Icelandic
# "ig", # Igbo
# "id", # Indonesian
# "ga", # Irish
# "it", # Italian
# "ja", # Japanese
# "jv", # Javanese
# "kn", # Kannada
# "kk", # Kazakh
# "km", # Khmer
# "rw", # Kinyarwanda
# "ko", # Korean
# "ku", # Kurdish
# "ky", # Kyrgyz
# "lo", # Lao
# "la", # Latin
# "lv", # Latvian
# "lt", # Lithuanian
# "lb", # Luxembourgish
# "mk", # Macedonian
# "mg", # Malagasy
# "ms", # Malay
# "ml", # Malayalam
# "mt", # Maltese
# "mi", # Maori
# "mr", # Marathi
# "mn", # Mongolian
# "my", # Myanmar (Burmese)
# "ne", # Nepali
# "no", # Norwegian
# "ny", # Nyanja (Chichewa)
# "or", # Odia (Oriya)
# "ps", # Pashto
# "fa", # Persian
# "pl", # Polish
# "pt", # Portuguese
# "pa", # Punjabi
# "ro", # Romanian
# "ru", # Russian
# "sm", # Samoan
# "gd", # Scots Gaelic
# "sr", # Serbian
# "st", # Sesotho
# "sn", # Shona
# "sd", # Sindhi
# "si", # Sinhala (Sinhalese)
# "sk", # Slovak
# "sl", # Slovenian
# "so", # Somali
# "es", # Spanish
# "su", # Sundanese
# "sw", # Swahili
# "sv", # Swedish
# "tg", # Tajik
# "ta", # Tamil
# "tt", # Tatar
# "te", # Telugu
# "th", # Thai
# "ti", # Tigrinya
# "to", # Tonga
# "lua", # Tshiluba
# "tum", # Tumbuka
# "tr", # Turkish
# "tk", # Turkmen
# "tw", # Twi
# "ug", # Uighur
# "uk", # Ukrainian
# "ur", # Urdu
# "uz", # Uzbek
# "vi", # Vietnamese
# "cy", # Welsh
# "wo", # Wolof
# "xh", # Xhosa
# "yi", # Yiddish
# "yo", # Yoruba
# "zu" # Zulu
# ] # Example list of target languages
# Parse the original strings.xml file
tree = ET.parse('src/strings.xml')
root = tree.getroot()
parent_directory = "src"
# Loop through the strings and translate them for each target language
for target_language in target_languages:
newDirectory = f'values-{target_language}'
# if os.path.isdir(os.path.join(parent_directory, newDirectory)):
os.mkdir(os.path.join(parent_directory, newDirectory))
# create a lock object
lock = threading.Lock()
def translate(sourceLang, targetLang, stringInput, i):
string_id = stringInput.attrib['name']
string_value = stringInput.text
url = f'https://translate.googleapis.com/translate_a/single?client=gtx&sl={sourceLang}&tl={targetLang}&dt=t&q={string_value}'
response = requests.get(url)
data = response.json()
translatedText = data[0][0][0]
print(translatedText)
return {
'row_number': i,
'query': stringInput,
'translation': translatedText
}
# acquire the lock before writing to the file
# with lock:
# with open(target_file, 'a') as f:
# f.write(f'\t<string name="{string_id}">{translatedText}</string>\n')
# create the queue to store the results
# Loop through the strings and translate them for each target language
for target_language in target_languages:
translationThreads = []
# Create a new strings.xml file for the target language
target_file = f'{parent_directory}/values-{target_language}/strings.xml'
with open(target_file, 'w') as f:
f.write('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n')
for string in root.iter('string'):
thread = threading.Thread(target=translate, args=(source_language, target_language, string))
thread.start()
translationThreads.append(thread)
# wait for all threads to finish
for thread in translationThreads:
thread.join()
# Close the resources tag in the target file
with open(target_file, 'a') as f:
f.write('</resources>')
print(time.perf_counter() - t1)