mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-05-03 06:02:27 +00:00
Restyled by black
This commit is contained in:
29
translate.py
29
translate.py
@ -75,7 +75,7 @@ languages: list[tuple[str, str, str]] = [
|
|||||||
("tha", "th", "Thai"),
|
("tha", "th", "Thai"),
|
||||||
("tur", "tr", "Turkish"),
|
("tur", "tr", "Turkish"),
|
||||||
("ukr", "uk", "Ukrainian"),
|
("ukr", "uk", "Ukrainian"),
|
||||||
("vie", "vi", "Vietnamese")
|
("vie", "vi", "Vietnamese"),
|
||||||
]
|
]
|
||||||
|
|
||||||
langLength = len(languages)
|
langLength = len(languages)
|
||||||
@ -85,22 +85,25 @@ titleIds: list[str] = []
|
|||||||
|
|
||||||
i = 1
|
i = 1
|
||||||
with open("./resources/strings/strings.xml", "r") as f:
|
with open("./resources/strings/strings.xml", "r") as f:
|
||||||
c = f.read().replace('\r', '')
|
c = f.read().replace("\r", "")
|
||||||
for l in languages:
|
for l in languages:
|
||||||
os.makedirs(f"./resources-{l[0]}/strings/", exist_ok=True)
|
os.makedirs(f"./resources-{l[0]}/strings/", exist_ok=True)
|
||||||
try:
|
try:
|
||||||
with open(f"./resources-{l[0]}/strings/corrections.xml", "r") as r:
|
with open(f"./resources-{l[0]}/strings/corrections.xml", "r") as r:
|
||||||
curr = BeautifulSoup(
|
curr = BeautifulSoup(r.read().replace("\r", ""), features="xml")
|
||||||
r.read().replace('\r', ''), features="xml")
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
curr = BeautifulSoup("", features=["xml"])
|
curr = BeautifulSoup("", features=["xml"])
|
||||||
print(f"{i} of {langLength}: Translating English to {l[2]}")
|
print(f"{i} of {langLength}: Translating English to {l[2]}")
|
||||||
soup = BeautifulSoup(c, features="xml")
|
soup = BeautifulSoup(c, features="xml")
|
||||||
soup.find(name="strings").insert_before("\n\n")
|
soup.find(name="strings").insert_before("\n\n")
|
||||||
soup.find(name="strings").insert_before(
|
soup.find(name="strings").insert_before(
|
||||||
Comment(f"\n Generated by Google Translate: English to {l[2]}\n " +
|
Comment(
|
||||||
GoogleTranslator(source='en', target=l[1]).translate("Generated by Google Translate from English") +
|
f"\n Generated by Google Translate: English to {l[2]}\n "
|
||||||
"\n")
|
+ GoogleTranslator(source="en", target=l[1]).translate(
|
||||||
|
"Generated by Google Translate from English"
|
||||||
|
)
|
||||||
|
+ "\n"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
soup.find(name="strings").insert_before("\n\n")
|
soup.find(name="strings").insert_before("\n\n")
|
||||||
|
|
||||||
@ -113,16 +116,20 @@ with open("./resources/strings/strings.xml", "r") as f:
|
|||||||
if s_curr:
|
if s_curr:
|
||||||
s.string = s_curr.string
|
s.string = s_curr.string
|
||||||
else:
|
else:
|
||||||
a = GoogleTranslator(
|
a = GoogleTranslator(source="en", target=l[1]).translate(s.string)
|
||||||
source='en', target=l[1]).translate(s.string)
|
|
||||||
if s["id"] in titleIds:
|
if s["id"] in titleIds:
|
||||||
s.string = a.title()
|
s.string = a.title()
|
||||||
else:
|
else:
|
||||||
s.string = a
|
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.insert_before(" ")
|
||||||
s.replace_with(
|
s.replace_with(
|
||||||
Comment(" " + GoogleTranslator(source='en', target=l[1]).translate(s) + " "))
|
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:
|
with open(f"./resources-{l[0]}/strings/strings.xml", "wb") as w:
|
||||||
|
Reference in New Issue
Block a user