i18n/mki18n.py
changeset 1756 08e4394ff4fb
parent 1754 63f4af6bf6d9
child 1757 0de89da92ee0
equal deleted inserted replaced
1755:624b9694cb0d 1756:08e4394ff4fb
   108             languageDict[i.CanonicalName] = i.Description
   108             languageDict[i.CanonicalName] = i.Description
   109 
   109 
   110     return languageDict
   110     return languageDict
   111 
   111 
   112 
   112 
       
   113 def verbosePrint(verbose, str):
       
   114     if verbose:
       
   115         print str
       
   116 
       
   117 
   113 def processCustomFiles(filein, fileout, regexp, prefix=''):
   118 def processCustomFiles(filein, fileout, regexp, prefix=''):
   114     appfil_file = open(filein, 'r')
   119     appfil_file = open(filein, 'r')
   115     messages_file = open(fileout, 'r')
   120     messages_file = open(fileout, 'r')
   116     messages = messages_file.read()
   121     messages = messages_file.read()
   117     messages_file.close()
   122     messages_file.close()
   181     #
   186     #
   182     #   -s                          : sort output by string content (easier to use when we need to merge several .po files)
   187     #   -s                          : sort output by string content (easier to use when we need to merge several .po files)
   183     #   --files-from=app.fil        : The list of files is taken from the file: app.fil
   188     #   --files-from=app.fil        : The list of files is taken from the file: app.fil
   184     #   --output=                   : specifies the name of the output file (using a .pot extension)
   189     #   --output=                   : specifies the name of the output file (using a .pot extension)
   185     cmd = 'xgettext -s --no-wrap --language=Python --files-from=' + filelist + ' --output=' + fileout + ' --package-name ' + applicationName
   190     cmd = 'xgettext -s --no-wrap --language=Python --files-from=' + filelist + ' --output=' + fileout + ' --package-name ' + applicationName
   186     if verbose: print cmd
   191     verbosePrint(verbose, cmd)
   187     os.system(cmd)
   192     os.system(cmd)
   188 
   193 
   189     XSD_STRING_MODEL = re.compile("<xsd\:(?:element|attribute) name=\"([^\"]*)\"[^\>]*\>")
   194     XSD_STRING_MODEL = re.compile("<xsd\:(?:element|attribute) name=\"([^\"]*)\"[^\>]*\>")
   190     processCustomFiles(filelist, fileout, XSD_STRING_MODEL, 'Extra XSD strings')
   195     processCustomFiles(filelist, fileout, XSD_STRING_MODEL, 'Extra XSD strings')
   191 
   196 
   192     XML_TC6_STRING_MODEL = re.compile("<documentation>\s*<xhtml\:p><!\[CDATA\[([^\]]*)\]\]></xhtml\:p>\s*</documentation>", re.MULTILINE | re.DOTALL)
   197     XML_TC6_STRING_MODEL = re.compile("<documentation>\s*<xhtml\:p><!\[CDATA\[([^\]]*)\]\]></xhtml\:p>\s*</documentation>", re.MULTILINE | re.DOTALL)
   193     processCustomFiles(filelist, fileout, XML_TC6_STRING_MODEL, 'Extra TC6 documentation strings')
   198     processCustomFiles(filelist, fileout, XML_TC6_STRING_MODEL, 'Extra TC6 documentation strings')
   194 
   199 
   195     # generate messages.po
   200     # generate messages.po
   196     cmd = 'msginit --no-wrap --no-translator -i %s -l en_US.UTF-8 -o messages.po' % (fileout)
   201     cmd = 'msginit --no-wrap --no-translator -i %s -l en_US.UTF-8 -o messages.po' % (fileout)
   197     if verbose: print cmd
   202     verbosePrint(verbose, cmd)
   198     os.system(cmd)
   203     os.system(cmd)
   199 
   204 
   200     languageDict = getlanguageDict()
   205     languageDict = getlanguageDict()
   201 
   206 
   202     for langCode in languageDict.keys():
   207     for langCode in languageDict.keys():
   204             pass
   209             pass
   205         else:
   210         else:
   206             langPOfileName = "%s_%s.po" % (applicationName, langCode)
   211             langPOfileName = "%s_%s.po" % (applicationName, langCode)
   207             if os.path.exists(langPOfileName):
   212             if os.path.exists(langPOfileName):
   208                 cmd = 'msgmerge -s --no-wrap "%s" %s > "%s.new"' % (langPOfileName, fileout, langPOfileName)
   213                 cmd = 'msgmerge -s --no-wrap "%s" %s > "%s.new"' % (langPOfileName, fileout, langPOfileName)
   209                 if verbose: print cmd
   214                 verbosePrint(verbose, cmd)
   210                 os.system(cmd)
   215                 os.system(cmd)
   211     os.chdir(currentDir)
   216     os.chdir(currentDir)
   212 
   217 
   213 
   218 
   214 def catPO(applicationDirectoryPath, listOf_extraPo, applicationDomain=None, targetDir=None, verbose=0):
   219 def catPO(applicationDirectoryPath, listOf_extraPo, applicationDomain=None, targetDir=None, verbose=0):
   232             if os.path.exists(langPOfileName):
   237             if os.path.exists(langPOfileName):
   233                 fileList = ''
   238                 fileList = ''
   234                 for fileName in listOf_extraPo:
   239                 for fileName in listOf_extraPo:
   235                     fileList += ("%s_%s.po " % (fileName, langCode))
   240                     fileList += ("%s_%s.po " % (fileName, langCode))
   236                 cmd = "msgcat -s --no-wrap %s %s > %s.cat" % (langPOfileName, fileList, langPOfileName)
   241                 cmd = "msgcat -s --no-wrap %s %s > %s.cat" % (langPOfileName, fileList, langPOfileName)
   237                 if verbose: print cmd
   242                 verbosePrint(verbose, cmd)
   238                 os.system(cmd)
   243                 os.system(cmd)
   239                 if targetDir is None:
   244                 if targetDir is None:
   240                     pass
   245                     pass
   241                 else:
   246                 else:
   242                     mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir, langCode)
   247                     mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir, langCode)
   243                     cmd = "msgfmt --output-file=%s/%s.mo %s_%s.po.cat" % (mo_targetDir, applicationName, applicationName, langCode)
   248                     cmd = "msgfmt --output-file=%s/%s.mo %s_%s.po.cat" % (mo_targetDir, applicationName, applicationName, langCode)
   244                     if verbose: print cmd
   249                     verbosePrint(verbose, cmd)
   245                     os.system(cmd)
   250                     os.system(cmd)
   246     os.chdir(currentDir)
   251     os.chdir(currentDir)
   247 
   252 
   248 
   253 
   249 def makeMO(applicationDirectoryPath, targetDir='./locale', applicationDomain=None, verbose=0, forceEnglish=0):
   254 def makeMO(applicationDirectoryPath, targetDir='./locale', applicationDomain=None, verbose=0, forceEnglish=0):
   261 
   266 
   262     makeMo stores the resulting files inside a sub-directory of `targetDir`
   267     makeMo stores the resulting files inside a sub-directory of `targetDir`
   263     called xx/LC_MESSAGES where 'xx' corresponds to the 2-letter language
   268     called xx/LC_MESSAGES where 'xx' corresponds to the 2-letter language
   264     code.
   269     code.
   265     """
   270     """
       
   271 
   266     if targetDir is None:
   272     if targetDir is None:
   267         targetDir = './locale'
   273         targetDir = './locale'
   268     if verbose:
   274 
   269         print "Target directory for .mo files is: %s" % targetDir
   275     verbosePrint(verbose, "Target directory for .mo files is: %s" % targetDir)
   270 
   276 
   271     if applicationDomain is None:
   277     if applicationDomain is None:
   272         applicationName = fileBaseOf(applicationDirectoryPath, withPath=0)
   278         applicationName = fileBaseOf(applicationDirectoryPath, withPath=0)
   273     else:
   279     else:
   274         applicationName = applicationDomain
   280         applicationName = applicationDomain
   285             if os.path.exists(langPOfileName):
   291             if os.path.exists(langPOfileName):
   286                 mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir, langCode)
   292                 mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir, langCode)
   287                 if not os.path.exists(mo_targetDir):
   293                 if not os.path.exists(mo_targetDir):
   288                     mkdir(mo_targetDir)
   294                     mkdir(mo_targetDir)
   289                 cmd = 'msgfmt --output-file="%s/%s.mo" "%s_%s.po"' % (mo_targetDir, applicationName, applicationName, langCode)
   295                 cmd = 'msgfmt --output-file="%s/%s.mo" "%s_%s.po"' % (mo_targetDir, applicationName, applicationName, langCode)
   290                 if verbose: print cmd
   296                 verbosePrint(verbose, cmd)
   291                 os.system(cmd)
   297                 os.system(cmd)
   292     os.chdir(currentDir)
   298     os.chdir(currentDir)
   293 
   299 
   294 
   300 
   295 def printUsage(errorMsg=None):
   301 def printUsage(errorMsg=None):
   443     option['mo'] = 0
   449     option['mo'] = 0
   444     option['po'] = 0
   450     option['po'] = 0
   445     option['verbose'] = 0
   451     option['verbose'] = 0
   446     option['domain'] = None
   452     option['domain'] = None
   447     option['moTarget'] = None
   453     option['moTarget'] = None
       
   454     optionKey = {
       
   455         '-e':         'forceEnglish',
       
   456         '-m':         'mo',
       
   457         '-p':         'po',
       
   458         '-v':         'verbose',
       
   459         '--domain':   'domain',
       
   460         '--moTarget': 'moTarget',
       
   461     }
       
   462 
   448     try:
   463     try:
   449         optionList, pargs = getopt.getopt(sys.argv[1:], validOptions, validLongOptions)
   464         optionList, pargs = getopt.getopt(sys.argv[1:], validOptions, validLongOptions)
   450     except getopt.GetoptError, e:
   465     except getopt.GetoptError, e:
   451         printUsage(e[0])
   466         printUsage(e[0])
   452         sys.exit(1)
   467         sys.exit(1)
   453     for (opt, val) in optionList:
   468     for (opt, val) in optionList:
   454         if (opt == '-h'):
   469         if (opt == '-h'):
   455             printUsage()
   470             printUsage()
   456             sys.exit(0)
   471             sys.exit(0)
   457         elif (opt == '-e'):         option['forceEnglish'] = 1
   472         option[optionKey[opt]] = 1 if val == '' else val
   458         elif (opt == '-m'):         option['mo'] = 1
       
   459         elif (opt == '-p'):         option['po'] = 1
       
   460         elif (opt == '-v'):         option['verbose'] = 1
       
   461         elif (opt == '--domain'):   option['domain'] = val
       
   462         elif (opt == '--moTarget'): option['moTarget'] = val
       
   463     if len(pargs) == 0:
   473     if len(pargs) == 0:
   464         appDirPath = os.getcwd()
   474         appDirPath = os.getcwd()
   465         if option['verbose']:
   475         if option['verbose']:
   466             print "No project directory given. Using current one:  %s" % appDirPath
   476             print "No project directory given. Using current one:  %s" % appDirPath
   467     elif len(pargs) == 1:
   477     elif len(pargs) == 1: