i18n/mki18n.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 01 Dec 2016 18:30:34 +0300
changeset 1587 b1be42401522
parent 1482 5d4543ee5a5d
child 1624 bc84d659894f
permissions -rw-r--r--
add i18n to PLC status

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