diff options
author | nokoe <nokoe@mailbox.org> | 2024-10-12 15:53:25 +0200 |
---|---|---|
committer | nokoe <nokoe@mailbox.org> | 2024-10-12 16:26:53 +0200 |
commit | 34eb3bdd5e282a77759b5ee7a4cf8493e817ec0c (patch) | |
tree | a33598e1f3a60fb09b8df5fb8bfea926dd621a0e /translate.py | |
parent | e5aa509069e0193e98ba0b5dec06ea7aa9cb7d59 (diff) | |
download | hurrycurry-dist-extra-34eb3bdd5e282a77759b5ee7a4cf8493e817ec0c.tar hurrycurry-dist-extra-34eb3bdd5e282a77759b5ee7a4cf8493e817ec0c.tar.bz2 hurrycurry-dist-extra-34eb3bdd5e282a77759b5ee7a4cf8493e817ec0c.tar.zst |
translate metadata
Diffstat (limited to 'translate.py')
-rw-r--r-- | translate.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/translate.py b/translate.py new file mode 100644 index 0000000..39c5745 --- /dev/null +++ b/translate.py @@ -0,0 +1,80 @@ +#!/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 <https://www.gnu.org/licenses/>. +# + +import os +import re +from string import Template + +templates = { + '{_a_browser_version}': '', + '{_a_book}': '', + '{_a_codeberg}': '', + '{_a_weblate}': '', + '{_a_end}': '', + '{_b}': '<em>', + '{_b_end}': '</em>', + '{_developers}': 'nokoe, metamuffin, tpart', +} + + +def add_languages(file, regex, format, 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 = [] + for lang in langs.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]) + to_substitute.append( + format.substitute(key=key, + value=translated, + lang=lang, + w=w)) + 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) + next(f) + for line in f: + 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"), 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</$key>"), langs) |