mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-08-01 17:38:40 +00:00
Merge branch 'main' into 259-code-tidy
This commit is contained in:
94
removeTranslations.py
Normal file
94
removeTranslations.py
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
####################################################################################
|
||||||
|
#
|
||||||
|
# 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, 24 July 2025
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Description:
|
||||||
|
#
|
||||||
|
# Python script to remove all the translations of a specific id from the XML files.
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# python removeTranslations.py <id>
|
||||||
|
#
|
||||||
|
# Python installation:
|
||||||
|
# pip install beautifulsoup4
|
||||||
|
# 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 sys
|
||||||
|
# from bs4 import BeautifulSoup
|
||||||
|
import os
|
||||||
|
|
||||||
|
def remove_translations(file_path: str, translation_id: str) -> None:
|
||||||
|
"""
|
||||||
|
Remove all translations of a specific id from the XML file.
|
||||||
|
|
||||||
|
:param file_path: Path to the XML file.
|
||||||
|
:param translation_id: The id of the translation to remove.
|
||||||
|
"""
|
||||||
|
# Breaks the formatting
|
||||||
|
# with open(file_path, "r", encoding="utf-8") as file:
|
||||||
|
# soup = BeautifulSoup(file, features="xml")
|
||||||
|
|
||||||
|
# # Find all string elements with the specified id
|
||||||
|
# strings_to_remove = soup.find_all("string", {"id": translation_id})
|
||||||
|
|
||||||
|
# for string in strings_to_remove:
|
||||||
|
# string.decompose() # Remove the string element
|
||||||
|
|
||||||
|
# # Write the modified XML back to the file
|
||||||
|
# with open(file_path, "wb") as file:
|
||||||
|
# file.write(soup.encode("utf-8") + b"\n")
|
||||||
|
|
||||||
|
# Use standard string replace instead
|
||||||
|
with open(file_path, "r", encoding="utf-8") as file:
|
||||||
|
content = file.read()
|
||||||
|
|
||||||
|
new = ""
|
||||||
|
for line in content.splitlines():
|
||||||
|
if not f'id="{translation_id}"' in line:
|
||||||
|
new += line + "\n"
|
||||||
|
|
||||||
|
with open(file_path, "w", encoding="utf-8") as file:
|
||||||
|
file.write(new)
|
||||||
|
|
||||||
|
def main(translation_id: str) -> None:
|
||||||
|
"""
|
||||||
|
Main function to process all XML files.
|
||||||
|
|
||||||
|
:param translation_id: The id of the translation to remove.
|
||||||
|
"""
|
||||||
|
xml_files = []
|
||||||
|
for directory in os.listdir("."):
|
||||||
|
if os.path.isdir(directory) and "resources-" in directory:
|
||||||
|
xml_file_path = os.path.join(directory, "strings", "strings.xml")
|
||||||
|
if os.path.exists(xml_file_path):
|
||||||
|
xml_files.append(xml_file_path)
|
||||||
|
|
||||||
|
for xml_file in xml_files:
|
||||||
|
print(f"Processing file: {xml_file}")
|
||||||
|
remove_translations(xml_file, translation_id)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print("Usage: python removeTranslations.py <id>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
translation_id = sys.argv[1]
|
||||||
|
main(translation_id)
|
50
translate.py
50
translate.py
@ -1,13 +1,13 @@
|
|||||||
####################################################################################
|
####################################################################################
|
||||||
#
|
#
|
||||||
# Distributed under MIT Licence
|
# Distributed under MIT Licence
|
||||||
# See https://github.com/house-of-abbey/GarminThermoNest/blob/main/LICENSE.
|
# See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE.
|
||||||
#
|
#
|
||||||
####################################################################################
|
####################################################################################
|
||||||
#
|
#
|
||||||
# ThermoNest is a Garmin IQ application written in Monkey C and routinely tested on
|
# GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely
|
||||||
# a Venu 2 device. The source code is provided at:
|
# tested on a Venu 2 device. The source code is provided at:
|
||||||
# https://github.com/house-of-abbey/GarminThermoNest.
|
# https://github.com/house-of-abbey/GarminHomeAssistant.
|
||||||
#
|
#
|
||||||
# J D Abbey & P A Abbey, 28 December 2022
|
# J D Abbey & P A Abbey, 28 December 2022
|
||||||
#
|
#
|
||||||
@ -84,25 +84,44 @@ langLength = len(languages)
|
|||||||
exceptionIds: list[str] = ["AppName", "AppVersionTitle"]
|
exceptionIds: list[str] = ["AppName", "AppVersionTitle"]
|
||||||
titleIds: list[str] = []
|
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
|
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)
|
||||||
|
# Old translations will not be automatically updated/removed, use removeTranslations.py
|
||||||
try:
|
try:
|
||||||
with open(f"./resources-{l[0]}/strings/corrections.xml", "r") as r:
|
with open(f"./resources-{l[0]}/strings/strings.xml", "r", encoding="utf-8") as r:
|
||||||
curr = BeautifulSoup(r.read().replace("\r", ""),
|
prev = BeautifulSoup(r.read().replace("\r", ""), features="xml")
|
||||||
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:
|
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")
|
||||||
|
translator = GoogleTranslator(source="en", target=l[1])
|
||||||
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(
|
Comment(
|
||||||
f"\n Generated by Google Translate: English to {l[2]}\n " +
|
f"\n Generated by Google Translate: English to {l[2]}\n " +
|
||||||
GoogleTranslator(source="en", target=l[1]).translate(
|
translator.translate("Generated by Google Translate from English") + "\n"))
|
||||||
"Generated by Google Translate from English") + "\n"))
|
|
||||||
soup.find(name="strings").insert_before("\n\n")
|
soup.find(name="strings").insert_before("\n\n")
|
||||||
|
|
||||||
for s in soup.find(name="strings").findAll(name="string"):
|
for s in soup.find(name="strings").findAll(name="string"):
|
||||||
@ -114,8 +133,11 @@ 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(source="en",
|
s_prev = prev.find(name="string", attrs={"id": s["id"]})
|
||||||
target=l[1]).translate(s.string)
|
if s_prev:
|
||||||
|
s.string = s_prev.string
|
||||||
|
else:
|
||||||
|
a = translator.translate(s.string)
|
||||||
if s["id"] in titleIds:
|
if s["id"] in titleIds:
|
||||||
s.string = a.title()
|
s.string = a.title()
|
||||||
else:
|
else:
|
||||||
@ -123,11 +145,7 @@ with open("./resources/strings/strings.xml", "r") as f:
|
|||||||
for s in soup.find(name="strings").findAll(
|
for s in soup.find(name="strings").findAll(
|
||||||
string=lambda text: isinstance(text, Comment)):
|
string=lambda text: isinstance(text, Comment)):
|
||||||
s.insert_before(" ")
|
s.insert_before(" ")
|
||||||
s.replace_with(
|
s.replace_with(Comment(" " + translator.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