summaryrefslogtreecommitdiff
path: root/translate.py
blob: ccfa7d21f1193e071237ea838104bcf68cbd4d46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/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',
}

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)
    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"), 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</$key>"),
              Template("$w<$key>$value</$key>"), langs)