mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-05-02 13:42:32 +00:00
Merge branch 'main' into 51-use-webhooks-for-reporting-battery-levels
This commit is contained in:
43
translate.py
43
translate.py
@ -30,11 +30,12 @@
|
||||
# * https://www.crummy.com/software/BeautifulSoup/bs4/doc/#xml
|
||||
#
|
||||
####################################################################################
|
||||
|
||||
from deep_translator import GoogleTranslator
|
||||
from bs4 import BeautifulSoup, Comment
|
||||
import os
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
from bs4 import Comment
|
||||
from deep_translator import GoogleTranslator
|
||||
|
||||
# List of tuples in the form os:
|
||||
# * Garmin IQ language three letter mnemonic,
|
||||
# * Google Translate language mnemonic,
|
||||
@ -75,7 +76,7 @@ languages: list[tuple[str, str, str]] = [
|
||||
("tha", "th", "Thai"),
|
||||
("tur", "tr", "Turkish"),
|
||||
("ukr", "uk", "Ukrainian"),
|
||||
("vie", "vi", "Vietnamese")
|
||||
("vie", "vi", "Vietnamese"),
|
||||
]
|
||||
|
||||
langLength = len(languages)
|
||||
@ -85,42 +86,50 @@ titleIds: list[str] = []
|
||||
|
||||
i = 1
|
||||
with open("./resources/strings/strings.xml", "r") as f:
|
||||
c = f.read().replace('\r', '')
|
||||
c = f.read().replace("\r", "")
|
||||
for l in languages:
|
||||
os.makedirs(f"./resources-{l[0]}/strings/", exist_ok=True)
|
||||
try:
|
||||
with open(f"./resources-{l[0]}/strings/corrections.xml", "r") as r:
|
||||
curr = BeautifulSoup(r.read().replace('\r', ''), features="xml")
|
||||
curr = BeautifulSoup(r.read().replace("\r", ""),
|
||||
features="xml")
|
||||
except FileNotFoundError:
|
||||
curr = BeautifulSoup("", features=["xml"])
|
||||
print(f"{i} of {langLength}: Translating English to {l[2]}")
|
||||
soup = BeautifulSoup(c, features="xml")
|
||||
soup.find(name="strings").insert_before("\n\n")
|
||||
soup.find(name="strings").insert_before(
|
||||
Comment(f"\n Generated by Google Translate: English to {l[2]}\n " +
|
||||
GoogleTranslator(source='en', target=l[1]).translate("Generated by Google Translate from English") +
|
||||
"\n")
|
||||
)
|
||||
Comment(
|
||||
f"\n Generated by Google Translate: English to {l[2]}\n " +
|
||||
GoogleTranslator(source="en", target=l[1]).translate(
|
||||
"Generated by Google Translate from English") + "\n"))
|
||||
soup.find(name="strings").insert_before("\n\n")
|
||||
|
||||
for s in soup.find(name="strings").findAll(name="string"):
|
||||
s.insert_before(" ")
|
||||
if s["id"] in exceptionIds: continue
|
||||
if s["id"] in exceptionIds:
|
||||
continue
|
||||
|
||||
s_curr = curr.find(name="string", attrs={ "id": s["id"] })
|
||||
s_curr = curr.find(name="string", attrs={"id": s["id"]})
|
||||
if s_curr:
|
||||
s.string = s_curr.string
|
||||
else:
|
||||
a = GoogleTranslator(source='en', target=l[1]).translate(s.string)
|
||||
a = GoogleTranslator(source="en",
|
||||
target=l[1]).translate(s.string)
|
||||
if s["id"] in titleIds:
|
||||
s.string = a.title()
|
||||
else:
|
||||
s.string = a
|
||||
for s in soup.find(name="strings").findAll(string=lambda text:isinstance(text, Comment)):
|
||||
for s in soup.find(name="strings").findAll(
|
||||
string=lambda text: isinstance(text, Comment)):
|
||||
s.insert_before(" ")
|
||||
s.replace_with(Comment(" " + GoogleTranslator(source='en', target=l[1]).translate(s) + " "))
|
||||
s.replace_with(
|
||||
Comment(
|
||||
" " +
|
||||
GoogleTranslator(source="en", target=l[1]).translate(s) +
|
||||
" "))
|
||||
|
||||
#print(str(soup))
|
||||
# print(str(soup))
|
||||
with open(f"./resources-{l[0]}/strings/strings.xml", "wb") as w:
|
||||
w.write(soup.encode("utf-8"))
|
||||
w.write(soup.encode("utf-8") + b"\n")
|
||||
i += 1
|
||||
|
Reference in New Issue
Block a user