import sys
class Translation:
"Translation class."
def __init__(self, *tables):
"Initialise object."
self.thanTables = tables # Each table represents translationb from one language to another
self.thanLangSet("en", "en")
def thanLangSet(self, from_, to):
"Set different languages."
self.thanTrans = {}
self.thanUnknown = {}
self.thanUnknownl = []
for table in self.thanTables:
langfrom, encfrom, langto, encto = table["__TRANSLATION__"]
if langfrom == from_ and langto == to: break
else:
return # The translation structures are empty
self.thanTrans.update(table)
del self.thanTrans["__TRANSLATION__"]
def __getitem__(self, key):
"Return the translation of key."
try:
return self.thanTrans[key]
except KeyError:
pass # Trnslation was not found for key
try:
i = self.thanUnknown[key] # We encountered the same key before
except KeyError:
self.thanUnknownl.append(key) # This is the first time we encounter key
i = 0
self.thanUnknown[key] = i+1 # Increase how many times we encountered key
return key # The "translation" is the same
def thanReport(self, fw=sys.stdout):
"Report all the keys which we did not found translation for."
for key in self.thanUnknownl:
n = max(0, 50-2-len(key))
fw.write('"%s"%s: "%s",\n' % (key, " "*n, self.thanUnknown[key]))
|