2022-12-25 18:49:59 +00:00
|
|
|
import re
|
|
|
|
import glob
|
|
|
|
import requests
|
2023-03-14 18:50:13 +00:00
|
|
|
import lxml.etree as ET # builtin library doesn't preserve comments
|
2022-12-25 18:49:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
SETTINGS_PATH = "app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsGeneral.kt"
|
|
|
|
START_MARKER = "/* begin language list */"
|
|
|
|
END_MARKER = "/* end language list */"
|
|
|
|
XML_NAME = "app/src/main/res/values-"
|
2023-02-07 10:04:27 +00:00
|
|
|
ISO_MAP_URL = "https://raw.githubusercontent.com/haliaeetus/iso-639/master/data/iso_639-1.min.json"
|
2022-12-25 18:49:59 +00:00
|
|
|
INDENT = " "*4
|
|
|
|
|
|
|
|
iso_map = requests.get(ISO_MAP_URL, timeout=300).json()
|
|
|
|
|
|
|
|
# Load settings file
|
|
|
|
src = open(SETTINGS_PATH, "r", encoding='utf-8').read()
|
|
|
|
before_src, rest = src.split(START_MARKER)
|
|
|
|
rest, after_src = rest.split(END_MARKER)
|
|
|
|
|
|
|
|
# Load already added langs
|
|
|
|
languages = {}
|
|
|
|
for lang in re.finditer(r'Triple\("(.*)", "(.*)", "(.*)"\)', rest):
|
|
|
|
flag, name, iso = lang.groups()
|
|
|
|
languages[iso] = (flag, name)
|
|
|
|
|
|
|
|
# Add not yet added langs
|
|
|
|
for folder in glob.glob(f"{XML_NAME}*"):
|
|
|
|
iso = folder[len(XML_NAME):]
|
|
|
|
if iso not in languages.keys():
|
2023-02-07 10:04:27 +00:00
|
|
|
entry = iso_map.get(iso.lower(),{'nativeName':iso})
|
|
|
|
languages[iso] = ("", entry['nativeName'].split(',')[0])
|
2022-12-25 18:49:59 +00:00
|
|
|
|
|
|
|
# Create triples
|
|
|
|
triples = []
|
|
|
|
for iso in sorted(languages.keys()):
|
|
|
|
flag, name = languages[iso]
|
|
|
|
triples.append(f'{INDENT}Triple("{flag}", "{name}", "{iso}"),')
|
|
|
|
|
|
|
|
# Update settings file
|
|
|
|
open(SETTINGS_PATH, "w+",encoding='utf-8').write(
|
|
|
|
before_src +
|
|
|
|
START_MARKER +
|
|
|
|
"\n" +
|
|
|
|
"\n".join(triples) +
|
|
|
|
"\n" +
|
|
|
|
END_MARKER +
|
|
|
|
after_src
|
2023-03-14 18:50:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Go through each values.xml file and fix escaped \@string
|
|
|
|
for file in glob.glob(f"{XML_NAME}*/strings.xml"):
|
|
|
|
try:
|
|
|
|
tree = ET.parse(file)
|
|
|
|
for child in tree.getroot():
|
2024-06-24 19:09:41 +00:00
|
|
|
if not child.text:
|
|
|
|
continue
|
2023-03-14 18:50:13 +00:00
|
|
|
if child.text.startswith("\\@string/"):
|
|
|
|
print(f"[{file}] fixing {child.attrib['name']}")
|
|
|
|
child.text = child.text.replace("\\@string/", "@string/")
|
2023-03-19 11:51:54 +00:00
|
|
|
with open(file, 'wb') as fp:
|
|
|
|
fp.write(b'<?xml version="1.0" encoding="utf-8"?>\n')
|
2023-03-18 08:22:07 +00:00
|
|
|
tree.write(fp, encoding="utf-8", method="xml", pretty_print=True, xml_declaration=False)
|
2023-03-14 18:50:13 +00:00
|
|
|
except ET.ParseError as ex:
|
2023-03-18 08:22:07 +00:00
|
|
|
print(f"[{file}] {ex}")
|