Attachment 'mki18n.py'

Download

   1 #! /usr/bin/env python
   2 # -*- coding: iso-8859-1 -*-
   3 # 
   4 #   PYTHON MODULE:     MKI18N.PY
   5 #                      =========
   6 # 
   7 #   Abstract:         Make Internationalization (i18n) files for an application.
   8 # 
   9 #   Copyright Pierre Rouleau. 2003. Released to public domain.
  10 # 
  11 #   Last update: Thu, June 25, 2020.
  12 # 
  13 #   File: ROUP2003N01::C:/dev/python/mki18n.py
  14 # 
  15 #   RCS $Header: //software/official/MKS/MKS_SI/TV_NT/dev/Python/rcs/mki18n.py 1.5 2003/11/05 19:40:04 PRouleau Exp $
  16 # 
  17 #   Update history:
  18 # 
  19 #   - File created: Saturday, June 7, 2003. by Pierre Rouleau
  20 #   - 10/06/03 rcs : RCS Revision 1.1  2003/06/10 10:06:12  PRouleau
  21 #   - 10/06/03 rcs : RCS Initial revision
  22 #   - 23/08/03 rcs : RCS Revision 1.2  2003/06/10 10:54:27  PRouleau
  23 #   - 23/08/03 P.R.: [code:fix] : The strings encoded in this file are encode in iso-8859-1 format.  Added the encoding
  24 #                    notification to Python to comply with Python's 2.3 PEP 263.
  25 #   - 23/08/03 P.R.: [feature:new] : Added the '-e' switch which is used to force the creation of the empty English .mo file.
  26 #   - 22/10/03 P.R.: [code] : incorporated utility functions in here to make script self sufficient.
  27 #   - 05/11/03 rcs : RCS Revision 1.4  2003/10/22 06:39:31  PRouleau
  28 #   - 05/11/03 P.R.: [code:fix] : included the unixpath() in this file.
  29 #   - 08/11/03 rcs : RCS Revision 1.5  2003/11/05 19:40:04  PRouleau
  30 #   - 25/06/20 rcs : Updated for Phoenix.
  31 #
  32 #   RCS $Log: $
  33 # 
  34 # 
  35 # -----------------------------------------------------------------------------
  36 """                                
  37 mki18n allows you to internationalize your software.  You can use it to 
  38 create the GNU .po files (Portable Object) and the compiled .mo files
  39 (Machine Object).
  40 
  41 mki18n module can be used from the command line or from within a script (see 
  42 the Usage at the end of this page).
  43 
  44     Table of Contents
  45     -----------------
  46     
  47     makePO()             -- Build the Portable Object file for the application --
  48     catPO()              -- Concatenate one or several PO files with the application domain files. --
  49     makeMO()             -- Compile the Portable Object files into the Machine Object stored in the right location. --
  50     printUsage           -- Displays how to use this script from the command line --
  51 
  52     Scriptexecution      -- Runs when invoked from the command line --
  53 
  54 
  55 NOTE:  this module uses GNU gettext utilities.
  56 
  57 You can get the gettext tools from the following sites:
  58 
  59    - `GNU FTP site for gettetx`_ where several versions (0.10.40, 0.11.2, 0.11.5 and 0.12.1) are available.
  60      Note  that you need to use `GNU libiconv`_ to use this. Get it from the `GNU
  61      libiconv  ftp site`_ and get version 1.9.1 or later. Get the Windows .ZIP
  62      files and install the packages inside c:/gnu. All binaries will be stored
  63      inside  c:/gnu/bin.  Just  put c:/gnu/bin inside your PATH. You will need
  64      the following files: 
  65 
  66       - `gettext-runtime-0.12.1.bin.woe32.zip`_ 
  67       - `gettext-tools-0.12.1.bin.woe32.zip`_
  68       - `libiconv-1.9.1.bin.woe32.zip`_ 
  69 
  70 
  71 .. _GNU libiconv:                            http://www.gnu.org/software/libiconv/
  72 .. _GNU libiconv ftp site:                   http://www.ibiblio.org/pub/gnu/libiconv/
  73 .. _gettext-runtime-0.12.1.bin.woe32.zip:    ftp://ftp.gnu.org/gnu/gettext/gettext-runtime-0.12.1.bin.woe32.zip           
  74 .. _gettext-tools-0.12.1.bin.woe32.zip:      ftp://ftp.gnu.org/gnu/gettext/gettext-tools-0.12.1.bin.woe32.zip 
  75 .. _libiconv-1.9.1.bin.woe32.zip:            http://www.ibiblio.org/pub/gnu/libiconv/libiconv-1.9.1.bin.woe32.zip
  76 
  77 """
  78 # -----------------------------------------------------------------------------
  79 # Module Import
  80 # -------------
  81 # 
  82 import os
  83 import sys
  84 import wx
  85 # -----------------------------------------------------------------------------
  86 # Global variables
  87 # ----------------
  88 #
  89 
  90 __author__ = "Pierre Rouleau"
  91 __version__= "$Revision: 1.5 $"
  92 
  93 # -----------------------------------------------------------------------------
  94 
  95 def getlanguageDict():
  96     languageDict = {}
  97     
  98     for lang in [x for x in dir(wx) if x.startswith("LANGUAGE")]:
  99         i = wx.Locale(wx.LANGUAGE_DEFAULT).GetLanguageInfo(getattr(wx, lang))
 100         if i:
 101             languageDict[i.CanonicalName] = i.Description
 102 
 103     return languageDict
 104 
 105 # -----------------------------------------------------------------------------
 106 # m a k e P O ( )         -- Build the Portable Object file for the application --
 107 # ^^^^^^^^^^^^^^^
 108 #
 109 def makePO(applicationDirectoryPath,  applicationDomain=None, verbose=0) :
 110     """Build the Portable Object Template file for the application.
 111 
 112     makePO builds the .pot file for the application stored inside 
 113     a specified directory by running xgettext for all application source 
 114     files.  It finds the name of all files by looking for a file called 'app.fil'. 
 115     If this file does not exists, makePo raises an IOError exception.
 116     By default the application domain (the application
 117     name) is the same as the directory name but it can be overridden by the
 118     'applicationDomain' argument.
 119 
 120     makePO always creates a new file called messages.pot.  If it finds files 
 121     of the form app_xx.po where 'app' is the application name and 'xx' is one 
 122     of the ISO 639 two-letter language codes, makePO resynchronizes those 
 123     files with the latest extracted strings (now contained in messages.pot). 
 124     This process updates all line location number in the language-specific
 125     .po files and may also create new entries for translation (or comment out 
 126     some).  The .po file is not changed, instead a new file is created with 
 127     the .new extension appended to the name of the .po file.
 128 
 129     By default the function does not display what it is doing.  Set the 
 130     verbose argument to 1 to force it to print its commands.
 131     """
 132 
 133     if applicationDomain is None:
 134         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
 135     else:
 136         applicationName = applicationDomain
 137     currentDir = os.getcwd()
 138     os.chdir(applicationDirectoryPath)                    
 139     if not os.path.exists('app.fil'):
 140         raise IOError(2,'No module file: app.fil')
 141 
 142     # Steps:                                  
 143     #  Use xgettext to parse all application modules
 144     #  The following switches are used:
 145     #  
 146     #   -s                          : sort output by string content (easier to use when we need to merge several .po files)
 147     #   --files-from=app.fil        : The list of files is taken from the file: app.fil
 148     #   --output=                   : specifies the name of the output file (using a .pot extension)
 149     cmd = 'xgettext -s --no-wrap --files-from=app.fil --output=messages.pot'
 150     if verbose: print( cmd)
 151     os.system(cmd)                                                
 152 
 153     languageDict = getlanguageDict()
 154 
 155     for langCode in languageDict.keys():
 156         if langCode == 'en':
 157             pass
 158         else:
 159             langPOfileName = "%s_%s.po" % (applicationName , langCode)
 160             if os.path.exists(langPOfileName):
 161                 cmd = 'msgmerge -s --no-wrap "%s" messages.pot > "%s.new"' % (langPOfileName, langPOfileName)
 162                 if verbose: print( cmd)
 163                 os.system(cmd)
 164     os.chdir(currentDir)
 165 
 166 # -----------------------------------------------------------------------------
 167 # c a t P O ( )         -- Concatenate one or several PO files with the application domain files. --
 168 # ^^^^^^^^^^^^^
 169 #
 170 def catPO(applicationDirectoryPath, listOf_extraPo, applicationDomain=None, targetDir=None, verbose=0) :
 171     """Concatenate one or several PO files with the application domain files.
 172     """
 173 
 174     if applicationDomain is None:
 175         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
 176     else:
 177         applicationName = applicationDomain
 178     currentDir = os.getcwd()
 179     os.chdir(applicationDirectoryPath)
 180 
 181     languageDict = getlanguageDict()
 182 
 183     for langCode in languageDict.keys():
 184         if langCode == 'en':
 185             pass
 186         else:
 187             langPOfileName = "%s_%s.po" % (applicationName , langCode)
 188             if os.path.exists(langPOfileName):
 189                 fileList = ''
 190                 for fileName in listOf_extraPo:
 191                     fileList += ("%s_%s.po " % (fileName,langCode))
 192                 cmd = "msgcat -s --no-wrap %s %s > %s.cat" % (langPOfileName, fileList, langPOfileName)
 193                 if verbose: print( cmd)
 194                 os.system(cmd)
 195                 if targetDir is None:
 196                     pass
 197                 else:
 198                     mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir,langCode)
 199                     cmd = "msgfmt --output-file=%s/%s.mo %s_%s.po.cat" % (mo_targetDir,applicationName,applicationName,langCode)
 200                     if verbose: print( cmd)
 201                     os.system(cmd)
 202     os.chdir(currentDir)
 203 
 204 # -----------------------------------------------------------------------------
 205 # m a k e M O ( )         -- Compile the Portable Object files into the Machine Object stored in the right location. --
 206 # ^^^^^^^^^^^^^^^
 207 # 
 208 def makeMO(applicationDirectoryPath,targetDir='./locale',applicationDomain=None, verbose=0, forceEnglish=0) :
 209     """Compile the Portable Object files into the Machine Object stored in the right location.
 210 
 211     makeMO converts all translated language-specific PO files located inside 
 212     the  application directory into the binary .MO files stored inside the 
 213     LC_MESSAGES sub-directory for the found locale files.
 214 
 215     makeMO searches for all files that have a name of the form 'app_xx.po' 
 216     inside the application directory specified by the first argument.  The 
 217     'app' is the application domain name (that can be specified by the 
 218     applicationDomain argument or is taken from the directory name). The 'xx' 
 219     corresponds to one of the ISO 639 two-letter language codes.
 220 
 221     makeMo stores the resulting files inside a sub-directory of `targetDir`
 222     called xx/LC_MESSAGES where 'xx' corresponds to the 2-letter language
 223     code.
 224     """
 225     if targetDir is None:
 226         targetDir = './locale'
 227     if verbose:
 228         print( "Target directory for .mo files is: %s" % targetDir)
 229 
 230     if applicationDomain is None:
 231         applicationName = fileBaseOf(applicationDirectoryPath,withPath=0)
 232     else:
 233         applicationName = applicationDomain
 234     currentDir = os.getcwd()
 235     os.chdir(applicationDirectoryPath)
 236 
 237     languageDict = getlanguageDict()
 238 
 239     for langCode in languageDict.keys():
 240         if (langCode == 'en') and (forceEnglish==0):
 241             pass
 242         else:
 243             langPOfileName = "%s_%s.po" % (applicationName , langCode)
 244             if os.path.exists(langPOfileName):
 245                 mo_targetDir = "%s/%s/LC_MESSAGES" % (targetDir,langCode) 
 246                 if not os.path.exists(mo_targetDir):
 247                     mkdir(mo_targetDir)
 248                 cmd = 'msgfmt --output-file="%s/%s.mo" "%s_%s.po"' % (mo_targetDir,applicationName,applicationName,langCode)
 249                 if verbose: print( cmd)
 250                 os.system(cmd)
 251     os.chdir(currentDir)
 252    
 253 # -----------------------------------------------------------------------------
 254 # p r i n t U s a g e         -- Displays how to use this script from the command line --
 255 # ^^^^^^^^^^^^^^^^^^^
 256 #
 257 def printUsage(errorMsg=None) :
 258     """Displays how to use this script from the command line."""
 259     print( """
 260     ##################################################################################
 261     #   mki18n :   Make internationalization files.                                  #
 262     #              Uses the GNU gettext system to create PO (Portable Object) files  #
 263     #              from source code, coimpile PO into MO (Machine Object) files.     #
 264     #              Supports C,C++,Python source files.                               #
 265     #                                                                                #
 266     #   Usage: mki18n {OPTION} [appDirPath]                                          #
 267     #                                                                                #
 268     #   Options:                                                                     #
 269     #     -e               : When -m is used, forces English .mo file creation       #
 270     #     -h               : prints this help                                        #
 271     #     -m               : make MO from existing PO files                          #
 272     #     -p               : make PO, update PO files: Creates a new messages.pot    #
 273     #                        file. Creates a dom_xx.po.new for every existing        #
 274     #                        language specific .po file. ('xx' stands for the ISO639 #
 275     #                        two-letter language code and 'dom' stands for the       #
 276     #                        application domain name).  mki18n requires that you     #
 277     #                        write a 'app.fil' file  which contains the list of all  #
 278     #                        source code to parse.                                   #
 279     #     -v               : verbose (prints comments while running)                 #
 280     #     --domain=appName : specifies the application domain name.  By default      #
 281     #                        the directory name is used.                             #
 282     #     --moTarget=dir : specifies the directory where .mo files are stored.       #
 283     #                      If not specified, the target is './locale'                #
 284     #                                                                                #
 285     #   You must specify one of the -p or -m option to perform the work.  You can    #
 286     #   specify the path of the target application.  If you leave it out mki18n      #
 287     #   will use the current directory as the application main directory.            #        
 288     #                                                                                #
 289     ##################################################################################""")
 290     if errorMsg:
 291         print( "\n   ERROR: %s" % errorMsg)
 292 
 293 # -----------------------------------------------------------------------------
 294 # f i l e B a s e O f ( )         -- Return base name of filename --
 295 # ^^^^^^^^^^^^^^^^^^^^^^^
 296 # 
 297 def fileBaseOf(filename,withPath=0) :
 298    """fileBaseOf(filename,withPath) ---> string
 299 
 300    Return base name of filename.  The returned string never includes the extension.
 301    Use os.path.basename() to return the basename with the extension.  The 
 302    second argument is optional.  If specified and if set to 'true' (non zero) 
 303    the string returned contains the full path of the file name.  Otherwise the 
 304    path is excluded.
 305 
 306    [Example]
 307    >>> fn = 'd:/dev/telepath/tvapp/code/test.html'
 308    >>> fileBaseOf(fn)
 309    'test'
 310    >>> fileBaseOf(fn)
 311    'test'
 312    >>> fileBaseOf(fn,1)
 313    'd:/dev/telepath/tvapp/code/test'
 314    >>> fileBaseOf(fn,0)
 315    'test'
 316    >>> fn = 'abcdef'
 317    >>> fileBaseOf(fn)
 318    'abcdef'
 319    >>> fileBaseOf(fn,1)
 320    'abcdef'
 321    >>> fn = "abcdef."
 322    >>> fileBaseOf(fn)
 323    'abcdef'
 324    >>> fileBaseOf(fn,1)
 325    'abcdef'
 326    """            
 327    pos = filename.rfind('.')             
 328    if pos > 0:
 329       filename = filename[:pos]
 330    if withPath:
 331       return filename
 332    else:
 333       return os.path.basename(filename)
 334 # -----------------------------------------------------------------------------
 335 # m k d i r ( )         -- Create a directory (and possibly the entire tree) --
 336 # ^^^^^^^^^^^^^
 337 # 
 338 def mkdir(directory) :
 339    """Create a directory (and possibly the entire tree).
 340 
 341    The os.mkdir() will fail to create a directory if one of the
 342    directory in the specified path does not exist.  mkdir()
 343    solves this problem.  It creates every intermediate directory
 344    required to create the final path. Under Unix, the function 
 345    only supports forward slash separator, but under Windows and MacOS
 346    the function supports the forward slash and the OS separator (backslash
 347    under windows).
 348    """ 
 349 
 350    # translate the path separators
 351    directory = unixpath(directory)
 352    # build a list of all directory elements
 353    aList = filter(lambda x: len(x)>0, directory.split('/'))
 354    theLen = len(aList)                     
 355    # if the first element is a Windows-style disk drive
 356    # concatenate it with the first directory
 357    if aList[0].endswith(':'):
 358       if theLen > 1:
 359          aList[1] = aList[0] + '/' + aList[1]
 360          del aList[0]      
 361          theLen -= 1         
 362    # if the original directory starts at root,
 363    # make sure the first element of the list 
 364    # starts at root too
 365    if directory[0] == '/':     
 366       aList[0] = '/' + aList[0]
 367    # Now iterate through the list, check if the 
 368    # directory exists and if not create it
 369    theDir = ''
 370    for i in range(theLen):
 371       theDir += aList[i]
 372       if not os.path.exists(theDir):
 373          os.mkdir(theDir)
 374       theDir += '/'   
 375       
 376 # -----------------------------------------------------------------------------
 377 # u n i x p a t h ( )         -- Return a path name that contains Unix separator. --
 378 # ^^^^^^^^^^^^^^^^^^^
 379 # 
 380 def unixpath(thePath) :
 381    r"""Return a path name that contains Unix separator.
 382 
 383    [Example]
 384    >>> unixpath(r"d:\test")
 385    'd:/test'
 386    >>> unixpath("d:/test/file.txt")
 387    'd:/test/file.txt'
 388    >>> 
 389    """
 390    thePath = os.path.normpath(thePath)
 391    if os.sep == '/':
 392       return thePath
 393    else:
 394       return thePath.replace(os.sep,'/')
 395 
 396 # ----------------------------------------------------------------------------- 
 397 
 398 # S c r i p t   e x e c u t i o n               -- Runs when invoked from the command line --
 399 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 400 # 
 401 if __name__ == "__main__":
 402     
 403     
 404     app = wx.App(False)
 405     app.MainLoop()    
 406 
 407     import getopt     # command line parsing
 408     argc = len(sys.argv)
 409     if argc == 1:
 410         printUsage('Missing argument: specify at least one of -m or -p (or both).')
 411         sys.exit(1)
 412     # If there is some arguments, parse the command line
 413     validOptions     = "ehmpv"
 414     validLongOptions = ['domain=', 'moTarget=']             
 415     option = {}
 416     option['forceEnglish'] = 0
 417     option['mo'] = 0
 418     option['po'] = 0        
 419     option['verbose'] = 0
 420     option['domain'] = None
 421     option['moTarget'] = None
 422     try:
 423         optionList,pargs = getopt.getopt(sys.argv[1:],validOptions,validLongOptions)
 424     except (getopt.GetoptError, e):
 425         printUsage(e[0])
 426         sys.exit(1)       
 427     for (opt,val) in optionList:
 428         if  (opt == '-h'):    
 429             printUsage()
 430             sys.exit(0) 
 431         elif (opt == '-e'):         option['forceEnglish'] = 1
 432         elif (opt == '-m'):         option['mo'] = 1
 433         elif (opt == '-p'):         option['po'] = 1
 434         elif (opt == '-v'):         option['verbose'] = 1
 435         elif (opt == '--domain'):   option['domain'] = val
 436         elif (opt == '--moTarget'): option['moTarget'] = val
 437     if len(pargs) == 0:
 438         appDirPath = os.getcwd()
 439         if option['verbose']:
 440             print( "No project directory given. Using current one:  %s" % appDirPath)
 441     elif len(pargs) == 1:
 442         appDirPath = pargs[0]
 443     else:
 444         printUsage('Too many arguments (%u).  Use double quotes if you have space in directory name' % len(pargs))
 445         sys.exit(1)
 446     if option['domain'] is None:
 447         # If no domain specified, use the name of the target directory
 448         option['domain'] = fileBaseOf(appDirPath)
 449     if option['verbose']:
 450         print( "Application domain used is: '%s'" % option['domain'])
 451     if option['po']:
 452         try:
 453             makePO(appDirPath,option['domain'],option['verbose'])
 454         except (IOError, e):
 455             printUsage(e[1] + '\n   You must write a file app.fil that contains the list of all files to parse.')
 456     if option['mo']:
 457         makeMO(appDirPath,option['moTarget'],option['domain'],option['verbose'],option['forceEnglish'])
 458     sys.exit(1)            
 459             
 460 
 461 # -----------------------------------------------------------------------------

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-10-04 09:16:03, 28.7 KB) [[attachment:i18n.html]]
  • [get | view] (2020-06-26 00:20:26, 20.0 KB) [[attachment:mki18n.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.