i18n/mki18n.py
changeset 1624 bc84d659894f
parent 1482 5d4543ee5a5d
child 1703 ba0dde7f047a
equal deleted inserted replaced
1623:aa9ec1d5c54c 1624:bc84d659894f
   106         if i:
   106         if i:
   107             languageDict[i.CanonicalName] = i.Description
   107             languageDict[i.CanonicalName] = i.Description
   108 
   108 
   109     return languageDict
   109     return languageDict
   110 
   110 
   111 XSD_STRING_MODEL = re.compile("<xsd\:(?:element|attribute) name=\"([^\"]*)\"[^\>]*\>")
   111 
       
   112 
       
   113 def processCustomFiles(filein, fileout, regexp, prefix = ''):
       
   114     appfil_file = open(filein, 'r')
       
   115     messages_file = open(fileout, 'r')
       
   116     messages = messages_file.read()
       
   117     messages_file.close()
       
   118     messages_file = open(fileout, 'a')
       
   119     messages_file.write('\n')
       
   120     messages_file.write('#: %s\n' % prefix)
       
   121     messages_file.write('\n')
       
   122 
       
   123     words_found = {}
       
   124     for filepath in appfil_file.xreadlines():
       
   125         code_file = open(filepath.strip(), 'r')
       
   126         for match in regexp.finditer(code_file.read()):
       
   127             word = match.group(1)
       
   128             if not words_found.get(word, False) and messages.find("msgid \"%s\"\nmsgstr \"\"" % word) == -1:
       
   129                 words_found[word] = True
       
   130                 messages_file.write('\n')
       
   131                 messages_file.write("msgid \"%s\"\n"%word)
       
   132                 messages_file.write("msgstr \"\"\n")
       
   133         code_file.close()
       
   134 
       
   135     messages_file.close()
       
   136     appfil_file.close()
       
   137 
   112 
   138 
   113 # -----------------------------------------------------------------------------
   139 # -----------------------------------------------------------------------------
   114 # m a k e P O ( )         -- Build the Portable Object file for the application --
   140 # m a k e P O ( )         -- Build the Portable Object file for the application --
   115 # ^^^^^^^^^^^^^^^
   141 # ^^^^^^^^^^^^^^^
   116 #
   142 #
   141     if applicationDomain is None:
   167     if applicationDomain is None:
   142         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
   168         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
   143     else:
   169     else:
   144         applicationName = applicationDomain
   170         applicationName = applicationDomain
   145     currentDir = os.getcwd()
   171     currentDir = os.getcwd()
   146     os.chdir(applicationDirectoryPath)                    
   172     os.chdir(applicationDirectoryPath)
   147     if not os.path.exists('app.fil'):
   173     filelist = 'app.fil'
   148         raise IOError(2,'No module file: app.fil')
   174     if not os.path.exists(filelist):
   149 
   175         raise IOError(2,'No module file: ' % filelist)
       
   176 
       
   177     fileout = 'messages.pot'
   150     # Steps:                                  
   178     # Steps:                                  
   151     #  Use xgettext to parse all application modules
   179     #  Use xgettext to parse all application modules
   152     #  The following switches are used:
   180     #  The following switches are used:
   153     #  
   181     #  
   154     #   -s                          : sort output by string content (easier to use when we need to merge several .po files)
   182     #   -s                          : sort output by string content (easier to use when we need to merge several .po files)
   155     #   --files-from=app.fil        : The list of files is taken from the file: app.fil
   183     #   --files-from=app.fil        : The list of files is taken from the file: app.fil
   156     #   --output=                   : specifies the name of the output file (using a .pot extension)
   184     #   --output=                   : specifies the name of the output file (using a .pot extension)
   157     cmd = 'xgettext -s --no-wrap --language=Python --files-from=app.fil --output=messages.pot'
   185     cmd = 'xgettext -s --no-wrap --language=Python --files-from=' + filelist + ' --output=' + fileout
   158     if verbose: print cmd
   186     if verbose: print cmd
   159     os.system(cmd)                                                
   187     os.system(cmd)                                                
   160 
   188 
   161     appfil_file = open("app.fil", 'r')
   189     XSD_STRING_MODEL = re.compile("<xsd\:(?:element|attribute) name=\"([^\"]*)\"[^\>]*\>")
   162     messages_file = open("messages.pot", 'r')
   190     processCustomFiles(filelist, fileout, XSD_STRING_MODEL, 'Extra XSD strings')
   163     messages = messages_file.read()
   191 
   164     messages_file.close()
   192     XML_TC6_STRING_MODEL = re.compile("<documentation>\s*<xhtml\:p><!\[CDATA\[([^\]]*)\]\]></xhtml\:p>\s*</documentation>", re.MULTILINE | re.DOTALL)
   165     messages_file = open("messages.pot", 'a')
   193     processCustomFiles(filelist, fileout, XML_TC6_STRING_MODEL, 'Extra TC6 documentation strings')    
   166     messages_file.write("""
   194 
   167 #: Extra XSD strings
       
   168 """)
       
   169     words_found = {}
       
   170     for filepath in appfil_file.xreadlines():
       
   171         code_file = open(filepath.strip(), 'r')
       
   172         for match in XSD_STRING_MODEL.finditer(code_file.read()):
       
   173             word = match.group(1)
       
   174             if not words_found.get(word, False) and messages.find("msgid \"%s\"\nmsgstr \"\"" % word) == -1:
       
   175                 words_found[word] = True
       
   176                 messages_file.write("""
       
   177 msgid "%s"
       
   178 msgstr ""
       
   179 """%word)
       
   180         code_file.close()
       
   181     messages_file.close()
       
   182     appfil_file.close()
       
   183     
       
   184     languageDict = getlanguageDict()
   195     languageDict = getlanguageDict()
   185 
   196 
   186     for langCode in languageDict.keys():
   197     for langCode in languageDict.keys():
   187         if langCode == 'en':
   198         if langCode == 'en':
   188             pass
   199             pass
   189         else:
   200         else:
   190             langPOfileName = "%s_%s.po" % (applicationName , langCode)
   201             langPOfileName = "%s_%s.po" % (applicationName , langCode)
   191             if os.path.exists(langPOfileName):
   202             if os.path.exists(langPOfileName):
   192                 cmd = 'msgmerge -s --no-wrap "%s" messages.pot > "%s.new"' % (langPOfileName, langPOfileName)
   203                 cmd = 'msgmerge -s --no-wrap "%s" %s > "%s.new"' % (langPOfileName, fileout, langPOfileName)
   193                 if verbose: print cmd
   204                 if verbose: print cmd
   194                 os.system(cmd)
   205                 os.system(cmd)
   195     os.chdir(currentDir)
   206     os.chdir(currentDir)
   196 
   207 
   197 # -----------------------------------------------------------------------------
   208 # -----------------------------------------------------------------------------