summaryrefslogtreecommitdiff
path: root/web/updatelang.py
blob: 8e0f82d6c5336488a72f9f2d2363e3621dc6ebf2 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#! /usr/bin/env python3
# -*- coding: utf8 -*-
#
# Copyright (C) 2018~2020  TMW-2
# Author: Jesusalva
# Note: This will "force" completion to 100%. Use review system!

import copy, datetime
import polib, yaml

defaultLang = "en"
rootPath = "../../site/i18n/"

langs=[]
files={}
originals={}
tm={defaultLang: ["","\n"]}

# Search for array[?]==search in an array of dicts
# Returns the dictionary, or returns "ERROR"
def dl_search(array, search):
    for key in array.keys():
        if array[key] == search:
            return key
    raise Exception("Key not found")

# For headers
def currentdate():
    dt=datetime.datetime.now()

    day=dt.timetuple()[2]
    month=dt.timetuple()[1]
    year=dt.timetuple()[0]

    hour=dt.timetuple()[3]
    minute=dt.timetuple()[4]
    #second=5
    #weekday=6
    #yearday=7

    return "%04d-%02d-%02d %02d:%02d-0300" % (year, month, day, hour, minute)


def init():
    global defaultLang, rootPath, langs, files, originals, tm
    # Populate langs
    o=open("langs.txt", "r")
    for i in o:
        langs.append(i.replace('\n',''))
    o.close()

    # Create the original file
    f=open(rootPath+defaultLang+".yml", "r")
    originals=yaml.load(f, Loader=yaml.FullLoader) # Dictionary
    f.close()

    # We don't care with metadata
    del originals["language_meta"]

    # Create each language's template
    for i in langs:
        files[str(i)]=copy.copy(originals)
        tm[str(i)]=["","\n"]

    """
    # Do the reading for each language
    for i in langs:
        f=open(rootpath+i+".yml", "r")
        files[str(i)]=yaml.load(f, Loader=yaml.FullLoader) # Dictionary
        f.close()
    """

# [OK] Returns name from language code
def lgname(lg):
    if lg == "en":
        return "English"
    elif lg == "pt-BR":
        return "Português (Brasil)"
    elif lg == "de":
        return "Deutsch"
    elif lg == "fr":
        return "François"
    elif lg == "es":
        return "Español"
    else:
        return "Unknown %s" % lg

# [OK] Entry
def poentry(org, ttl, comments):
    return polib.POEntry(msgid=org, msgstr=ttl, comment=comments)

# Creates/Loads stuff
def generatePoFiles():
    global tm
    context=copy.copy(langs)
    context.append('en')
    for lg in context:
        print("Updating po file for "+lg)
        po=polib.POFile()
        po.metadata = {
            'Project-Id-Version': '1.0',
            'Report-Msgid-Bugs-To': 'dev@tmw2.org',
            'POT-Creation-Date': currentdate(),
            'PO-Revision-Date': currentdate(),
            'Last-Translator': 'TMW2 Team <dev@tmw2.org>',
            'MIME-Version': '1.0',
            'Content-Type': 'text/plain; charset=utf-8',
            'Content-Transfer-Encoding': '8bit',
        }

        for key, speech in originals.items():
            # Translation Memory (no duplicates)
            if speech in tm[lg]:
                continue
            tm[lg].append(speech)

            # Add to po file
            if lg == "en":
                po.append(poentry(speech, "", key))
            else:
                po.append(poentry(speech, files[lg][key], key))

        po.save("po/%s.po" % lg)
    context.remove('en')





# [OK] Reads Po Files
def readPoFile(lg):
    try:
        po=polib.pofile("po/"+lg+".po")
    except:
        a=open("po/"+lg+".po", "w")
        a.close()
        print("%s.po does not exist, not reading" % lg)
        return

    print("%s Progress: %d%%" % (lgname(lg), po.percent_translated()))

    for entry in po:
        #print("%s = %s" % (entry.msgid, entry.msgstr))

        try:
            if entry.msgstr != "":
                dest=dl_search(files[lg], entry.msgid)
                #print("[+] %s" % dest)
                files[lg][dest]=entry.msgstr
            else:
                dest=dl_search(files[lg], entry.msgid)
                #print("%s.%s IS EMPTY" % (lg, dest))
                try:
                    files[lg][dest]=originals[dest]
                except:
                    print("%s - failed to obtain originals" % entry.msgid)
        except:
                print("%s - string was removed" % entry.msgstr)

# [OK] Save the new YML file
def writeLocal(lg):
    f=open(rootPath+lg+".yml", 'w')
    f.write("# THIS FILE WAS GENERATED AUTOMATICALLY\n#EDITING IT WILL HAVE NO EFFECT\n\n")
    files[lg]["language_meta"]={"code": lg, "name": lgname(lg)}
    yaml.dump(files[lg], f)
    f.close()











# Mainframe: setup
init()

# Save translations to their YML files
for lg in langs:
    readPoFile(lg)
    writeLocal(lg)

# Mainframe: handle PO files
generatePoFiles()