i18n/mki18n.py
changeset 1739 ec153828ded2
parent 1738 d2e979738700
child 1740 b789b695b5c6
equal deleted inserted replaced
1738:d2e979738700 1739:ec153828ded2
   139 
   139 
   140 # -----------------------------------------------------------------------------
   140 # -----------------------------------------------------------------------------
   141 # m a k e P O ( )         -- Build the Portable Object file for the application --
   141 # m a k e P O ( )         -- Build the Portable Object file for the application --
   142 # ^^^^^^^^^^^^^^^
   142 # ^^^^^^^^^^^^^^^
   143 #
   143 #
   144 def makePO(applicationDirectoryPath,  applicationDomain=None, verbose=0) :
   144 def makePO(applicationDirectoryPath,  applicationDomain=None, verbose=0):
   145     """Build the Portable Object Template file for the application.
   145     """Build the Portable Object Template file for the application.
   146 
   146 
   147     makePO builds the .pot file for the application stored inside
   147     makePO builds the .pot file for the application stored inside
   148     a specified directory by running xgettext for all application source
   148     a specified directory by running xgettext for all application source
   149     files.  It finds the name of all files by looking for a file called 'app.fil'.
   149     files.  It finds the name of all files by looking for a file called 'app.fil'.
   202 
   202 
   203     for langCode in languageDict.keys():
   203     for langCode in languageDict.keys():
   204         if langCode == 'en':
   204         if langCode == 'en':
   205             pass
   205             pass
   206         else:
   206         else:
   207             langPOfileName = "%s_%s.po" % (applicationName , langCode)
   207             langPOfileName = "%s_%s.po" % (applicationName, langCode)
   208             if os.path.exists(langPOfileName):
   208             if os.path.exists(langPOfileName):
   209                 cmd = 'msgmerge -s --no-wrap "%s" %s > "%s.new"' % (langPOfileName, fileout, langPOfileName)
   209                 cmd = 'msgmerge -s --no-wrap "%s" %s > "%s.new"' % (langPOfileName, fileout, langPOfileName)
   210                 if verbose: print cmd
   210                 if verbose: print cmd
   211                 os.system(cmd)
   211                 os.system(cmd)
   212     os.chdir(currentDir)
   212     os.chdir(currentDir)
   213 
   213 
   214 
   214 
   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)
   227 
   227 
   228     for langCode in languageDict.keys():
   228     for langCode in languageDict.keys():
   229         if langCode == 'en':
   229         if langCode == 'en':
   230             pass
   230             pass
   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)
   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.
   280 
   280 
   281     for langCode in languageDict.keys():
   281     for langCode in languageDict.keys():
   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 
   296 def printUsage(errorMsg=None) :
   296 def printUsage(errorMsg=None):
   297     """Displays how to use this script from the command line."""
   297     """Displays how to use this script from the command line."""
   298     print """
   298     print """
   299     ##################################################################################
   299     ##################################################################################
   300     #   mki18n :   Make internationalization files.                                  #
   300     #   mki18n :   Make internationalization files.                                  #
   301     #              Uses the GNU gettext system to create PO (Portable Object) files  #
   301     #              Uses the GNU gettext system to create PO (Portable Object) files  #
   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)
   367       return filename
   367       return filename
   368    else:
   368    else:
   369       return os.path.basename(filename)
   369       return os.path.basename(filename)
   370 
   370 
   371 
   371 
   372 def mkdir(directory) :
   372 def mkdir(directory):
   373    """Create a directory (and possibly the entire tree).
   373    """Create a directory (and possibly the entire tree).
   374 
   374 
   375    The os.mkdir() will fail to create a directory if one of the
   375    The os.mkdir() will fail to create a directory if one of the
   376    directory in the specified path does not exist.  mkdir()
   376    directory in the specified path does not exist.  mkdir()
   377    solves this problem.  It creates every intermediate directory
   377    solves this problem.  It creates every intermediate directory
   406       if not os.path.exists(theDir):
   406       if not os.path.exists(theDir):
   407          os.mkdir(theDir)
   407          os.mkdir(theDir)
   408       theDir += '/'
   408       theDir += '/'
   409 
   409 
   410 
   410 
   411 def unixpath(thePath) :
   411 def unixpath(thePath):
   412    r"""Return a path name that contains Unix separator.
   412    r"""Return a path name that contains Unix separator.
   413 
   413 
   414    [Example]
   414    [Example]
   415    >>> unixpath(r"d:\test")
   415    >>> unixpath(r"d:\test")
   416    'd:/test'
   416    'd:/test'