forked from MycroftAI/skill-hello-world
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsync_translations.py
More file actions
64 lines (58 loc) · 2.86 KB
/
sync_translations.py
File metadata and controls
64 lines (58 loc) · 2.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
import json
from os.path import dirname
import os
from ovos_utils.bracket_expansion import expand_template
from ovos_utils.list_utils import flatten_list, deduplicate_list
locale = f"{dirname(dirname(__file__))}/ovos_skill_hello_world/locale"
tx = f"{dirname(dirname(__file__))}/ovos_skill_hello_world/translations"
for lang in os.listdir(tx):
intents = f"{tx}/{lang}/intents.json"
dialogs = f"{tx}/{lang}/dialogs.json"
vocs = f"{tx}/{lang}/vocabs.json"
regexes = f"{tx}/{lang}/regexes.json"
if os.path.isfile(intents):
with open(intents) as f:
data = json.load(f)
for fid, samples in data.items():
if samples:
samples = deduplicate_list(flatten_list([expand_template(s.strip())
for s in samples
if s and s.strip() != "[UNUSED]"])) # s may be None
if fid.startswith("/"):
p = f"{locale}/{lang.lower()}{fid}"
else:
p = f"{locale}/{lang.lower()}/{fid}"
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(f"{locale}/{lang.lower()}/{fid}", "w") as f:
f.write("\n".join(sorted(samples)))
if os.path.isfile(dialogs):
with open(dialogs) as f:
data = json.load(f)
for fid, samples in data.items():
print(fid, samples)
if samples:
samples = deduplicate_list(flatten_list([expand_template(s.strip())
for s in samples
if s and s.strip() != "[UNUSED]"])) # s may be None
if fid.startswith("/"):
p = f"{locale}/{lang.lower()}{fid}"
else:
p = f"{locale}/{lang.lower()}/{fid}"
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(f"{locale}/{lang.lower()}/{fid}", "w") as f:
f.write("\n".join(sorted(samples)))
if os.path.isfile(vocs):
with open(vocs) as f:
data = json.load(f)
for fid, samples in data.items():
if samples:
samples = deduplicate_list(flatten_list([expand_template(s.strip())
for s in samples
if s and s.strip() != "[UNUSED]"])) # s may be None
if fid.startswith("/"):
p = f"{locale}/{lang.lower()}{fid}"
else:
p = f"{locale}/{lang.lower()}/{fid}"
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(f"{locale}/{lang.lower()}/{fid}", "w") as f:
f.write("\n".join(sorted(samples)))