i18n/mki18n.py
changeset 1740 b789b695b5c6
parent 1739 ec153828ded2
child 1741 dd94b9a68c61
equal deleted inserted replaced
1739:ec153828ded2 1740:b789b695b5c6
   164     By default the function does not display what it is doing.  Set the
   164     By default the function does not display what it is doing.  Set the
   165     verbose argument to 1 to force it to print its commands.
   165     verbose argument to 1 to force it to print its commands.
   166     """
   166     """
   167 
   167 
   168     if applicationDomain is None:
   168     if applicationDomain is None:
   169         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
   169         applicationName = fileBaseOf(applicationDirectoryPath, withPath=0)
   170     else:
   170     else:
   171         applicationName = applicationDomain
   171         applicationName = applicationDomain
   172     currentDir = os.getcwd()
   172     currentDir = os.getcwd()
   173     os.chdir(applicationDirectoryPath)
   173     os.chdir(applicationDirectoryPath)
   174     filelist = 'app.fil'
   174     filelist = 'app.fil'
   175     if not os.path.exists(filelist):
   175     if not os.path.exists(filelist):
   176         raise IOError(2,'No module file: ' % filelist)
   176         raise IOError(2, 'No module file: ' % filelist)
   177 
   177 
   178     fileout = 'messages.pot'
   178     fileout = 'messages.pot'
   179     # Steps:
   179     # Steps:
   180     #  Use xgettext to parse all application modules
   180     #  Use xgettext to parse all application modules
   181     #  The following switches are used:
   181     #  The following switches are used:
   215 def catPO(applicationDirectoryPath, listOf_extraPo, applicationDomain=None, targetDir=None, verbose=0):
   215 def catPO(applicationDirectoryPath, listOf_extraPo, applicationDomain=None, targetDir=None, verbose=0):
   216     """Concatenate one or several PO files with the application domain files.
   216     """Concatenate one or several PO files with the application domain files.
   217     """
   217     """
   218 
   218 
   219     if applicationDomain is None:
   219     if applicationDomain is None:
   220         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
   220         applicationName = fileBaseOf(applicationDirectoryPath, withPath=0)
   221     else:
   221     else:
   222         applicationName = applicationDomain
   222         applicationName = applicationDomain
   223     currentDir = os.getcwd()
   223     currentDir = os.getcwd()
   224     os.chdir(applicationDirectoryPath)
   224     os.chdir(applicationDirectoryPath)
   225 
   225 
   231         else:
   231         else:
   232             langPOfileName = "%s_%s.po" % (applicationName, langCode)
   232             langPOfileName = "%s_%s.po" % (applicationName, langCode)
   233             if os.path.exists(langPOfileName):
   233             if os.path.exists(langPOfileName):
   234                 fileList = ''
   234                 fileList = ''
   235                 for fileName in listOf_extraPo:
   235                 for fileName in listOf_extraPo:
   236                     fileList += ("%s_%s.po " % (fileName,langCode))
   236                     fileList += ("%s_%s.po " % (fileName, langCode))
   237                 cmd = "msgcat -s --no-wrap %s %s > %s.cat" % (langPOfileName, fileList, langPOfileName)
   237                 cmd = "msgcat -s --no-wrap %s %s > %s.cat" % (langPOfileName, fileList, langPOfileName)
   238                 if verbose: print cmd
   238                 if verbose: print cmd
   239                 os.system(cmd)
   239                 os.system(cmd)
   240                 if targetDir is None:
   240                 if targetDir is None:
   241                     pass
   241                     pass
   242                 else:
   242                 else:
   243                     mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir,langCode)
   243                     mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir, langCode)
   244                     cmd = "msgfmt --output-file=%s/%s.mo %s_%s.po.cat" % (mo_targetDir,applicationName,applicationName,langCode)
   244                     cmd = "msgfmt --output-file=%s/%s.mo %s_%s.po.cat" % (mo_targetDir, applicationName, applicationName, langCode)
   245                     if verbose: print cmd
   245                     if verbose: print cmd
   246                     os.system(cmd)
   246                     os.system(cmd)
   247     os.chdir(currentDir)
   247     os.chdir(currentDir)
   248 
   248 
   249 
   249 
   250 def makeMO(applicationDirectoryPath,targetDir='./locale',applicationDomain=None, verbose=0, forceEnglish=0):
   250 def makeMO(applicationDirectoryPath, targetDir='./locale', applicationDomain=None, verbose=0, forceEnglish=0):
   251     """Compile the Portable Object files into the Machine Object stored in the right location.
   251     """Compile the Portable Object files into the Machine Object stored in the right location.
   252 
   252 
   253     makeMO converts all translated language-specific PO files located inside
   253     makeMO converts all translated language-specific PO files located inside
   254     the  application directory into the binary .MO files stored inside the
   254     the  application directory into the binary .MO files stored inside the
   255     LC_MESSAGES sub-directory for the found locale files.
   255     LC_MESSAGES sub-directory for the found locale files.
   268         targetDir = './locale'
   268         targetDir = './locale'
   269     if verbose:
   269     if verbose:
   270         print "Target directory for .mo files is: %s" % targetDir
   270         print "Target directory for .mo files is: %s" % targetDir
   271 
   271 
   272     if applicationDomain is None:
   272     if applicationDomain is None:
   273         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
   273         applicationName = fileBaseOf(applicationDirectoryPath, withPath=0)
   274     else:
   274     else:
   275         applicationName = applicationDomain
   275         applicationName = applicationDomain
   276     currentDir = os.getcwd()
   276     currentDir = os.getcwd()
   277     os.chdir(applicationDirectoryPath)
   277     os.chdir(applicationDirectoryPath)
   278 
   278 
   282         if (langCode == 'en') and (forceEnglish==0):
   282         if (langCode == 'en') and (forceEnglish==0):
   283             pass
   283             pass
   284         else:
   284         else:
   285             langPOfileName = "%s_%s.po" % (applicationName, langCode)
   285             langPOfileName = "%s_%s.po" % (applicationName, langCode)
   286             if os.path.exists(langPOfileName):
   286             if os.path.exists(langPOfileName):
   287                 mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir,langCode)
   287                 mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir, langCode)
   288                 if not os.path.exists(mo_targetDir):
   288                 if not os.path.exists(mo_targetDir):
   289                     mkdir(mo_targetDir)
   289                     mkdir(mo_targetDir)
   290                 cmd = 'msgfmt --output-file="%s/%s.mo" "%s_%s.po"' % (mo_targetDir,applicationName,applicationName,langCode)
   290                 cmd = 'msgfmt --output-file="%s/%s.mo" "%s_%s.po"' % (mo_targetDir, applicationName, applicationName, langCode)
   291                 if verbose: print cmd
   291                 if verbose: print cmd
   292                 os.system(cmd)
   292                 os.system(cmd)
   293     os.chdir(currentDir)
   293     os.chdir(currentDir)
   294 
   294 
   295 
   295 
   328     ##################################################################################"""
   328     ##################################################################################"""
   329     if errorMsg:
   329     if errorMsg:
   330         print "\n   ERROR: %s" % errorMsg
   330         print "\n   ERROR: %s" % errorMsg
   331 
   331 
   332 
   332 
   333 def fileBaseOf(filename,withPath=0):
   333 def fileBaseOf(filename, withPath=0):
   334    """fileBaseOf(filename,withPath) ---> string
   334    """fileBaseOf(filename,withPath) ---> string
   335 
   335 
   336    Return base name of filename.  The returned string never includes the extension.
   336    Return base name of filename.  The returned string never includes the extension.
   337    Use os.path.basename() to return the basename with the extension.  The
   337    Use os.path.basename() to return the basename with the extension.  The
   338    second argument is optional.  If specified and if set to 'true' (non zero)
   338    second argument is optional.  If specified and if set to 'true' (non zero)
   420    """
   420    """
   421    thePath = os.path.normpath(thePath)
   421    thePath = os.path.normpath(thePath)
   422    if os.sep == '/':
   422    if os.sep == '/':
   423       return thePath
   423       return thePath
   424    else:
   424    else:
   425       return thePath.replace(os.sep,'/')
   425       return thePath.replace(os.sep, '/')
   426 
   426 
   427 # -----------------------------------------------------------------------------
   427 # -----------------------------------------------------------------------------
   428 
   428 
   429 # S c r i p t   e x e c u t i o n               -- Runs when invoked from the command line --
   429 # S c r i p t   e x e c u t i o n               -- Runs when invoked from the command line --
   430 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   430 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   444     option['po'] = 0
   444     option['po'] = 0
   445     option['verbose'] = 0
   445     option['verbose'] = 0
   446     option['domain'] = None
   446     option['domain'] = None
   447     option['moTarget'] = None
   447     option['moTarget'] = None
   448     try:
   448     try:
   449         optionList,pargs = getopt.getopt(sys.argv[1:],validOptions,validLongOptions)
   449         optionList, pargs = getopt.getopt(sys.argv[1:], validOptions, validLongOptions)
   450     except getopt.GetoptError, e:
   450     except getopt.GetoptError, e:
   451         printUsage(e[0])
   451         printUsage(e[0])
   452         sys.exit(1)
   452         sys.exit(1)
   453     for (opt,val) in optionList:
   453     for (opt, val) in optionList:
   454         if (opt == '-h'):
   454         if (opt == '-h'):
   455             printUsage()
   455             printUsage()
   456             sys.exit(0)
   456             sys.exit(0)
   457         elif (opt == '-e'):         option['forceEnglish'] = 1
   457         elif (opt == '-e'):         option['forceEnglish'] = 1
   458         elif (opt == '-m'):         option['mo'] = 1
   458         elif (opt == '-m'):         option['mo'] = 1
   474         option['domain'] = fileBaseOf(appDirPath)
   474         option['domain'] = fileBaseOf(appDirPath)
   475     if option['verbose']:
   475     if option['verbose']:
   476         print "Application domain used is: '%s'" % option['domain']
   476         print "Application domain used is: '%s'" % option['domain']
   477     if option['po']:
   477     if option['po']:
   478         try:
   478         try:
   479             makePO(appDirPath,option['domain'],option['verbose'])
   479             makePO(appDirPath, option['domain'], option['verbose'])
   480         except IOError, e:
   480         except IOError, e:
   481             printUsage(e[1] + '\n   You must write a file app.fil that contains the list of all files to parse.')
   481             printUsage(e[1] + '\n   You must write a file app.fil that contains the list of all files to parse.')
   482     if option['mo']:
   482     if option['mo']:
   483         makeMO(appDirPath,option['moTarget'],option['domain'],option['verbose'],option['forceEnglish'])
   483         makeMO(appDirPath, option['moTarget'], option['domain'], option['verbose'], option['forceEnglish'])
   484     sys.exit(1)
   484     sys.exit(1)
   485 
   485 
   486 
   486 
   487 # -----------------------------------------------------------------------------
   487 # -----------------------------------------------------------------------------