diff --git a/.github/workflows/update-locales.yml b/.github/workflows/update-locales.yml index 6a605f5f64..bfbc330b72 100644 --- a/.github/workflows/update-locales.yml +++ b/.github/workflows/update-locales.yml @@ -5,6 +5,7 @@ on: branches: - main paths: + - app/src/main/res/values-*/strings.xml - app/src/main/res/values/settings.xml permissions: actions: none @@ -25,6 +26,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4.1.0 + - name: Add new locales + run: .scripts/new-locales.py - name: Update locales run: .scripts/locales.py - name: Create Pull Request diff --git a/.scripts/new-locales.py b/.scripts/new-locales.py new file mode 100755 index 0000000000..757011276e --- /dev/null +++ b/.scripts/new-locales.py @@ -0,0 +1,120 @@ +#!/usr/bin/python3 + +import glob +import re + +from typing import Iterator, List, Tuple + +import requests + +MIN_PERCENT = 90 +NOT_LANGS = ("night", "w600dp") +REPLACE_CODES = { + "el": "el-rGR", + "id": "in-rID", + "ro": "ro-rRO", + "zh_Hans": "zh-rCN", + "zh_Hant": "zh-rTW", +} +STATS_URL = "https://hosted.weblate.org/api/components/catima/catima/statistics/" + + +def get_weblate_langs() -> List[Tuple[str, int]]: + r = requests.get(STATS_URL, timeout=5) + r.raise_for_status() + results = [] + for lang in r.json()["results"]: + if lang["code"] != "en": + code = REPLACE_CODES.get(lang["code"], lang["code"]).replace("_", "-r") + results.append((code, round(lang["translated_percent"]))) + return sorted(results) + + +def get_dir_langs() -> List[str]: + results = [] + for d in glob.glob("app/src/main/res/values-*"): + code = d.split("-", 1)[1] + if code not in NOT_LANGS: + results.append(code) + return sorted(results) + + +def get_xml_langs() -> List[Tuple[str, bool]]: + results = [] + in_section = False + with open("app/src/main/res/values/settings.xml") as fh: + for line in fh: + if not in_section and 'name="locale_values"' in line: + in_section = True + elif in_section: + if "string-array" in line: + break + disabled = "\n" + else: + yield f" {lang}\n" + + +def main() -> None: + web_langs = get_weblate_langs() + dir_langs = get_dir_langs() + xml_langs = get_xml_langs() + + web_codes = set(code for code, _ in web_langs) + dir_codes = set(dir_langs) + xml_codes = set(code for code, _ in xml_langs) + + if diff := web_codes - dir_codes: + print(f"WARNING: Weblate codes w/o dir: {diff}") + if diff := xml_codes - dir_codes: + print(f"WARNING: XML codes w/o dir: {diff}") + + percentages = dict(web_langs) + all_langs = xml_langs[:] + + # add new langs as disabled + for code in dir_codes - xml_codes: + all_langs.append((code, True)) + + # enable disabled langs if they are at least MIN_PERCENT translated now + updated_langs = sorted( + (code, percentages[code] < MIN_PERCENT if disabled else disabled) + for code, disabled in all_langs + ) + + if updated_langs != xml_langs: + print("Updating...") + update_xml_langs(updated_langs) + + +if __name__ == "__main__": + main()