summaryrefslogtreecommitdiff
path: root/web/updatelang.py
diff options
context:
space:
mode:
Diffstat (limited to 'web/updatelang.py')
-rwxr-xr-xweb/updatelang.py244
1 files changed, 127 insertions, 117 deletions
diff --git a/web/updatelang.py b/web/updatelang.py
index e7a367a..c2162c3 100755
--- a/web/updatelang.py
+++ b/web/updatelang.py
@@ -4,161 +4,171 @@
# Copyright (C) 2018 TMW-2
# Author: Jesusalva
-import os
+import os, copy, datetime
+import polib, yaml
defaultLang = "en"
-rootPath = "../../web/"
+rootPath = "../../site/i18n/"
langs=[]
files={}
originals={}
tm=["","\n"]
-def invalidline(line, filx="none"):
- return (line.startswith("include ") or
- line.startswith("<?php") or
- line.startswith("?>") or
- "<div " in line or
- "</div>" in line or
- "iframe" in line or
- "header" in filx or
- (line.startswith(" <p>") and line.endswith("<p>\n")) or
- (line.startswith(" </p>") and line.endswith("</p>\n")) or
- (line.startswith(" <pre><code>") and line.endswith("<code>\n")) or
- (line.startswith(" </code></pre>") and line.endswith("</pre>\n")) or
- line.replace('\n','') == "")
-
-# Populates the basic arrays
-def populate():
+# 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
+ return "ERROR"
+
+# 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()
- # Populate files
- for i in os.listdir(rootPath):
- if i.endswith(".php.en"):
- print("Valid file: "+str(i))
- files[i]={}
- for x in langs:
- files[i][x]={}
-
- #print(str(files))
-
+ # 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)
+
+ """
+ # 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 Brasileiro"
+ elif lg == "de":
+ return "Deustche"
+ elif lg == "fr":
+ return "François"
+ elif lg == "es":
+ return "Español"
+ else:
+ return "Unknown %s" % lg
+
+# [OK] Entry
+def poentry(org, ttl):
+ return polib.POEntry(msgid=org, msgstr=ttl)
# Creates/Loads stuff
-def genreadPoFile():
- for a in langs:
- print("Reading translations for "+a)
- for i in files:
- readPoFile(i,a)
-
def generatePoFiles():
global tm
- context=langs
+ context=copy.copy(langs)
context.append('en')
- for a in langs:
- print("Updating po file for "+a)
- xrv=open("po/"+a+".po", "w")
- # Prints header
- xrv.write('\
-# Copyright (C) 2018 TMW2\n\
-#\n\
-\n\
-msgid ""\n\
-msgstr ""\n\
-"Project-Id-Version: TMW2\\n"\n\
-"MIME-Version: 1.0\\n"\n\
-"Content-Type: text/plain; charset=UTF-8\\n"\n\
-"Content-Transfer-Encoding: 8bit\\n"\n\
-\n\
-')
- xrv.close()
-
- tm=[""]
- for i in files:
- writePoFile(i,a)
+ 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():
+ if lg == "en":
+ po.append(poentry(speech, ""))
+ else:
+ po.append(poentry(speech, files[lg][key]))
+
+ po.save("po/%s.po" % lg)
context.remove('en')
-def generateLocal():
- for a in langs:
- print("generating local file for "+a)
- for i in files:
- writeLocal(i,a)
-# Write po files. TODO: WARNING: Some translations are getting overriden!
-def writePoFile(nf,lg):
- # Translation Memory, to prevent duplicates
- global tm
- a=open("po/"+lg+".po", "a")
- f=open(rootPath+nf, "r")
+# [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))
- for line in f:
- if line in tm or invalidline(line, nf):
- continue
+ if entry.msgstr != "":
+ dest=dl_search(files[lg], entry.msgid)
+ print("[+] %s" % dest)
+ files[lg][dest]=entry.msgstr
else:
- tm.append(line)
+ dest=dl_search(files[lg], entry.msgid)
+ print("IS EMPTY")
+ files[lg][dest]=originals[dest]
+
+# [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()
+
+
- a.write("#, no-c-format\n")
- a.write('msgid "%s"\n' % line.replace('\n', '').replace('"','\\"'))
- try:
- if files[nf][lg][line.replace('\n', '')] != line.replace('\n', '') and files[nf][lg][line.replace('\n', '')] != "":
- a.write('msgstr "%s"\n' % files[nf][lg][line.replace('\n', '')].replace('\n','').replace('"','\\"'))
- else:
- a.write('msgstr "%s"\n' % "")
- except KeyError:
- a.write('msgstr "%s"\n' % "")
- a.write('\n')
- a.close()
-# Reads Po Files
-def readPoFile(nf, lg):
- try:
- a=open("po/"+lg+".po", "r")
- except:
- a=open("po/"+lg+".po", "w")
- a.close()
- a=open("po/"+lg+".po", "r")
-
- ctx=""
- for line in a:
- if "msgid " in line:
- ctx=line.replace('"\n','').replace('\\"','"').replace('msgid "', "")
- if "msgstr " in line and ctx != "":
- if line != 'msgstr ""\n':
- files[nf][lg][ctx]=line.replace('"\n', '').replace('\\"','"').replace('msgstr "', "")
- else:
- files[nf][lg][ctx]=ctx
- ctx=""
- a.close()
-def writeLocal(nf, lg):
- a=open(rootPath+nf[:-2]+lg, 'w')
- b=open(rootPath+nf, 'r')
- for line in b:
- try:
- a.write(files[nf][lg][line.replace('\n', '')])
- a.write('\n')
- except KeyError:
- a.write(line)
- b.close()
- a.close()
+# Mainframe: setup
+init()
-# Mainframe: populate arrays
-populate()
+# Save translations to their YML files
+for lg in langs:
+ readPoFile(lg)
+ writeLocal(lg)
# Mainframe: handle PO files
-genreadPoFile()
generatePoFiles()
-generateLocal()