Pella86

Translator

Aug 17th, 2017
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | None | 0 0
  1. from os.path import isfile
  2. import os
  3.  
  4. class Translator:
  5.     pass
  6.     # the class will receive strings form the program
  7.     # will check if the strings exists and return a translation of the string in the requested language
  8.     # will compose a text file where the first string is the english version
  9.     # or bot version, while the second is the translation
  10.     # the bot will take the strings as soon as they are called, check for the
  11.     # translation and select the user language option
  12.    
  13.     def __init__(self):
  14.        
  15.         # define a file where one stores the tags
  16.         # it is in the folder /data/language_tags
  17.        
  18.         self.lang_folder = "./data/languages/"
  19.        
  20.         if not os.path.isdir(self.lang_folder):
  21.             os.mkdir(self.lang_folder)
  22.        
  23.         self.p = self.lang_folder + "language_tags.txt"
  24.        
  25.         self.tags = []
  26.        
  27.         self.langtostr = {} # key is (language-tag, string)
  28.        
  29.         if isfile(self.p):
  30.             with open(self.p, 'r') as f:
  31.                 dlines = f.readlines()
  32.            
  33.             for line in dlines:
  34.                 if not line.startswith("#") and line.strip():
  35.                     self.tags.append(line.strip())
  36.         else:
  37.             #initialize a empty file
  38.             with open(self.p, 'w') as f:
  39.                 f.write("\n")
  40.        
  41.         #read the blocks
  42.         for tag in self.tags:
  43.             print("tag:", tag)
  44.             with open(self.getLangFileName(tag), "r") as f:
  45.                 dlines = f.readlines()
  46.            
  47.             line = dlines.pop(0)
  48.             while dlines:
  49.                 print("line", line)
  50.                 org_str = ""
  51.                 tstring = ""
  52.                 if line == "-----English String:\n":
  53.                     line = dlines.pop(0)
  54.                    
  55.                     while line != "Translated String:\n":
  56.                         org_str += line
  57.                         line = dlines.pop(0)
  58.                    
  59.                     line = dlines.pop(0)
  60.                    
  61.                     while line != "-----English String:\n" and dlines:
  62.                         tstring += line
  63.                         line = dlines.pop(0)
  64.                    
  65.                    
  66.                    
  67.                     while tstring.endswith("\n"):
  68.                         tstring = tstring[:-1]
  69.                    
  70.                     print("match strings:")
  71.                     print(repr(org_str[:-2]), repr(tstring))    
  72.                     self.langtostr[(tag, org_str[:-2])] = tstring
  73.                
  74.                 if dlines and line != "-----English String:\n":
  75.                     line = dlines.pop(0)
  76.                    
  77.  
  78.    
  79.     def getTagFolderName(self, tag):
  80.         return self.lang_folder + "language_" + tag + "/"
  81.    
  82.     def getLangFileName(self, tag):
  83.         return self.getTagFolderName(tag) + "translation_file_" + tag + ".txt"
  84.    
  85.     def updateTags(self, tag):
  86.         self.tags.append(tag)
  87.         # write to file
  88.         with open(self.p, "a") as f:
  89.             f.write(tag + "\n")
  90.            
  91.         # open a folder with name "language_XX-XX"
  92.         os.mkdir(self.getTagFolderName(tag))
  93.        
  94.         with open(self.getLangFileName(tag), 'w') as f:
  95.             f.write("\n")
  96.        
  97.     def addTranslationBlock(self, string, tag):
  98.         with open(self.getLangFileName(tag), "a") as f:
  99.             f.write("-----English String:\n")    
  100.             f.write(string)
  101.            
  102.             f.write("\n\nTranslated String:\n")
  103.             f.write("\n\n")
  104.  
  105.  
  106. mytr = Translator()                
  107.    
  108. def _(string, language_tag):
  109.     if language_tag not in mytr.tags:
  110.         mytr.updateTags(language_tag)
  111.    
  112.    
  113.     if (language_tag, string) not in mytr.langtostr:
  114.         # append to the translation file in the folder the string
  115.         mytr.addTranslationBlock(string, language_tag)
  116.         mytr.langtostr[(language_tag, string)] = ""
  117.    
  118.     # get the translated string
  119.     langstr = mytr.langtostr[(language_tag, string)]
  120.     if langstr:
  121.         return langstr
  122.     else:
  123.         return string
  124.            
  125.  
  126. if __name__ == "__main__":
  127.     print("Hello")  
  128.  
  129.     mylang = _("Hello my name is Ciao", "it-IT")    
  130.  
  131.     print(mylang)    
  132.    
  133.     mylang = _("Hello my name is Ciao", "de-CH")
  134.    
  135.     print(mylang)
  136.    
  137.     mylang = _("We have good news", "de-CH")
  138.    
  139.     print(mylang)
Advertisement
Add Comment
Please, Sign In to add comment