#!/usr/bin/python # translate.py # Copyright 2024 nokoe # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, version 3 of the License only. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # import os import re from string import Template templates = { '{_a_browser_version}': '', '{_a_book}': '', '{_a_codeberg}': '', '{_a_weblate}': '', '{_a_end}': '', '{_b}': '', '{_b_end}': '', '{_developers}': 'nokoe, metamuffin, tpart', } default = "en" def add_languages(file, regex, format, format_default, langs): with open(file) as f: text = f.read() for line in text.splitlines(): m = regex.match(line) if m: w = m.group(1) key = m.group(2).strip() translate = m.group(3).strip() to_substitute = [] keys = list(langs.keys()) keys.remove("en") keys.insert(0, "en") for lang in keys: if translate in langs[ lang] and not langs[lang][translate].strip() == "": translated = langs[lang][translate] for k in templates.keys(): translated = translated.replace(k, templates[k]) line = format.substitute( key=key, value=translated, lang=lang, w=w ) if not lang == default else format_default.substitute( key=key, value=translated, w=w) to_substitute.append(line) text = text.replace(m.string, "\n".join(to_substitute)) new_file = open(f.name.removeprefix("template."), "w") new_file.write(text) langs = {} for i in [ x for x in os.scandir("locale") if x.is_file() and x.name.endswith(".ini") ]: lang = i.name.removesuffix(".ini") translations = {} f = open(i) for line in f: if line.strip() == "" or line.strip() == "[hurrycurry]": continue key, value = line.split("=", 1) translations[key.strip()] = value.strip() langs[lang] = translations add_languages("template.org.metamuffin.hurrycurry.client.desktop", re.compile(r"^(\s*)(.*)\[LANG\]=(.*)$"), Template("$w$key[$lang]=$value"), Template("$w$key=$value"), langs) add_languages("template.org.metamuffin.hurrycurry.client.metainfo.xml", re.compile(r"(\s*)<(.*)\s*xml:lang=\"LANG\"\s*>(.*)"), Template("$w<$key xml:lang=\"$lang\">$value"), Template("$w<$key>$value"), langs)