Merge pull request #63 from house-of-abbey/restyled/59-minor-fix-for-translation-script

This commit is contained in:
Joseph Abbey
2024-01-11 18:17:38 +00:00
committed by GitHub

View File

@ -30,52 +30,53 @@
# * https://www.crummy.com/software/BeautifulSoup/bs4/doc/#xml # * https://www.crummy.com/software/BeautifulSoup/bs4/doc/#xml
# #
#################################################################################### ####################################################################################
from deep_translator import GoogleTranslator
from bs4 import BeautifulSoup, Comment
import os import os
from bs4 import BeautifulSoup
from bs4 import Comment
from deep_translator import GoogleTranslator
# List of tuples in the form os: # List of tuples in the form os:
# * Garmin IQ language three letter mnemonic, # * Garmin IQ language three letter mnemonic,
# * Google Translate language mnemonic, # * Google Translate language mnemonic,
# * Language familiar name (mainly for reference) # * Language familiar name (mainly for reference)
languages: list[tuple[str, str, str]] = [ languages: list[tuple[str, str, str]] = [
("ara", "ar", "Arabic"), ("ara", "ar", "Arabic"),
("bul", "bg", "Bulgarian"), ("bul", "bg", "Bulgarian"),
("zhs", "zh-CN", "Chinese (Simplified)"), ("zhs", "zh-CN", "Chinese (Simplified)"),
("zht", "zh-TW", "Chinese (Traditional)"), ("zht", "zh-TW", "Chinese (Traditional)"),
("hrv", "hr", "Croatian"), ("hrv", "hr", "Croatian"),
("ces", "cs", "Czech"), ("ces", "cs", "Czech"),
("dan", "da", "Danish"), ("dan", "da", "Danish"),
("dut", "nl", "Dutch"), ("dut", "nl", "Dutch"),
("deu", "de", "German"), ("deu", "de", "German"),
("gre", "el", "Greek"), ("gre", "el", "Greek"),
# ("eng", "en", "English"), # ("eng", "en", "English"),
("est", "et", "Estonian"), ("est", "et", "Estonian"),
("fin", "fi", "Finnish"), ("fin", "fi", "Finnish"),
("fre", "fr", "French"), ("fre", "fr", "French"),
("heb", "iw", "Hebrew"), ("heb", "iw", "Hebrew"),
("hun", "hu", "Hungarian"), ("hun", "hu", "Hungarian"),
("ind", "id", "Indonesian"), ("ind", "id", "Indonesian"),
("ita", "it", "Italian"), ("ita", "it", "Italian"),
("jpn", "ja", "Japanese"), ("jpn", "ja", "Japanese"),
("kor", "ko", "Korean"), ("kor", "ko", "Korean"),
("lav", "lv", "Latvian"), ("lav", "lv", "Latvian"),
("lit", "lt", "Lithuanian"), ("lit", "lt", "Lithuanian"),
("zsm", "ms", "Standard (Bahasa) Malay"), ("zsm", "ms", "Standard (Bahasa) Malay"),
("nob", "no", "Norwegian"), ("nob", "no", "Norwegian"),
("pol", "pl", "Polish"), ("pol", "pl", "Polish"),
("por", "pt", "Portuguese"), ("por", "pt", "Portuguese"),
("ron", "ro", "Romanian"), ("ron", "ro", "Romanian"),
# ("rus", "ru", "Russian"), # ("rus", "ru", "Russian"),
("slo", "sk", "Slovak"), ("slo", "sk", "Slovak"),
("slv", "sl", "Slovenian"), ("slv", "sl", "Slovenian"),
("spa", "es", "Spanish"), ("spa", "es", "Spanish"),
("swe", "sv", "Swedish"), ("swe", "sv", "Swedish"),
("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,42 +86,50 @@ 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(r.read().replace('\r', ''), features="xml") curr = BeautifulSoup(r.read().replace("\r", ""),
except FileNotFoundError: features="xml")
curr = BeautifulSoup("", features=["xml"]) except FileNotFoundError:
print(f"{i} of {langLength}: Translating English to {l[2]}") curr = BeautifulSoup("", features=["xml"])
soup = BeautifulSoup(c, features="xml") print(f"{i} of {langLength}: Translating English to {l[2]}")
soup.find(name="strings").insert_before("\n\n") soup = BeautifulSoup(c, features="xml")
soup.find(name="strings").insert_before( soup.find(name="strings").insert_before("\n\n")
Comment(f"\n Generated by Google Translate: English to {l[2]}\n " + soup.find(name="strings").insert_before(
GoogleTranslator(source='en', target=l[1]).translate("Generated by Google Translate from English") + Comment(
"\n") f"\n Generated by Google Translate: English to {l[2]}\n " +
) GoogleTranslator(source="en", target=l[1]).translate(
soup.find(name="strings").insert_before("\n\n") "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"): for s in soup.find(name="strings").findAll(name="string"):
s.insert_before(" ") 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: if s_curr:
s.string = s_curr.string s.string = s_curr.string
else: else:
a = GoogleTranslator(source='en', target=l[1]).translate(s.string) a = GoogleTranslator(source="en",
if s["id"] in titleIds: target=l[1]).translate(s.string)
s.string = a.title() if s["id"] in titleIds:
else: s.string = a.title()
s.string = a else:
for s in soup.find(name="strings").findAll(string=lambda text:isinstance(text, Comment)): s.string = a
s.insert_before(" ") for s in soup.find(name="strings").findAll(
s.replace_with(Comment(" " + GoogleTranslator(source='en', target=l[1]).translate(s) + " ")) string=lambda text: isinstance(text, Comment)):
s.insert_before(" ")
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: with open(f"./resources-{l[0]}/strings/strings.xml", "wb") as w:
w.write(soup.encode("utf-8") + b"\n") w.write(soup.encode("utf-8") + b"\n")
i += 1 i += 1