mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-07-30 16:38:31 +00:00
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: __JosephAbbey <me@josephabbey.dev>
154 lines
5.6 KiB
Python
154 lines
5.6 KiB
Python
####################################################################################
|
|
#
|
|
# Distributed under MIT Licence
|
|
# See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE.
|
|
#
|
|
####################################################################################
|
|
#
|
|
# GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely
|
|
# tested on a Venu 2 device. The source code is provided at:
|
|
# https://github.com/house-of-abbey/GarminHomeAssistant.
|
|
#
|
|
# J D Abbey & P A Abbey, 28 December 2022
|
|
#
|
|
#
|
|
# Description:
|
|
#
|
|
# Python script to automatically translate the strings.xml file to each supported
|
|
# language using Google Translate.
|
|
#
|
|
# Python installation:
|
|
# pip install beautifulsoup4
|
|
# pip install deep-translator
|
|
# NB. For XML formatting:
|
|
# pip install lxml
|
|
#
|
|
# References:
|
|
# * https://www.crummy.com/software/BeautifulSoup/bs4/doc/
|
|
# * https://realpython.com/beautiful-soup-web-scraper-python/
|
|
# * https://www.crummy.com/software/BeautifulSoup/bs4/doc/#parsing-xml
|
|
# * https://www.crummy.com/software/BeautifulSoup/bs4/doc/#xml
|
|
#
|
|
####################################################################################
|
|
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,
|
|
# * Language familiar name (mainly for reference)
|
|
languages: list[tuple[str, str, str]] = [
|
|
("ara", "ar", "Arabic"),
|
|
("bul", "bg", "Bulgarian"),
|
|
("zhs", "zh-CN", "Chinese (Simplified)"),
|
|
("zht", "zh-TW", "Chinese (Traditional)"),
|
|
("hrv", "hr", "Croatian"),
|
|
("ces", "cs", "Czech"),
|
|
("dan", "da", "Danish"),
|
|
("dut", "nl", "Dutch"),
|
|
("deu", "de", "German"),
|
|
("gre", "el", "Greek"),
|
|
# ("eng", "en", "English"),
|
|
("est", "et", "Estonian"),
|
|
("fin", "fi", "Finnish"),
|
|
("fre", "fr", "French"),
|
|
("heb", "iw", "Hebrew"),
|
|
("hun", "hu", "Hungarian"),
|
|
("ind", "id", "Indonesian"),
|
|
("ita", "it", "Italian"),
|
|
("jpn", "ja", "Japanese"),
|
|
("kor", "ko", "Korean"),
|
|
("lav", "lv", "Latvian"),
|
|
("lit", "lt", "Lithuanian"),
|
|
("zsm", "ms", "Standard (Bahasa) Malay"),
|
|
("nob", "no", "Norwegian"),
|
|
("pol", "pl", "Polish"),
|
|
("por", "pt", "Portuguese"),
|
|
("ron", "ro", "Romanian"),
|
|
# ("rus", "ru", "Russian"),
|
|
("slo", "sk", "Slovak"),
|
|
("slv", "sl", "Slovenian"),
|
|
("spa", "es", "Spanish"),
|
|
("swe", "sv", "Swedish"),
|
|
("tha", "th", "Thai"),
|
|
("tur", "tr", "Turkish"),
|
|
("ukr", "uk", "Ukrainian"),
|
|
("vie", "vi", "Vietnamese"),
|
|
]
|
|
|
|
langLength = len(languages)
|
|
|
|
exceptionIds: list[str] = ["AppName", "AppVersionTitle"]
|
|
titleIds: list[str] = []
|
|
|
|
# def merge(curr: BeautifulSoup, prev: BeautifulSoup) -> BeautifulSoup:
|
|
# """
|
|
# Merge the current strings.xml with the previous one, overwriting
|
|
# the previous strings with the current ones if they exist.
|
|
# """
|
|
# out = prev.__copy__()
|
|
# for s in curr.find(name="strings").findAll(name="string"):
|
|
# s_prev = out.find(name="string", attrs={"id": s["id"]})
|
|
# if s_prev:
|
|
# s_prev.string = s.string
|
|
# else:
|
|
# out.find(name="strings").append(s)
|
|
# return out
|
|
|
|
i = 1
|
|
with open("./resources/strings/strings.xml", "r") as f:
|
|
c = f.read().replace("\r", "")
|
|
for l in languages:
|
|
os.makedirs(f"./resources-{l[0]}/strings/", exist_ok=True)
|
|
# Old translations will not be automatically updated/removed, use removeTranslations.py
|
|
try:
|
|
with open(f"./resources-{l[0]}/strings/strings.xml", "r", encoding="utf-8") as r:
|
|
prev = BeautifulSoup(r.read().replace("\r", ""), features="xml")
|
|
except FileNotFoundError:
|
|
prev = BeautifulSoup("", features="xml")
|
|
try:
|
|
with open(f"./resources-{l[0]}/strings/corrections.xml", "r", encoding="utf-8") as r:
|
|
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")
|
|
translator = GoogleTranslator(source="en", target=l[1])
|
|
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 " +
|
|
translator.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
|
|
|
|
s_curr = curr.find(name="string", attrs={"id": s["id"]})
|
|
if s_curr:
|
|
s.string = s_curr.string
|
|
else:
|
|
s_prev = prev.find(name="string", attrs={"id": s["id"]})
|
|
if s_prev:
|
|
s.string = s_prev.string
|
|
else:
|
|
a = translator.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)):
|
|
s.insert_before(" ")
|
|
s.replace_with(Comment(" " + translator.translate(s) + " "))
|
|
|
|
# print(str(soup))
|
|
with open(f"./resources-{l[0]}/strings/strings.xml", "wb") as w:
|
|
w.write(soup.encode("utf-8") + b"\n")
|
|
i += 1
|