-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
81 lines (65 loc) · 2.43 KB
/
main.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
import concurrent.futures
import json
import os
import subprocess
from deep_translator import GoogleTranslator
input_file = 'input/en_us.json'
languages = {
# "source_language" : "file_name"
'zh-CN': 'zh_cn',
'zh-TW': 'zh_tw',
'fr': 'fr_fr',
'de': 'de_de',
'it': 'it_it',
'ja': 'ja_jp',
'ko': 'ko_kr',
'pt': 'pt_br',
'ru': 'ru_ru',
'uk': 'uk_ua',
'es': 'es_es',
'pl': 'pl_pl',
'tr': 'tr_tr',
}
def translate_string(lang_code, string):
try:
return GoogleTranslator(source='en', target=lang_code).translate(string)
except Exception as e:
print(f"Error occurred during translation: {e}")
return string # return original string if translation fails
def translate_file(file_path):
try:
# Attempt to open the file
with open(file_path, 'r', encoding='utf-8') as file:
content = json.load(file)
except FileNotFoundError:
print(f"File {file_path} not found.")
return
except Exception as e:
print(f"An error occurred while reading the file: {e}")
return
# Loop over the languages
for lang, lang_code in languages.items():
print(f"Starting translation for language: {lang_code}")
# Create a new dictionary to hold the translated content
translated_content = {}
# Create a ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor() as executor:
# Loop over each key-value pair in the content
for key, value in content.items():
# Translate the value and add it to the translated_content dictionary
future = executor.submit(translate_string, lang, value)
translated_content[key] = future.result()
print(f"Finished translation for language: {lang_code}") # Print when translation ends
# Check if output directory exists, if not, create it
if not os.path.exists('output'):
os.mkdir('output')
try:
# Write the translated content to a new file in the output folder
with open(f'output/{lang_code}.json', 'w', encoding='utf-8') as file:
json.dump(translated_content, file, ensure_ascii=False, indent=4)
except Exception as e:
print(f"Error occurred while writing file: {e}")
print('Done!')
if __name__ == '__main__':
subprocess.call(['python3', 'cleanOutputDir.py'])
translate_file(input_file)