GarminHomeAssistant/translate.py
Philip Abbey 816b0dc890 Internationalisation & Multi-watch support
Copied two methods from GarminThermoNest to translate languages and manage different sized icons for the various sizes of watch. Also copied ErrorView for smaller wrapped text for error messages.
2023-11-02 21:32:42 +00:00

116 lines
3.9 KiB
Python

####################################################################################
#
# Distributed under MIT Licence
# See https://github.com/house-of-abbey/GarminThermoNest/blob/main/LICENSE.
#
####################################################################################
#
# ThermoNest 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/GarminThermoNest.
#
# 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 BeautifulSoup
# 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
#
####################################################################################
from deep_translator import GoogleTranslator
from bs4 import BeautifulSoup, Comment
import os
# 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] = ["setMode", "tapIcon"]
i = 1
with open("./resources/strings/strings.xml") as f:
c = f.read().replace('\r', '')
for l in languages:
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")
)
soup.find(name="strings").insert_before("\n\n")
for s in soup.find(name="strings").findAll(name="string"):
s.insert_before(" ")
if s["id"] not in exceptionIds:
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(text=lambda text:isinstance(text, Comment)):
s.insert_before(" ")
s.replace_with(Comment(" " + GoogleTranslator(source='en', target=l[1]).translate(s) + " "))
#print(str(soup))
os.makedirs(f"./resources-{l[0]}/strings/", exist_ok=True)
with open(f"./resources-{l[0]}/strings/strings.xml", "wb") as w:
w.write(soup.encode("utf-8"))
i += 1