-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic-model-translator.py
More file actions
104 lines (86 loc) · 3.85 KB
/
Copy pathsemantic-model-translator.py
File metadata and controls
104 lines (86 loc) · 3.85 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
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
import os
import sys
import requests
from bs4 import BeautifulSoup
from xml.etree import ElementTree as ET
import time
from tqdm import tqdm
# Function to remove namespace in the passed document in place.
def remove_namespace(doc):
for elem in doc.iter():
if '}' in elem.tag:
elem.tag = elem.tag.split('}', 1)[1] # Removes namespace
# Function to scrape the Google translate mobile website
def scrape_google_translate(text, source_language, target_language):
# Google Translate URL
url = "https://translate.google.com/m"
# Prepare the payload with the query parameters
payload = {
'hl': target_language,
'q': text,
'sl': source_language,
'tl': target_language,
}
# Make the request to Google Translate with rate limiting
while True:
response = requests.get(url, params=payload)
if response.status_code == 200:
break
elif response.status_code == 429:
print("Rate limit exceeded. Waiting for 5 seconds...")
time.sleep(5)
else:
response.raise_for_status()
# Parse the response with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the div with the 'result-container' class
swap_container = soup.find('div', class_='result-container')
# Extract the text or other desired parts from the swap_container
if swap_container:
translated_text = swap_container.get_text() # or any other method depending on what you need
else:
translated_text = "The result-container div was not found."
return translated_text
# Function to process an individual file
def process_file(file_path, file_progress_bar):
# Parse the XML file
tree = ET.parse(file_path)
root = tree.getroot()
remove_namespace(root) # Call the function to remove namespaces
# Count the total number of Localization elements in the file
total_elements = sum(1 for _ in root.iter('Localization'))
# Loop through each Localization element
for i, localization in enumerate(root.iter('Localization')):
culture = localization.get('Culture')
if culture not in ['en-GB', 'en-US']: # Skip default English localizations
source_lang = 'en' # Assuming the source language is English
target_lang = culture[:2] # Assuming the target language is the first two letters of the culture code
try:
# Attempt to translate the text using the new scrape function
translated_text = scrape_google_translate(localization.text, source_lang, target_lang)
localization.text = translated_text
except AttributeError as e: # Catching attribute errors specifically
print(f"Translation service error for {culture}: {e}")
except Exception as e: # Catching all other exceptions
print(f"General error translating {culture}: {e}")
# Update element-level progress bar
file_progress_bar.update(1)
# Write back to file
ET.register_namespace('', '') # Register an empty namespace
tree.write(file_path, encoding='utf-8', xml_declaration=True)
# Main function to process files within a directory
def main(root_directory):
for subdir, dirs, files in os.walk(root_directory):
for file in files:
if file.endswith(".sm"):
file_path = os.path.join(subdir, file)
# Initialize file-level progress bar
file_progress_bar = tqdm(desc=f"Processing {file}", unit=" element", total=0)
process_file(file_path, file_progress_bar)
# Close the file-level progress bar
file_progress_bar.close()
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <root_directory>")
sys.exit(1)
main(sys.argv[1])