| 1 | """Internationalization and localization support.
|
|---|
| 2 |
|
|---|
| 3 | This module provides internationalization (I18N) and localization (L10N)
|
|---|
| 4 | support for your Python programs by providing an interface to the GNU gettext
|
|---|
| 5 | message catalog library.
|
|---|
| 6 |
|
|---|
| 7 | I18N refers to the operation by which a program is made aware of multiple
|
|---|
| 8 | languages. L10N refers to the adaptation of your program, once
|
|---|
| 9 | internationalized, to the local language and cultural habits.
|
|---|
| 10 |
|
|---|
| 11 | """
|
|---|
| 12 |
|
|---|
| 13 | # This module represents the integration of work, contributions, feedback, and
|
|---|
| 14 | # suggestions from the following people:
|
|---|
| 15 | #
|
|---|
| 16 | # Martin von Loewis, who wrote the initial implementation of the underlying
|
|---|
| 17 | # C-based libintlmodule (later renamed _gettext), along with a skeletal
|
|---|
| 18 | # gettext.py implementation.
|
|---|
| 19 | #
|
|---|
| 20 | # Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule,
|
|---|
| 21 | # which also included a pure-Python implementation to read .mo files if
|
|---|
| 22 | # intlmodule wasn't available.
|
|---|
| 23 | #
|
|---|
| 24 | # James Henstridge, who also wrote a gettext.py module, which has some
|
|---|
| 25 | # interesting, but currently unsupported experimental features: the notion of
|
|---|
| 26 | # a Catalog class and instances, and the ability to add to a catalog file via
|
|---|
| 27 | # a Python API.
|
|---|
| 28 | #
|
|---|
| 29 | # Barry Warsaw integrated these modules, wrote the .install() API and code,
|
|---|
| 30 | # and conformed all C and Python code to Python's coding standards.
|
|---|
| 31 | #
|
|---|
| 32 | # Francois Pinard and Marc-Andre Lemburg also contributed valuably to this
|
|---|
| 33 | # module.
|
|---|
| 34 | #
|
|---|
| 35 | # J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs.
|
|---|
| 36 | #
|
|---|
| 37 | # TODO:
|
|---|
| 38 | # - Lazy loading of .mo files. Currently the entire catalog is loaded into
|
|---|
| 39 | # memory, but that's probably bad for large translated programs. Instead,
|
|---|
| 40 | # the lexical sort of original strings in GNU .mo files should be exploited
|
|---|
| 41 | # to do binary searches and lazy initializations. Or you might want to use
|
|---|
| 42 | # the undocumented double-hash algorithm for .mo files with hash tables, but
|
|---|
| 43 | # you'll need to study the GNU gettext code to do this.
|
|---|
| 44 | #
|
|---|
| 45 | # - Support Solaris .mo file formats. Unfortunately, we've been unable to
|
|---|
| 46 | # find this format documented anywhere.
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | import locale, copy, os, re, struct, sys
|
|---|
| 50 | from errno import ENOENT
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
|
|---|
| 54 | 'find', 'translation', 'install', 'textdomain', 'bindtextdomain',
|
|---|
| 55 | 'dgettext', 'dngettext', 'gettext', 'ngettext',
|
|---|
| 56 | ]
|
|---|
| 57 |
|
|---|
| 58 | _default_localedir = os.path.join(sys.prefix, 'share', 'locale')
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | def test(condition, true, false):
|
|---|
| 62 | """
|
|---|
| 63 | Implements the C expression:
|
|---|
| 64 |
|
|---|
| 65 | condition ? true : false
|
|---|
| 66 |
|
|---|
| 67 | Required to correctly interpret plural forms.
|
|---|
| 68 | """
|
|---|
| 69 | if condition:
|
|---|
| 70 | return true
|
|---|
| 71 | else:
|
|---|
| 72 | return false
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | def c2py(plural):
|
|---|
| 76 | """Gets a C expression as used in PO files for plural forms and returns a
|
|---|
| 77 | Python lambda function that implements an equivalent expression.
|
|---|
| 78 | """
|
|---|
| 79 | # Security check, allow only the "n" identifier
|
|---|
| 80 | try:
|
|---|
| 81 | from cStringIO import StringIO
|
|---|
| 82 | except ImportError:
|
|---|
| 83 | from StringIO import StringIO
|
|---|
| 84 | import token, tokenize
|
|---|
| 85 | tokens = tokenize.generate_tokens(StringIO(plural).readline)
|
|---|
| 86 | try:
|
|---|
| 87 | danger = [x for x in tokens if x[0] == token.NAME and x[1] != 'n']
|
|---|
| 88 | except tokenize.TokenError:
|
|---|
| 89 | raise ValueError, \
|
|---|
| 90 | 'plural forms expression error, maybe unbalanced parenthesis'
|
|---|
| 91 | else:
|
|---|
| 92 | if danger:
|
|---|
| 93 | raise ValueError, 'plural forms expression could be dangerous'
|
|---|
| 94 |
|
|---|
| 95 | # Replace some C operators by their Python equivalents
|
|---|
| 96 | plural = plural.replace('&&', ' and ')
|
|---|
| 97 | plural = plural.replace('||', ' or ')
|
|---|
| 98 |
|
|---|
| 99 | expr = re.compile(r'\!([^=])')
|
|---|
| 100 | plural = expr.sub(' not \\1', plural)
|
|---|
| 101 |
|
|---|
| 102 | # Regular expression and replacement function used to transform
|
|---|
| 103 | # "a?b:c" to "test(a,b,c)".
|
|---|
| 104 | expr = re.compile(r'(.*?)\?(.*?):(.*)')
|
|---|
| 105 | def repl(x):
|
|---|
| 106 | return "test(%s, %s, %s)" % (x.group(1), x.group(2),
|
|---|
| 107 | expr.sub(repl, x.group(3)))
|
|---|
| 108 |
|
|---|
| 109 | # Code to transform the plural expression, taking care of parentheses
|
|---|
| 110 | stack = ['']
|
|---|
| 111 | for c in plural:
|
|---|
| 112 | if c == '(':
|
|---|
| 113 | stack.append('')
|
|---|
|
|---|