Author: ianb
Date: 2007-04-09 13:29:17 -0600 (Mon, 09 Apr 2007)
New Revision: 2501

Added:
   FormEncode/trunk/formencode/i18n/msgfmt.py
Modified:
   FormEncode/trunk/docs/Validator.txt
   FormEncode/trunk/docs/i18n.txt
   FormEncode/trunk/setup.cfg
Log:
Added i18n.txt to setup.cfg.  Integrated Gregor's notes into Validator.txt.  
Added msgfmt.py from the Python distribution (it is references in Gregor's 
notes)

Modified: FormEncode/trunk/docs/Validator.txt
===================================================================
--- FormEncode/trunk/docs/Validator.txt 2007-04-09 15:25:03 UTC (rev 2500)
+++ FormEncode/trunk/docs/Validator.txt 2007-04-09 19:29:17 UTC (rev 2501)
@@ -458,6 +458,82 @@
         raise Invalid(self.message('key', substitution='apples'),
                       value, state)
 
+Localization of Error Messages (i18n)
+-------------------------------------
+
+When a failed validation accurs FormEncode tries to output the error
+message in the appropirate language. For this it uses the standard
+`gettext <http://python.org/doc/current/lib/module-gettext.html>`_
+mechanism of python. To translate the message in the appropirate
+message FormEncode has to find a gettext function that translates the
+string. The language to be translated into and the used domain is
+determined by the found gettext function. To serve a standard
+translation mechanism and to enable custom translations it looks in
+the following order to find a gettext (``_``) function:
+
+1. method of the ``state`` object
+
+2. function ``__builtin__._``.  This function is only used when::
+
+       Validator.use_builtin_gettext == True #True is default
+
+3. formencode builtin ``_stdtrans`` function
+
+   for standalone use of FormEncode. The language to use is determined
+   out of the local system (see gettext documentation). Optionally you
+   can also set the language or the domain explicitly with the
+   function::
+
+       formencode.api.set_stdtranslation(domain="FormEncode", languages=["de"])
+
+   Formencode comes with a Domain ``FormEncode`` and the corresponding
+   messages in the directory
+   ``localedir/language/LC_MESSAGES/FormEncode.mo``
+
+4. Custom gettext function and addtional parameters
+
+   If you use a custom gettext function and you want FormEncode to
+   call your function with additional parameters you can set the
+   dictionary::
+
+       Validators.gettextargs
+
+Available languages
+~~~~~~~~~~~~~~~~~~~
+
+All available languages are distributed with the code. You can see the
+currently available languages in the source under the directory
+``formencode/i18n``.
+
+If your's is not present yet, please consider contributing a
+translation (where ``<lang>`` is you language code)::
+
+    $ svn co http://svn.formencode.org/FormEncode/trunk/``
+    $ cd formencode/i18n
+    $ mkdir <lang>/LC_MESSAGES
+    $ cp FormEncode.pot <lang>/LC_MESSAGES/FormEncode.po
+    $ emacs <lang>/LC_MESSAGES/FormEncode.po # or whatever editor you prefer
+      # make the translation
+    $ python msgfmt.py <lang>/LC_MESSAGES/FormEncode.po
+
+Then test, and send the PO and MO files to [EMAIL PROTECTED]
+
+See also `the Python internationalization documents
+<http://docs.python.org/lib/node738.html>`_.
+
+Optionally you can also add a test of your language to
+``tests/test_i18n.py``.  An Example of a language test::
+
+    ne = formencode.validators.NotEmpty()
+    [...]
+    def test_de():
+        _test_lang("de", u"Bitte einen Wert eingeben")
+
+And the test for your language::
+
+    def test_<lang>():
+        _test_lang("<lang>", u"<translation of Not Empty Text in the language 
<lang>") 
+
 HTTP/HTML Form Input
 --------------------
 

Modified: FormEncode/trunk/docs/i18n.txt
===================================================================
--- FormEncode/trunk/docs/i18n.txt      2007-04-09 15:25:03 UTC (rev 2500)
+++ FormEncode/trunk/docs/i18n.txt      2007-04-09 19:29:17 UTC (rev 2501)
@@ -1,6 +1,8 @@
 FormEncode Internationalization (gettext)
 +++++++++++++++++++++++++++++++++++++++++
 
+:author: Gregor Horvath <gh at gregor-horvath dot com> 2006
+
 There are different translation options available:
 
 Domain "FormEncode" 
@@ -31,4 +33,3 @@
 If no translation mechanism is found a fallback returns the plain string.
 
 
-Gregor Horvath, 2006 [EMAIL PROTECTED]

Added: FormEncode/trunk/formencode/i18n/msgfmt.py
===================================================================
--- FormEncode/trunk/formencode/i18n/msgfmt.py                          (rev 0)
+++ FormEncode/trunk/formencode/i18n/msgfmt.py  2007-04-09 19:29:17 UTC (rev 
2501)
@@ -0,0 +1,203 @@
+#! /usr/bin/python2.4
+# -*- coding: iso-8859-1 -*-
+# Written by Martin v. L�wis <[EMAIL PROTECTED]>
+
+"""Generate binary message catalog from textual translation description.
+
+This program converts a textual Uniforum-style message catalog (.po file) into
+a binary GNU catalog (.mo file).  This is essentially the same function as the
+GNU msgfmt program, however, it is a simpler implementation.
+
+Usage: msgfmt.py [OPTIONS] filename.po
+
+Options:
+    -o file
+    --output-file=file
+        Specify the output file to write to.  If omitted, output will go to a
+        file named filename.mo (based off the input file name).
+
+    -h
+    --help
+        Print this message and exit.
+
+    -V
+    --version
+        Display version information and exit.
+"""
+
+import sys
+import os
+import getopt
+import struct
+import array
+
+__version__ = "1.1"
+
+MESSAGES = {}
+
+
+
+def usage(code, msg=''):
+    print >> sys.stderr, __doc__
+    if msg:
+        print >> sys.stderr, msg
+    sys.exit(code)
+
+
+
+def add(id, str, fuzzy):
+    "Add a non-fuzzy translation to the dictionary."
+    global MESSAGES
+    if not fuzzy and str:
+        MESSAGES[id] = str
+
+
+
+def generate():
+    "Return the generated output."
+    global MESSAGES
+    keys = MESSAGES.keys()
+    # the keys are sorted in the .mo file
+    keys.sort()
+    offsets = []
+    ids = strs = ''
+    for id in keys:
+        # For each string, we need size and file offset.  Each string is NUL
+        # terminated; the NUL does not count into the size.
+        offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
+        ids += id + '\0'
+        strs += MESSAGES[id] + '\0'
+    output = ''
+    # The header is 7 32-bit unsigned integers.  We don't use hash tables, so
+    # the keys start right after the index tables.
+    # translated string.
+    keystart = 7*4+16*len(keys)
+    # and the values start after the keys
+    valuestart = keystart + len(ids)
+    koffsets = []
+    voffsets = []
+    # The string table first has the list of keys, then the list of values.
+    # Each entry has first the size of the string, then the file offset.
+    for o1, l1, o2, l2 in offsets:
+        koffsets += [l1, o1+keystart]
+        voffsets += [l2, o2+valuestart]
+    offsets = koffsets + voffsets
+    output = struct.pack("Iiiiiii",
+                         0x950412deL,       # Magic
+                         0,                 # Version
+                         len(keys),         # # of entries
+                         7*4,               # start of key index
+                         7*4+len(keys)*8,   # start of value index
+                         0, 0)              # size and offset of hash table
+    output += array.array("i", offsets).tostring()
+    output += ids
+    output += strs
+    return output
+
+
+
+def make(filename, outfile):
+    ID = 1
+    STR = 2
+
+    # Compute .mo name from .po name and arguments
+    if filename.endswith('.po'):
+        infile = filename
+    else:
+        infile = filename + '.po'
+    if outfile is None:
+        outfile = os.path.splitext(infile)[0] + '.mo'
+
+    try:
+        lines = open(infile).readlines()
+    except IOError, msg:
+        print >> sys.stderr, msg
+        sys.exit(1)
+
+    section = None
+    fuzzy = 0
+
+    # Parse the catalog
+    lno = 0
+    for l in lines:
+        lno += 1
+        # If we get a comment line after a msgstr, this is a new entry
+        if l[0] == '#' and section == STR:
+            add(msgid, msgstr, fuzzy)
+            section = None
+            fuzzy = 0
+        # Record a fuzzy mark
+        if l[:2] == '#,' and 'fuzzy' in l:
+            fuzzy = 1
+        # Skip comments
+        if l[0] == '#':
+            continue
+        # Now we are in a msgid section, output previous section
+        if l.startswith('msgid'):
+            if section == STR:
+                add(msgid, msgstr, fuzzy)
+            section = ID
+            l = l[5:]
+            msgid = msgstr = ''
+        # Now we are in a msgstr section
+        elif l.startswith('msgstr'):
+            section = STR
+            l = l[6:]
+        # Skip empty lines
+        l = l.strip()
+        if not l:
+            continue
+        # XXX: Does this always follow Python escape semantics?
+        l = eval(l)
+        if section == ID:
+            msgid += l
+        elif section == STR:
+            msgstr += l
+        else:
+            print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
+                  'before:'
+            print >> sys.stderr, l
+            sys.exit(1)
+    # Add last entry
+    if section == STR:
+        add(msgid, msgstr, fuzzy)
+
+    # Compute output
+    output = generate()
+
+    try:
+        open(outfile,"wb").write(output)
+    except IOError,msg:
+        print >> sys.stderr, msg
+
+
+
+def main():
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
+                                   ['help', 'version', 'output-file='])
+    except getopt.error, msg:
+        usage(1, msg)
+
+    outfile = None
+    # parse options
+    for opt, arg in opts:
+        if opt in ('-h', '--help'):
+            usage(0)
+        elif opt in ('-V', '--version'):
+            print >> sys.stderr, "msgfmt.py", __version__
+            sys.exit(0)
+        elif opt in ('-o', '--output-file'):
+            outfile = arg
+    # do it
+    if not args:
+        print >> sys.stderr, 'No input file given'
+        print >> sys.stderr, "Try `msgfmt --help' for more information."
+        return
+
+    for filename in args:
+        make(filename, outfile)
+
+
+if __name__ == '__main__':
+    main()


Property changes on: FormEncode/trunk/formencode/i18n/msgfmt.py
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native

Modified: FormEncode/trunk/setup.cfg
===================================================================
--- FormEncode/trunk/setup.cfg  2007-04-09 15:25:03 UTC (rev 2500)
+++ FormEncode/trunk/setup.cfg  2007-04-09 19:29:17 UTC (rev 2501)
@@ -10,7 +10,7 @@
 docs = docs/index.txt docs/Validator.txt docs/ToDo.txt
        docs/news.txt docs/htmlfill.txt docs/Design.txt
        docs/community.txt docs/download.txt
-       docs/history.txt
+       docs/history.txt docs/i18n.txt
 doc_base = docs/
 dest = docs/html
 modules = formencode


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
FormEncode-CVS mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-cvs

Reply via email to