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