forked from OpenVoiceOS/ovos-skill-naptime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
47 lines (38 loc) · 1.46 KB
/
translate.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
import os
from os.path import dirname, join, exists
from ovos_utils.bracket_expansion import expand_options
from googletranslate_neon_plugin import GoogleTranslator
tx = GoogleTranslator()
src_lang = "en-us"
target_langs = ["es-es", "de-de", "fr-fr", "it-it", "pt-pt"]
exts = [".voc", ".dialog", ".intent", ".entity"]
res_folder = join(dirname(dirname(__file__)), "locale")
target_langs = list(set(target_langs + os.listdir(res_folder)))
src_files = {}
for root, dirs, files in os.walk(res_folder):
if src_lang not in root:
continue
for f in files:
if any(f.endswith(e) for e in exts):
src_files[f] = join(root, f)
for lang in target_langs:
os.makedirs(join(res_folder, lang), exist_ok=True)
for name, src in src_files.items():
dst = join(res_folder, lang, name)
if exists(dst):
continue
tx_lines = []
with open(src) as f:
lines = [l for l in f.read().split("\n") if l and not l.startswith("#")]
for l in lines:
expanded = expand_options(l)
for l2 in expanded:
try:
translated = tx.translate(l2, target=lang, source=src_lang)
tx_lines.append(translated)
except:
continue
with open(dst, "w") as f:
f.write(f"# auto translated from {src_lang} to {lang}\n")
for translated in set(tx_lines):
f.write(translated + "\n")