i18n/mki18n.py
changeset 1911 c1298e7ffe3a
parent 1624 bc84d659894f
child 1703 ba0dde7f047a
equal deleted inserted replaced
1910:a375e31bf312 1911:c1298e7ffe3a
    93 
    93 
    94 # -----------------------------------------------------------------------------
    94 # -----------------------------------------------------------------------------
    95 
    95 
    96 def getlanguageDict():
    96 def getlanguageDict():
    97     languageDict = {}
    97     languageDict = {}
    98     
    98 
       
    99     if wx.VERSION >= (3, 0, 0):
       
   100         app = wx.App()
       
   101     else:
       
   102         app = wx.PySimpleApp()
       
   103 
    99     for lang in [x for x in dir(wx) if x.startswith("LANGUAGE")]:
   104     for lang in [x for x in dir(wx) if x.startswith("LANGUAGE")]:
   100         i = wx.Locale(wx.LANGUAGE_DEFAULT).GetLanguageInfo(getattr(wx, lang))
   105         i = wx.Locale(wx.LANGUAGE_DEFAULT).GetLanguageInfo(getattr(wx, lang))
   101         if i:
   106         if i:
   102             languageDict[i.CanonicalName] = i.Description
   107             languageDict[i.CanonicalName] = i.Description
   103 
   108 
   104     return languageDict
   109     return languageDict
   105 
   110 
   106 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 
   107 
   138 
   108 # -----------------------------------------------------------------------------
   139 # -----------------------------------------------------------------------------
   109 # 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 --
   110 # ^^^^^^^^^^^^^^^
   141 # ^^^^^^^^^^^^^^^
   111 #
   142 #
   136     if applicationDomain is None:
   167     if applicationDomain is None:
   137         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
   168         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
   138     else:
   169     else:
   139         applicationName = applicationDomain
   170         applicationName = applicationDomain
   140     currentDir = os.getcwd()
   171     currentDir = os.getcwd()
   141     os.chdir(applicationDirectoryPath)                    
   172     os.chdir(applicationDirectoryPath)
   142     if not os.path.exists('app.fil'):
   173     filelist = 'app.fil'
   143         raise IOError(2,'No module file: app.fil')
   174     if not os.path.exists(filelist):
   144 
   175         raise IOError(2,'No module file: ' % filelist)
       
   176 
       
   177     fileout = 'messages.pot'
   145     # Steps:                                  
   178     # Steps:                                  
   146     #  Use xgettext to parse all application modules
   179     #  Use xgettext to parse all application modules
   147     #  The following switches are used:
   180     #  The following switches are used:
   148     #  
   181     #  
   149     #   -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)
   150     #   --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
   151     #   --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)
   152     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
   153     if verbose: print cmd
   186     if verbose: print cmd
   154     os.system(cmd)                                                
   187     os.system(cmd)                                                
   155 
   188 
   156     appfil_file = open("app.fil", 'r')
   189     XSD_STRING_MODEL = re.compile("<xsd\:(?:element|attribute) name=\"([^\"]*)\"[^\>]*\>")
   157     messages_file = open("messages.pot", 'r')
   190     processCustomFiles(filelist, fileout, XSD_STRING_MODEL, 'Extra XSD strings')
   158     messages = messages_file.read()
   191 
   159     messages_file.close()
   192     XML_TC6_STRING_MODEL = re.compile("<documentation>\s*<xhtml\:p><!\[CDATA\[([^\]]*)\]\]></xhtml\:p>\s*</documentation>", re.MULTILINE | re.DOTALL)
   160     messages_file = open("messages.pot", 'a')
   193     processCustomFiles(filelist, fileout, XML_TC6_STRING_MODEL, 'Extra TC6 documentation strings')    
   161     messages_file.write("""
   194 
   162 #: Extra XSD strings
       
   163 """)
       
   164     words_found = {}
       
   165     for filepath in appfil_file.xreadlines():
       
   166         code_file = open(filepath.strip(), 'r')
       
   167         for match in XSD_STRING_MODEL.finditer(code_file.read()):
       
   168             word = match.group(1)
       
   169             if not words_found.get(word, False) and messages.find("msgid \"%s\"\nmsgstr \"\"" % word) == -1:
       
   170                 words_found[word] = True
       
   171                 messages_file.write("""
       
   172 msgid "%s"
       
   173 msgstr ""
       
   174 """%word)
       
   175         code_file.close()
       
   176     messages_file.close()
       
   177     appfil_file.close()
       
   178     
       
   179     languageDict = getlanguageDict()
   195     languageDict = getlanguageDict()
   180 
   196 
   181     for langCode in languageDict.keys():
   197     for langCode in languageDict.keys():
   182         if langCode == 'en':
   198         if langCode == 'en':
   183             pass
   199             pass
   184         else:
   200         else:
   185             langPOfileName = "%s_%s.po" % (applicationName , langCode)
   201             langPOfileName = "%s_%s.po" % (applicationName , langCode)
   186             if os.path.exists(langPOfileName):
   202             if os.path.exists(langPOfileName):
   187                 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)
   188                 if verbose: print cmd
   204                 if verbose: print cmd
   189                 os.system(cmd)
   205                 os.system(cmd)
   190     os.chdir(currentDir)
   206     os.chdir(currentDir)
   191 
   207 
   192 # -----------------------------------------------------------------------------
   208 # -----------------------------------------------------------------------------