-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
173 lines (135 loc) · 5.86 KB
/
main.py
File metadata and controls
173 lines (135 loc) · 5.86 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
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
import json
import requests
from config import DEEPL_API_KEY, SUPPORTED_LANGUAGES, NON_TRANSLATABLE_LANGUAGES
def load_file(filename):
"""Load a JSON file and return its content"""
with open(filename, 'r', encoding='utf-8') as f:
content = json.load(f)
return content
def save_file(filename, content):
"""Save content to a JSON file"""
with open(filename, 'w', encoding='utf-8') as f:
json.dump(content, f, indent=2, ensure_ascii=False)
def translate_text_deepl(text, target_language):
"""Translate text using DeepL API"""
url = "https://api-free.deepl.com/v2/translate"
params = {
'auth_key': DEEPL_API_KEY,
'text': text,
'source_lang': 'ES',
'target_lang': target_language
}
response = requests.post(url, data=params)
result = response.json()
return result['translations'][0]['text']
def compare_keys(file1, file2):
"""Compare keys between two files"""
content1 = load_file(file1)
content2 = load_file(file2)
keys1 = set(content1.keys())
keys2 = set(content2.keys())
if keys1 == keys2:
print("The keys are identical.")
return
print(f"Keys in {file1} but not in {file2}: {keys1 - keys2}")
print("Do you want to add the missing keys? (y/n): ")
confirmation = input().lower()
if confirmation == 'y':
add_keys(content1, content2, file2)
else:
print("Missing keys were not added.")
def remove_different_keys(original_file, compare_file):
"""Remove keys that exist in compare_file but not in original_file"""
original_content = load_file(original_file)
compare_content = load_file(compare_file)
original_keys = set(original_content.keys())
compare_keys = set(compare_content.keys())
keys_to_remove = compare_keys - original_keys
if not keys_to_remove:
print("There are no keys in the compare file that don't exist in the original file.")
return
for key in keys_to_remove:
confirmation = input(f"Do you want to remove the key '{key}' from {compare_file}? (y/n): ").lower()
if confirmation == 'y':
if key in compare_content:
print(f"Removing key '{key}' from {compare_file}")
del compare_content[key]
else:
print(f"Key '{key}' was not removed.")
save_file(compare_file, compare_content)
def translate_values(source_file, target_file, target_language):
"""Translate values from source_file to target_file"""
source_content = load_file(source_file)
target_content = load_file(target_file)
# Translate values from source_file to target_file
for key, value in source_content.items():
if key != "@@locale" and key in target_content:
target_content[key] = translate_text_deepl(value, target_language)
save_file(target_file, target_content)
def get_filename(language_suffix):
"""Generate filename for a given language"""
return f"intl_{language_suffix}.arb"
def add_keys(content1, content2, file2):
"""Add missing keys from content1 to content2"""
keys1 = set(content1.keys())
keys2 = set(content2.keys())
for key in keys1 - keys2:
content2[key] = content1[key]
sorted(content2.keys())
save_file(file2, content2)
print("Missing keys were added.")
def compare_and_update_key_order(original_file, compare_file):
"""Compare and update the order of keys in compare_file to match original_file"""
original_content = load_file(original_file)
compare_content = load_file(compare_file)
original_keys = list(original_content.keys())
compare_keys = list(compare_content.keys())
if original_keys == compare_keys:
print("The order of keys is identical in both files.")
else:
print("The order of keys is not identical in both files. Updating the order in the compare file.")
compare_content = {key: compare_content[key] for key in original_keys}
save_file(compare_file, compare_content)
print(f"The compare file now has the same key order as the original file.")
def main():
"""Main function to run the application"""
main_language = "es"
while True:
compare_language = input("Enter the language of the file to compare (fr): ")
if main_language == compare_language:
print("Error: Languages cannot be the same.")
continue
if compare_language in NON_TRANSLATABLE_LANGUAGES:
print(f"Warning: The language '{compare_language}' cannot be translated.")
confirmation = input("Do you want to continue? (y/n): ").lower()
if confirmation != 'y':
continue
if compare_language not in SUPPORTED_LANGUAGES:
print(f"Error: The language suffix '{compare_language}' is not valid. Please choose one of: {', '.join(SUPPORTED_LANGUAGES)}")
continue
main_file = get_filename(main_language)
compare_file = get_filename(compare_language)
while True:
print("\n1. Compare keys")
print("2. Compare and update key order")
print("3. Remove different keys")
print("4. Translate values")
print("5. Change language")
print("6. Exit")
option = input("Select an option (1/2/3/4/5/6): ")
if option == "1":
compare_keys(main_file, compare_file)
elif option == "2":
compare_and_update_key_order(main_file, compare_file)
elif option == "3":
remove_different_keys(main_file, compare_file)
elif option == "4":
translate_values(main_file, compare_file, compare_language)
elif option == "5":
break
elif option == "6":
exit()
else:
print("Invalid option. Please select a valid option.")
if __name__ == "__main__":
main()