-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_update.py
More file actions
executable file
·69 lines (55 loc) · 1.83 KB
/
_update.py
File metadata and controls
executable file
·69 lines (55 loc) · 1.83 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
#!/usr/bin/env python3
"""
I have a translations JSON file. Please add entries for:
ar el es he id ja uk nl pt ru zh-TW zh-CN de fr hi it ko pl ro sv
{
"en": {
"translation": {
"example": {
"hello": "Hello"
}
}
},
"uk": {
"translation": {
"example": {
"hello": "Привіт"
}
}
},
...
}
"""
import os, json
def deep_update(original, updates):
for key, value in updates.items():
if isinstance(value, dict):
original[key] = deep_update(original.get(key, {}), value)
else:
original[key] = value
return original
def update_translations(input_file, target_folder):
# Read the input file with new translations
with open(input_file, 'r', encoding='utf-8') as f:
new_translations = json.load(f)
# Iterate over each language in the input file
for lang, data in new_translations.items():
target_file = os.path.join(target_folder, f"{lang}.json")
# Check if the target file exists
if os.path.exists(target_file):
# Read the target file
with open(target_file, 'r', encoding='utf-8') as f:
target_data = json.load(f)
# Update the target file with new translations
target_data = deep_update(target_data, data)
# Write the updated translations back to the target file
with open(target_file, 'w', encoding='utf-8') as f:
json.dump(target_data, f, ensure_ascii=False, indent=4)
else:
print(f"Target file {target_file} does not exist.")
if __name__ == "__main__":
# Path to the input file containing new translations
input_file = '_new.json'
# Folder containing the target JSON files
target_folder = '.'
update_translations(input_file, target_folder)