home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Armagetron_Advanced / armagetronad-0.2.8.3.1.gcc.win32.exe / language / update.py < prev   
Encoding:
Python Source  |  2010-02-20  |  8.8 KB  |  234 lines

  1. #!/usr/bin/python
  2. # usage: call here to bring all translations up to date
  3. # usage forms:
  4. # update.py [--complete] <list of language files>
  5. #           updates all language files in the editable form (with comments); untranslated items
  6. #           are marked and decorated with the original text and the translations from the other
  7. #           languages from the given file list. Example:
  8. #           update.py spanish.txt is useful for editing the portugese translation if the s
  9. #           panish translation is up to date.
  10. #           the --complete switch adds the original texts as comments to all items, even
  11. #           those already translated.
  12. #           the --scm switch removes all comments added by previous runs so you can
  13. #           easily check in partial translations into source control.
  14. #
  15. # update.py --dist
  16. #           compactifies translations: strips comments and whitespace
  17.  
  18. import sys
  19. import os
  20. import string
  21.  
  22. # parse line for identifier/value pattern
  23. class LanguageUpdater:
  24.     def __init__(self):
  25.         # maximal assumed length of identifier
  26.         self.maxlen = 30
  27.         self.commentAll  = False
  28.         self.commentNone = False
  29.         self.autoComment = "#ORIGINAL TEXT:"
  30.         self.autoComment2 = "#TRANSLATION "
  31.         self.untranslated = "UNTRANSLATED\n"
  32.         self.outdated = "# Outdated:\n"
  33.         
  34.         self.translations = {}
  35.  
  36.     def ParseLine( self, line ):
  37.         stripped = line.expandtabs(4).lstrip()
  38.         if len(stripped) > 0 and stripped[0] != '#':
  39.             pair = stripped.split(" ",1)
  40.             pair[1] = pair[1].lstrip()
  41.             return pair
  42.         else:
  43.             return False
  44.     
  45.     def Write( self, key, value ):
  46.         self.outfile.write( (key + " ").ljust( self.maxlen ) + value )
  47.  
  48.     # fetch, print and delete translation from dictionary
  49.     def WriteFromDictionary( self, key ):
  50.         try:
  51.             # fetch translation...
  52.             trans = self.dictionary[ key ]
  53.             # print it...
  54.             self.Write( key, trans )
  55.             # and delete it.
  56.             del self.dictionary[ key ]
  57.         except KeyError:
  58.             # translation not found: note that.
  59.             if not self.commentNone:
  60.                 self.Write( "#" + key, self.untranslated )
  61.  
  62.     # writes dictionary to open outfile
  63.     def WriteDictionary0( self ):
  64.         for key in self.dictionary:
  65.             self.Write( key, self.dictionary[key] )
  66.  
  67.     # opens file, writes dictionary
  68.     def WriteDictionary1( self, translation ):
  69.         # open outfile
  70.         self.outfile = open( translation, "w" )
  71.  
  72.         # write to it in condensed form
  73.         self.WriteHeader()
  74.         self.outfile.write("# We don't maintain this file in this horrible unsorted format.\n# Execute update.py without arguments to return it into its good form.\n\n")
  75.         self.WriteDictionary0()
  76.  
  77.         # close outfile
  78.         self.outfile.close()
  79.         del self.outfile
  80.  
  81.     def WriteHeader( self ):
  82.         # write language the outfile is in
  83.         self.outfile.write(self.leadingComment)
  84.         self.WriteFromDictionary( "language" )
  85.         self.outfile.write("\n")
  86.  
  87.     # read contents of translation into dictionary
  88.     def ReadDictionary( self, translation ):
  89.         # read translation into dictionary
  90.         self.leadingComment = ""
  91.         started = False
  92.         outfilein = open( translation, "r" )
  93.         self.dictionary = {}
  94.         self.lostcomments = {}
  95.         for line in outfilein.readlines():
  96.             # parse line for identifier/value pattern
  97.                 pair = self.ParseLine( line )
  98.                 # put pair into dictionary
  99.                 if pair != False:
  100.                     started = True
  101.                     self.dictionary[pair[0]] = pair[1]
  102.                 else:
  103.                     if not started:
  104.                         self.leadingComment += line;
  105.                     else:
  106.                         if not line.startswith( self.autoComment ) and not line.startswith( self.autoComment2 ) and not line.endswith( self.untranslated ) and not line == self.outdated:
  107.                             self.lostcomments[line] = True
  108.         outfilein.close()
  109.  
  110.     # write contents of dictionary in the order its items appear in the file base.
  111.     def WriteDictionary( self, base, translation ):
  112.         # open outfile
  113.         self.outfile = open( translation, "w" )
  114.         
  115.         # open infile
  116.         infile  = open( base, "r" )
  117.     
  118.         self.WriteHeader()
  119.  
  120.         # flag indicating whether the next item needs an extra separation line
  121.         separate = True    
  122.  
  123.         # flag indicating whether the last language item was translated
  124.         lastTranslated = True
  125.  
  126.         # flag indicating whether the last line was empty
  127.         lastEmpty = False
  128.  
  129.         # read through base file, rewriting it
  130.         for line in infile.readlines():
  131.             pair = self.ParseLine( line )
  132.             if pair == False:
  133.                 # delete comment from lost comment archive
  134.                 try: del self.lostcomments[ line ]
  135.                 except KeyError: pass
  136.                 # just print line, it is a comment or whitespace
  137.                 empty = ( len(line) <= 1 )
  138.                 if lastTranslated or ( not empty or not lastEmpty ):
  139.                     self.outfile.write( line )
  140.                     lastEmpty = empty
  141.                 separate = False
  142.             else:
  143.                 if not pair[0] == "include":
  144.                     # write original text as a comment
  145.                     lastTranslated = pair[0] in self.dictionary
  146.                     if self.commentAll or ( not lastTranslated and not self.commentNone ):
  147.                         if separate:
  148.                             self.outfile.write("\n")
  149.                         self.Write( self.autoComment, pair[1] )
  150.                         for otherTranslation in self.translations:
  151.                             try:
  152.                                 self.Write( self.autoComment2 + otherTranslation, self.translations[otherTranslation][pair[0]] )
  153.                             except KeyError: pass
  154.                        
  155.                     # write translation
  156.                     self.WriteFromDictionary( pair[0] )
  157.                     separate = True
  158.                 
  159.         # write translated items that don't appear in the original
  160.         if len( self.dictionary ) > 0:
  161.             self.outfile.write( "\n" + self.outdated )
  162.             self.WriteDictionary0()
  163.  
  164.         # write old comments
  165.         for line in self.lostcomments:
  166.             self.outfile.write( line )
  167.         
  168.         self.outfile.close()
  169.         del self.outfile
  170.     
  171.     # update translation files with new items to translate
  172.     def Translate( self, base, translation ):
  173.         # read dictionary
  174.         self.ReadDictionary( translation )
  175.         # rewrite into different file
  176.         self.WriteDictionary( base, translation + ".out" )
  177.  
  178.         # rename files if no error happened
  179.         os.rename( translation, translation + ".bak" )
  180.         os.rename( translation + ".out", translation )
  181.  
  182.     # update translation files for distribution
  183.     def Distribute( self, translation ):
  184.         # read dictionary
  185.         self.ReadDictionary( translation )
  186.         # rewrite into different file
  187.         self.WriteDictionary1( translation + ".out" )
  188.  
  189.         # rename files if no error happened
  190.         os.rename( translation, translation + ".bak" )
  191.         os.rename( translation + ".out", translation )
  192.  
  193.     # add a file to the list of translations to print along with the english original
  194.     def AddTranslation( self, file ):
  195.         # read dictionary
  196.         self.ReadDictionary( file )
  197.         # store it
  198.         self.translations[string.split(file,".")[0]] = self.dictionary
  199.         del self.dictionary
  200.  
  201. if __name__ == "__main__":
  202.     # determine languages to update: everything included from
  203.     # languages.txt.in except am/eng and custom.
  204.     files = []
  205.     listfile = open("languages.txt.in")
  206.     for line in listfile.readlines():
  207.         if line.startswith("include"):
  208.             file =line.split()[1]
  209.             if file != "american.txt" and file != "english.txt" and file != "british.txt" and file != "custom.txt":
  210.                 files += [file]
  211.  
  212.     lu = LanguageUpdater()
  213.     if len( sys.argv ) >= 2 and sys.argv[1] == "--dist":
  214.         # strip comments from translations
  215.         lu.maxlen = 0
  216.         for file in files:            
  217.             lu.Distribute( file );
  218.     else:
  219.         if len( sys.argv ) >= 2 and sys.argv[1] == "--complete":
  220.             lu.commentAll = True
  221.         if len( sys.argv ) >= 2 and sys.argv[1] == "--scm":
  222.             lu.commentNone = True
  223.  
  224.         # read in all translations
  225.         for file in sys.argv[1:]:
  226.             if file[0] != "-":
  227.                 lu.AddTranslation(file);
  228.             
  229.         # update translations: mark untranslated items and add original text as comment
  230.         for file in files:            
  231.             lu.Translate( "english_base.txt", file );
  232.     
  233. #   if len( sys.argv ) >=3:
  234.