annotate Lib/gettext.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents ea1d2050d7da
children b246ca033e3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 """Internationalization and localization support.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
2
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 This module provides internationalization (I18N) and localization (L10N)
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 support for your Python programs by providing an interface to the GNU gettext
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
5 message catalog library.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
6
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
7 I18N refers to the operation by which a program is made aware of multiple
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
8 languages. L10N refers to the adaptation of your program, once
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
9 internationalized, to the local language and cultural habits.
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
10
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
11 """
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
12
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
13 # This module represents the integration of work, contributions, feedback, and
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
14 # suggestions from the following people:
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
16 # Martin von Loewis, who wrote the initial implementation of the underlying
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
17 # C-based libintlmodule (later renamed _gettext), along with a skeletal
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
18 # gettext.py implementation.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
19 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 # Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule,
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
21 # which also included a pure-Python implementation to read .mo files if
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
22 # intlmodule wasn't available.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
24 # James Henstridge, who also wrote a gettext.py module, which has some
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
25 # interesting, but currently unsupported experimental features: the notion of
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
26 # a Catalog class and instances, and the ability to add to a catalog file via
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
27 # a Python API.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
28 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
29 # Barry Warsaw integrated these modules, wrote the .install() API and code,
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
30 # and conformed all C and Python code to Python's coding standards.
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
31 #
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
32 # Francois Pinard and Marc-Andre Lemburg also contributed valuably to this
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
33 # module.
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
34 #
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
35 # J. David Ibanez implemented plural forms.
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
36 #
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
37 # TODO:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
38 # - Lazy loading of .mo files. Currently the entire catalog is loaded into
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
39 # memory, but that's probably bad for large translated programs. Instead,
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
40 # the lexical sort of original strings in GNU .mo files should be exploited
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
41 # to do binary searches and lazy initializations. Or you might want to use
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
42 # the undocumented double-hash algorithm for .mo files with hash tables, but
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
43 # you'll need to study the GNU gettext code to do this.
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
44 #
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
45 # - Support Solaris .mo file formats. Unfortunately, we've been unable to
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
46 # find this format documented anywhere.
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
47
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
48
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
49 import copy, os, re, struct, sys
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
50 from errno import ENOENT
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
51
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
52
16160
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
53 __all__ = ["bindtextdomain","textdomain","gettext","dgettext",
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
54 "find","translation","install","Catalog"]
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
55
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
56 _default_localedir = os.path.join(sys.prefix, 'share', 'locale')
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
57
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
58
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
59 def test(condition, true, false):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
60 """
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
61 Implements the C expression:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
62
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
63 condition ? true : false
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
64
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
65 Required to correctly interpret plural forms.
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
66 """
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
67 if condition:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
68 return true
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
69 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
70 return false
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
71
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
72
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
73 def c2py(plural):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
74 """
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
75 Gets a C expression as used in PO files for plural forms and
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
76 returns a Python lambda function that implements an equivalent
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
77 expression.
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
78 """
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
79 # Security check, allow only the "n" identifier
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
80 from StringIO import StringIO
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
81 import token, tokenize
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
82 tokens = tokenize.generate_tokens(StringIO(plural).readline)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
83 danger = [ x for x in tokens if x[0] == token.NAME and x[1] != 'n' ]
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
84 if danger:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
85 raise ValueError, 'dangerous expression'
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
86
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
87 # Replace some C operators by their Python equivalents
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
88 plural = plural.replace('&&', ' and ')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
89 plural = plural.replace('||', ' or ')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
90
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
91 expr = re.compile(r'\![^=]')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
92 plural = expr.sub(' not ', plural)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
93
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
94 # Regular expression and replacement function used to transform
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
95 # "a?b:c" to "test(a,b,c)".
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
96 expr = re.compile(r'(.*?)\?(.*?):(.*)')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
97 def repl(x):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
98 return "test(%s, %s, %s)" % (x.group(1), x.group(2),
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
99 expr.sub(repl, x.group(3)))
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
100
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
101 # Code to transform the plural expression, taking care of parentheses
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
102 stack = ['']
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
103 for c in plural:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
104 if c == '(':
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
105 stack.append('')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
106 elif c == ')':
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
107 if len(stack) == 0:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
108 raise ValueError, 'unbalanced parenthesis in plural form'
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
109 s = expr.sub(repl, stack.pop())
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
110 stack[-1] += '(%s)' % s
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
111 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
112 stack[-1] += c
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
113 plural = expr.sub(repl, stack.pop())
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
114
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
115 return eval('lambda n: int(%s)' % plural)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
116
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
117
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
118
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
119 def _expand_lang(locale):
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
120 from locale import normalize
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
121 locale = normalize(locale)
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
122 COMPONENT_CODESET = 1 << 0
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
123 COMPONENT_TERRITORY = 1 << 1
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
124 COMPONENT_MODIFIER = 1 << 2
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
125 # split up the locale into its base components
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
126 mask = 0
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
127 pos = locale.find('@')
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
128 if pos >= 0:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
129 modifier = locale[pos:]
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
130 locale = locale[:pos]
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
131 mask |= COMPONENT_MODIFIER
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
132 else:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
133 modifier = ''
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
134 pos = locale.find('.')
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
135 if pos >= 0:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
136 codeset = locale[pos:]
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
137 locale = locale[:pos]
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
138 mask |= COMPONENT_CODESET
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
139 else:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
140 codeset = ''
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
141 pos = locale.find('_')
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
142 if pos >= 0:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
143 territory = locale[pos:]
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
144 locale = locale[:pos]
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
145 mask |= COMPONENT_TERRITORY
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
146 else:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
147 territory = ''
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
148 language = locale
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
149 ret = []
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
150 for i in range(mask+1):
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
151 if not (i & ~mask): # if all components for this combo exist ...
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
152 val = language
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
153 if i & COMPONENT_TERRITORY: val += territory
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
154 if i & COMPONENT_CODESET: val += codeset
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
155 if i & COMPONENT_MODIFIER: val += modifier
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
156 ret.append(val)
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
157 ret.reverse()
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
158 return ret
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
159
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
160
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
161
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
162 class NullTranslations:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
163 def __init__(self, fp=None):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
164 self._info = {}
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
165 self._charset = None
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
166 self._fallback = None
23612
3caee2958387 Replace boolean test with is None
Raymond Hettinger <python@rcn.com>
parents: 21704
diff changeset
167 if fp is not None:
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
168 self._parse(fp)
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
169
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
170 def _parse(self, fp):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
171 pass
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
172
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
173 def add_fallback(self, fallback):
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
174 if self._fallback:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
175 self._fallback.add_fallback(fallback)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
176 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
177 self._fallback = fallback
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
178
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
179 def gettext(self, message):
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
180 if self._fallback:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
181 return self._fallback.gettext(message)
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
182 return message
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
183
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
184 def ngettext(self, msgid1, msgid2, n):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
185 if self._fallback:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
186 return self._fallback.ngettext(msgid1, msgid2, n)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
187 if n == 1:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
188 return msgid1
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
189 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
190 return msgid2
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
191
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
192 def ugettext(self, message):
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
193 if self._fallback:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
194 return self._fallback.ugettext(message)
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
195 return unicode(message)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
196
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
197 def ungettext(self, msgid1, msgid2, n):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
198 if self._fallback:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
199 return self._fallback.ungettext(msgid1, msgid2, n)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
200 if n == 1:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
201 return unicode(msgid1)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
202 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
203 return unicode(msgid2)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
204
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
205 def info(self):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
206 return self._info
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
207
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
208 def charset(self):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
209 return self._charset
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
210
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
211 def install(self, unicode=0):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
212 import __builtin__
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
213 __builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
214
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
215
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
216 class GNUTranslations(NullTranslations):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
217 # Magic number of .mo files
24812
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
218 LE_MAGIC = 0x950412deL
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
219 BE_MAGIC = 0xde120495L
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
220
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
221 def _parse(self, fp):
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
222 """Override this method to support alternative .mo formats."""
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
223 unpack = struct.unpack
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
224 filename = getattr(fp, 'name', '')
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
225 # Parse the .mo file header, which consists of 5 little endian 32
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
226 # bit words.
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
227 self._catalog = catalog = {}
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
228 buf = fp.read()
14317
2dcfad3b2115 GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents: 14292
diff changeset
229 buflen = len(buf)
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
230 # Are we big endian or little endian?
24812
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
231 magic = unpack('<I', buf[:4])[0]
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
232 if magic == self.LE_MAGIC:
24812
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
233 version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
234 ii = '<II'
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
235 elif magic == self.BE_MAGIC:
24812
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
236 version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
237 ii = '>II'
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
238 else:
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
239 raise IOError(0, 'Bad magic number', filename)
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
240 # Now put all messages from the .mo file buffer into the catalog
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
241 # dictionary.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
242 for i in xrange(0, msgcount):
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
243 mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
24812
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
244 mend = moff + mlen
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
245 tlen, toff = unpack(ii, buf[transidx:transidx+8])
24812
74ed457208c1 Patch by Tim to shut up the compiler's DeprecationWarnings on the
Barry Warsaw <barry@python.org>
parents: 23612
diff changeset
246 tend = toff + tlen
14317
2dcfad3b2115 GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents: 14292
diff changeset
247 if mend < buflen and tend < buflen:
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
248 msg = buf[moff:mend]
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
249 tmsg = buf[toff:tend]
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
250 if msg.find('\x00') >= 0:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
251 # Plural forms
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
252 msgid1, msgid2 = msg.split('\x00')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
253 tmsg = tmsg.split('\x00')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
254 for i in range(len(tmsg)):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
255 catalog[(msgid1, i)] = tmsg[i]
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
256 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
257 catalog[msg] = tmsg
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
258 else:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
259 raise IOError(0, 'File is corrupt', filename)
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
260 # See if we're looking at GNU .mo conventions for metadata
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
261 if mlen == 0 and tmsg.lower().startswith('project-id-version:'):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
262 # Catalog description
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
263 for item in tmsg.split('\n'):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
264 item = item.strip()
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
265 if not item:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
266 continue
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
267 k, v = item.split(':', 1)
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
268 k = k.strip().lower()
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
269 v = v.strip()
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
270 self._info[k] = v
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
271 if k == 'content-type':
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
272 self._charset = v.split('charset=')[1]
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
273 elif k == 'plural-forms':
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
274 v = v.split(';')
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
275 ## nplurals = v[0].split('nplurals=')[1]
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
276 ## nplurals = int(nplurals.strip())
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
277 plural = v[1].split('plural=')[1]
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
278 self.plural = c2py(plural)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
279 # advance to next entry in the seek tables
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
280 masteridx += 8
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
281 transidx += 8
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
282
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
283 def gettext(self, message):
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
284 try:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
285 return self._catalog[message]
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
286 except KeyError:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
287 if self._fallback:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
288 return self._fallback.gettext(message)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
289 return message
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
290
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
291
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
292 def ngettext(self, msgid1, msgid2, n):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
293 try:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
294 return self._catalog[(msgid1, self.plural(n))]
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
295 except KeyError:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
296 if self._fallback:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
297 return self._fallback.ngettext(msgid1, msgid2, n)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
298 if n == 1:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
299 return msgid1
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
300 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
301 return msgid2
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
302
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
303
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
304 def ugettext(self, message):
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
305 try:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
306 tmsg = self._catalog[message]
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
307 except KeyError:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
308 if self._fallback:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
309 return self._fallback.ugettext(message)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
310 tmsg = message
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
311 return unicode(tmsg, self._charset)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
312
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
313
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
314 def ungettext(self, msgid1, msgid2, n):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
315 try:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
316 tmsg = self._catalog[(msgid1, self.plural(n))]
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
317 except KeyError:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
318 if self._fallback:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
319 return self._fallback.ungettext(msgid1, msgid2, n)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
320 if n == 1:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
321 tmsg = msgid1
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
322 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
323 tmsg = msgid2
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
324 return unicode(tmsg, self._charset)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
325
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
326
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
327 # Locate a .mo file using the gettext strategy
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
328 def find(domain, localedir=None, languages=None, all=0):
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
329 # Get some reasonable defaults for arguments that were not supplied
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
330 if localedir is None:
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
331 localedir = _default_localedir
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
332 if languages is None:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
333 languages = []
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
334 for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
335 val = os.environ.get(envar)
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
336 if val:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
337 languages = val.split(':')
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
338 break
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
339 if 'C' not in languages:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
340 languages.append('C')
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
341 # now normalize and expand the languages
15244
71ea2bb01889 find(): Application of (slightly modified) SF patch #101928 by Ulf
Barry Warsaw <barry@python.org>
parents: 15035
diff changeset
342 nelangs = []
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
343 for lang in languages:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
344 for nelang in _expand_lang(lang):
15244
71ea2bb01889 find(): Application of (slightly modified) SF patch #101928 by Ulf
Barry Warsaw <barry@python.org>
parents: 15035
diff changeset
345 if nelang not in nelangs:
71ea2bb01889 find(): Application of (slightly modified) SF patch #101928 by Ulf
Barry Warsaw <barry@python.org>
parents: 15035
diff changeset
346 nelangs.append(nelang)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
347 # select a language
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
348 if all:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
349 result = []
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
350 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
351 result = None
15244
71ea2bb01889 find(): Application of (slightly modified) SF patch #101928 by Ulf
Barry Warsaw <barry@python.org>
parents: 15035
diff changeset
352 for lang in nelangs:
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
353 if lang == 'C':
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
354 break
14192
9bf159ff4912 Group consensus is that supporting alternative locale categories is
Barry Warsaw <barry@python.org>
parents: 14187
diff changeset
355 mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
356 if os.path.exists(mofile):
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
357 if all:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
358 result.append(mofile)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
359 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
360 return mofile
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
361 return result
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
362
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
363
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
364
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
365 # a mapping between absolute .mo file path and Translation object
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
366 _translations = {}
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
367
21702
48ecd6061e88 Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents: 16160
diff changeset
368 def translation(domain, localedir=None, languages=None,
48ecd6061e88 Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents: 16160
diff changeset
369 class_=None, fallback=0):
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
370 if class_ is None:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
371 class_ = GNUTranslations
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
372 mofiles = find(domain, localedir, languages, all=1)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
373 if len(mofiles)==0:
21702
48ecd6061e88 Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents: 16160
diff changeset
374 if fallback:
48ecd6061e88 Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents: 16160
diff changeset
375 return NullTranslations()
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
376 raise IOError(ENOENT, 'No translation file found for domain', domain)
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
377 # TBD: do we need to worry about the file pointer getting collected?
15035
894dca225d24 translation(): Minor optimization patch which avoids instantiating the
Barry Warsaw <barry@python.org>
parents: 14326
diff changeset
378 # Avoid opening, reading, and parsing the .mo file after it's been done
894dca225d24 translation(): Minor optimization patch which avoids instantiating the
Barry Warsaw <barry@python.org>
parents: 14326
diff changeset
379 # once.
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
380 result = None
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
381 for mofile in mofiles:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
382 key = os.path.abspath(mofile)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
383 t = _translations.get(key)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
384 if t is None:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
385 t = _translations.setdefault(key, class_(open(mofile, 'rb')))
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
386 # Copy the translation object to allow setting fallbacks.
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
387 # All other instance data is shared with the cached object.
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
388 t = copy.copy(t)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
389 if result is None:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
390 result = t
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
391 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
392 result.add_fallback(t)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
393 return result
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
394
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
395
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
396 def install(domain, localedir=None, unicode=0):
21702
48ecd6061e88 Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents: 16160
diff changeset
397 translation(domain, localedir, fallback=1).install(unicode)
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
398
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
399
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
400
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
401 # a mapping b/w domains and locale directories
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
402 _localedirs = {}
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
403 # current global domain, `messages' used for compatibility w/ GNU gettext
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
404 _current_domain = 'messages'
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
405
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
406
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
407 def textdomain(domain=None):
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
408 global _current_domain
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
409 if domain is not None:
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
410 _current_domain = domain
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
411 return _current_domain
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
412
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
413
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
414 def bindtextdomain(domain, localedir=None):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
415 global _localedirs
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
416 if localedir is not None:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
417 _localedirs[domain] = localedir
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
418 return _localedirs.get(domain, _default_localedir)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
419
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
420
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
421 def dgettext(domain, message):
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
422 try:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
423 t = translation(domain, _localedirs.get(domain, None))
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
424 except IOError:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
425 return message
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
426 return t.gettext(message)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
427
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
428
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
429 def dngettext(domain, msgid1, msgid2, n):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
430 try:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
431 t = translation(domain, _localedirs.get(domain, None))
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
432 except IOError:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
433 if n == 1:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
434 return msgid1
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
435 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
436 return msgid2
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
437 return t.ngettext(msgid1, msgid2, n)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
438
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
439
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
440 def gettext(message):
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
441 return dgettext(_current_domain, message)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
442
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
443
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
444 def ngettext(msgid1, msgid2, n):
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
445 return dngettext(_current_domain, msgid1, msgid2, n)
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
446
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
447
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
448 # dcgettext() has been deemed unnecessary and is not implemented.
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
449
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
450 # James Henstridge's Catalog constructor from GNOME gettext. Documented usage
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
451 # was:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
452 #
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
453 # import gettext
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
454 # cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR)
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
455 # _ = cat.gettext
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
456 # print _('Hello World')
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
457
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
458 # The resulting catalog object currently don't support access through a
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
459 # dictionary API, which was supported (but apparently unused) in GNOME
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
460 # gettext.
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
461
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
462 Catalog = translation