Mercurial > cpython-lru_cache-weakref
annotate Lib/gettext.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | ee5350f1d0c2 |
| children |
| 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 # |
|
28256
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
35 # J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs. |
|
26327
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 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
49 import locale, 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 |
|
28582
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
53 __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', |
|
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
54 'find', 'translation', 'install', 'textdomain', 'bindtextdomain', |
|
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
55 'dgettext', 'dngettext', 'gettext', 'ngettext', |
|
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
56 ] |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
57 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
58 _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
|
59 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
60 |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
61 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
|
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 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
|
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 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
|
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 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
|
68 """ |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
69 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
|
70 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
|
71 else: |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
72 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
|
73 |
|
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 def c2py(plural): |
| 28795 | 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. | |
|
26327
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 |
|
34293
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
32794
diff
changeset
|
80 try: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
32794
diff
changeset
|
81 from cStringIO import StringIO |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
32794
diff
changeset
|
82 except ImportError: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
32794
diff
changeset
|
83 from StringIO import StringIO |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
84 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
|
85 tokens = tokenize.generate_tokens(StringIO(plural).readline) |
|
28256
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
86 try: |
| 28795 | 87 danger = [x for x in tokens if x[0] == token.NAME and x[1] != 'n'] |
|
28256
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
88 except tokenize.TokenError: |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
89 raise ValueError, \ |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
90 'plural forms expression error, maybe unbalanced parenthesis' |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
91 else: |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
92 if danger: |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
93 raise ValueError, 'plural forms expression could be dangerous' |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
94 |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
95 # 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
|
96 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
|
97 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
|
98 |
|
28256
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
99 expr = re.compile(r'\!([^=])') |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
100 plural = expr.sub(' not \\1', plural) |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
101 |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
102 # 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
|
103 # "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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
109 # 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
|
110 stack = [''] |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
111 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
|
112 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
|
113 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
|
114 elif c == ')': |
|
28256
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
115 if len(stack) == 1: |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
116 # Actually, we never reach this code, because unbalanced |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
117 # parentheses get caught in the security check at the |
|
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
118 # beginning. |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 else: |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
123 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
|
124 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
|
125 |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
126 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
|
127 |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
128 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
129 |
|
14194
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
130 def _expand_lang(locale): |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
131 from locale import normalize |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
132 locale = normalize(locale) |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
133 COMPONENT_CODESET = 1 << 0 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
134 COMPONENT_TERRITORY = 1 << 1 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
135 COMPONENT_MODIFIER = 1 << 2 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
136 # 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
|
137 mask = 0 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
138 pos = locale.find('@') |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
139 if pos >= 0: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
140 modifier = locale[pos:] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
141 locale = locale[:pos] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
142 mask |= COMPONENT_MODIFIER |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
143 else: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
144 modifier = '' |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
145 pos = locale.find('.') |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
146 if pos >= 0: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
147 codeset = locale[pos:] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
148 locale = locale[:pos] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
149 mask |= COMPONENT_CODESET |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
150 else: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
151 codeset = '' |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
152 pos = locale.find('_') |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
153 if pos >= 0: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
154 territory = locale[pos:] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
155 locale = locale[:pos] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
156 mask |= COMPONENT_TERRITORY |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
157 else: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
158 territory = '' |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
159 language = locale |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
160 ret = [] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
161 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
|
162 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
|
163 val = language |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
164 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
|
165 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
|
166 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
|
167 ret.append(val) |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
168 ret.reverse() |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
169 return ret |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
170 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
171 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
172 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
173 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
|
174 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
|
175 self._info = {} |
|
28586
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
176 self._charset = None |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
177 self._output_charset = None |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
178 self._fallback = None |
|
23612
3caee2958387
Replace boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
21704
diff
changeset
|
179 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
|
180 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
|
181 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
182 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
|
183 pass |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
184 |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
185 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
|
186 if self._fallback: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
187 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
|
188 else: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
189 self._fallback = fallback |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
190 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
191 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
|
192 if self._fallback: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
193 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
|
194 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
|
195 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
196 def lgettext(self, message): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
197 if self._fallback: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
198 return self._fallback.lgettext(message) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
199 return message |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
200 |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 else: |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
207 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
|
208 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
209 def lngettext(self, msgid1, msgid2, n): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
210 if self._fallback: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
211 return self._fallback.lngettext(msgid1, msgid2, n) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
212 if n == 1: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
213 return msgid1 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
214 else: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
215 return msgid2 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
216 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
217 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
|
218 if self._fallback: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
219 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
|
220 return unicode(message) |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
221 |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 else: |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
228 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
|
229 |
|
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 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
|
231 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
|
232 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
233 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
|
234 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
|
235 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
236 def output_charset(self): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
237 return self._output_charset |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
238 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
239 def set_output_charset(self, charset): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
240 self._output_charset = charset |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
241 |
|
36593
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
242 def install(self, unicode=False, names=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
|
243 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
|
244 __builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext |
|
36593
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
245 if hasattr(names, "__contains__"): |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
246 if "gettext" in names: |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
247 __builtin__.__dict__['gettext'] = __builtin__.__dict__['_'] |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
248 if "ngettext" in names: |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
249 __builtin__.__dict__['ngettext'] = (unicode and self.ungettext |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
250 or self.ngettext) |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
251 if "lgettext" in names: |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
252 __builtin__.__dict__['lgettext'] = self.lgettext |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
253 if "lngettext" in names: |
|
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
254 __builtin__.__dict__['lngettext'] = self.lngettext |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
255 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
256 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
257 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
|
258 # 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
|
259 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
|
260 BE_MAGIC = 0xde120495L |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
261 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
262 def _parse(self, fp): |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
263 """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
|
264 unpack = struct.unpack |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
265 filename = getattr(fp, 'name', '') |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
266 # 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
|
267 # 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
|
268 self._catalog = catalog = {} |
|
28256
b246ca033e3e
Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents:
26327
diff
changeset
|
269 self.plural = lambda n: int(n != 1) # germanic plural by default |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
270 buf = fp.read() |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
271 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
|
272 # 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 else: |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
281 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
|
282 # 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
|
283 # dictionary. |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
284 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
|
285 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
|
286 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
|
287 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
|
288 tend = toff + tlen |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
289 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
|
290 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
|
291 tmsg = buf[toff:tend] |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
292 else: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
293 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
|
294 # See if we're looking at GNU .mo conventions for metadata |
|
28586
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
295 if mlen == 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
|
296 # Catalog description |
|
30641
71757cea257f
GNUTranslations._parse(): Initialize local variable k so that if the
Barry Warsaw <barry@python.org>
parents:
29190
diff
changeset
|
297 lastk = k = None |
|
28582
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
298 for item in tmsg.splitlines(): |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
299 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
|
300 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
|
301 continue |
|
29190
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
302 if ':' in item: |
|
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
303 k, v = item.split(':', 1) |
|
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
304 k = k.strip().lower() |
|
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
305 v = v.strip() |
|
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
306 self._info[k] = v |
|
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
307 lastk = k |
|
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
308 elif lastk: |
|
227c6646cc6b
GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents:
28795
diff
changeset
|
309 self._info[lastk] += '\n' + item |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
310 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
|
311 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
|
312 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
|
313 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
|
314 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
|
315 self.plural = c2py(plural) |
| 28795 | 316 # Note: we unconditionally convert both msgids and msgstrs to |
| 317 # Unicode using the character encoding specified in the charset | |
| 318 # parameter of the Content-Type header. The gettext documentation | |
|
68503
ee5350f1d0c2
#11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65093
diff
changeset
|
319 # strongly encourages msgids to be us-ascii, but some applications |
| 28795 | 320 # require alternative encodings (e.g. Zope's ZCML and ZPT). For |
| 321 # traditional gettext applications, the msgid conversion will | |
| 322 # cause no problems since us-ascii should always be a subset of | |
| 323 # the charset encoding. We may want to fall back to 8-bit msgids | |
| 324 # if the Unicode conversion fails. | |
|
31967
3d046d063a84
Replace str.find()!=1 with the more readable "in" operator.
Raymond Hettinger <python@rcn.com>
parents:
30641
diff
changeset
|
325 if '\x00' in msg: |
|
28586
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
326 # Plural forms |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
327 msgid1, msgid2 = msg.split('\x00') |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
328 tmsg = tmsg.split('\x00') |
| 28795 | 329 if self._charset: |
|
28586
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
330 msgid1 = unicode(msgid1, self._charset) |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
331 tmsg = [unicode(x, self._charset) for x in tmsg] |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
332 for i in range(len(tmsg)): |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
333 catalog[(msgid1, i)] = tmsg[i] |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
334 else: |
| 28795 | 335 if self._charset: |
|
28586
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
336 msg = unicode(msg, self._charset) |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
337 tmsg = unicode(tmsg, self._charset) |
|
94a9865aa1c6
NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents:
28582
diff
changeset
|
338 catalog[msg] = tmsg |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
339 # 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
|
340 masteridx += 8 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
341 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
|
342 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
343 def gettext(self, message): |
| 28795 | 344 missing = object() |
| 345 tmsg = self._catalog.get(message, missing) | |
| 346 if tmsg is missing: | |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
347 if self._fallback: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
348 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
|
349 return message |
| 28795 | 350 # Encode the Unicode tmsg back to an 8-bit string, if possible |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
351 if self._output_charset: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
352 return tmsg.encode(self._output_charset) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
353 elif self._charset: |
| 28795 | 354 return tmsg.encode(self._charset) |
| 355 return tmsg | |
|
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 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
357 def lgettext(self, message): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
358 missing = object() |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
359 tmsg = self._catalog.get(message, missing) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
360 if tmsg is missing: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
361 if self._fallback: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
362 return self._fallback.lgettext(message) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
363 return message |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
364 if self._output_charset: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
365 return tmsg.encode(self._output_charset) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
366 return tmsg.encode(locale.getpreferredencoding()) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
367 |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
368 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
|
369 try: |
| 28795 | 370 tmsg = self._catalog[(msgid1, self.plural(n))] |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
371 if self._output_charset: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
372 return tmsg.encode(self._output_charset) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
373 elif self._charset: |
| 28795 | 374 return tmsg.encode(self._charset) |
| 375 return tmsg | |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 else: |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
382 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
|
383 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
384 def lngettext(self, msgid1, msgid2, n): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
385 try: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
386 tmsg = self._catalog[(msgid1, self.plural(n))] |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
387 if self._output_charset: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
388 return tmsg.encode(self._output_charset) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
389 return tmsg.encode(locale.getpreferredencoding()) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
390 except KeyError: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
391 if self._fallback: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
392 return self._fallback.lngettext(msgid1, msgid2, n) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
393 if n == 1: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
394 return msgid1 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
395 else: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
396 return msgid2 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
397 |
|
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 def ugettext(self, message): |
|
28582
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
399 missing = object() |
|
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
400 tmsg = self._catalog.get(message, missing) |
|
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
401 if tmsg is missing: |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
402 if self._fallback: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
403 return self._fallback.ugettext(message) |
| 28795 | 404 return unicode(message) |
|
28582
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
405 return tmsg |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
406 |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
407 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
|
408 try: |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
409 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
|
410 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
|
411 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
|
412 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
|
413 if n == 1: |
| 28795 | 414 tmsg = unicode(msgid1) |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
415 else: |
| 28795 | 416 tmsg = unicode(msgid2) |
|
28582
03bb0c408d57
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents:
28256
diff
changeset
|
417 return tmsg |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
418 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
419 |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
420 # 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
|
421 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
|
422 # 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
|
423 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
|
424 localedir = _default_localedir |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
425 if languages is None: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
426 languages = [] |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
427 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
|
428 val = os.environ.get(envar) |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
429 if val: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
430 languages = val.split(':') |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
431 break |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
432 if 'C' not in languages: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
433 languages.append('C') |
|
14194
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
434 # 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
|
435 nelangs = [] |
|
14194
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
436 for lang in languages: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
437 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
|
438 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
|
439 nelangs.append(nelang) |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
440 # 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
|
441 if all: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
442 result = [] |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
443 else: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
444 result = None |
|
15244
71ea2bb01889
find(): Application of (slightly modified) SF patch #101928 by Ulf
Barry Warsaw <barry@python.org>
parents:
15035
diff
changeset
|
445 for lang in nelangs: |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
446 if lang == 'C': |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
447 break |
|
14192
9bf159ff4912
Group consensus is that supporting alternative locale categories is
Barry Warsaw <barry@python.org>
parents:
14187
diff
changeset
|
448 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
|
449 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
|
450 if all: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
451 result.append(mofile) |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
452 else: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
453 return mofile |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
454 return result |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
455 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
456 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
457 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
458 # 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
|
459 _translations = {} |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
460 |
|
21702
48ecd6061e88
Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
461 def translation(domain, localedir=None, languages=None, |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
462 class_=None, fallback=False, codeset=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
|
463 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
|
464 class_ = GNUTranslations |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
465 mofiles = find(domain, localedir, languages, all=1) |
| 28795 | 466 if not mofiles: |
|
21702
48ecd6061e88
Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
467 if fallback: |
|
48ecd6061e88
Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
468 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
|
469 raise IOError(ENOENT, 'No translation file found for domain', domain) |
|
15035
894dca225d24
translation(): Minor optimization patch which avoids instantiating the
Barry Warsaw <barry@python.org>
parents:
14326
diff
changeset
|
470 # 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
|
471 # once. |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
472 result = None |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
473 for mofile in mofiles: |
|
65093
12c6da5acd73
Merged revisions 85223 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
53963
diff
changeset
|
474 key = (class_, os.path.abspath(mofile)) |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
475 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
|
476 if t is None: |
|
53963
a7c53fad13bc
close file explicitly
Benjamin Peterson <benjamin@python.org>
parents:
36593
diff
changeset
|
477 with open(mofile, 'rb') as fp: |
|
a7c53fad13bc
close file explicitly
Benjamin Peterson <benjamin@python.org>
parents:
36593
diff
changeset
|
478 t = _translations.setdefault(key, class_(fp)) |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
479 # Copy the translation object to allow setting fallbacks and |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
480 # output charset. All other instance data is shared with the |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
481 # cached object. |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
482 t = copy.copy(t) |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
483 if codeset: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
484 t.set_output_charset(codeset) |
|
21704
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
485 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
|
486 result = t |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
487 else: |
|
7531cc592e46
Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents:
21702
diff
changeset
|
488 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
|
489 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
|
490 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
491 |
|
36593
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
492 def install(domain, localedir=None, unicode=False, codeset=None, names=None): |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
493 t = translation(domain, localedir, fallback=True, codeset=codeset) |
|
36593
bd7f2d8e8dea
Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
494 t.install(unicode, names) |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
495 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
496 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
497 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
498 # 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
|
499 _localedirs = {} |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
500 # a mapping b/w domains and codesets |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
501 _localecodesets = {} |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
502 # 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
|
503 _current_domain = 'messages' |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
504 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
505 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
506 def textdomain(domain=None): |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
507 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
|
508 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
|
509 _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
|
510 return _current_domain |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
511 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
512 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
513 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
|
514 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
|
515 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
|
516 _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
|
517 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
|
518 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
519 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
520 def bind_textdomain_codeset(domain, codeset=None): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
521 global _localecodesets |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
522 if codeset is not None: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
523 _localecodesets[domain] = codeset |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
524 return _localecodesets.get(domain) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
525 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
526 |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
527 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
|
528 try: |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
529 t = translation(domain, _localedirs.get(domain, None), |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
530 codeset=_localecodesets.get(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
|
531 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
|
532 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
|
533 return t.gettext(message) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
534 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
535 def ldgettext(domain, message): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
536 try: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
537 t = translation(domain, _localedirs.get(domain, None), |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
538 codeset=_localecodesets.get(domain)) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
539 except IOError: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
540 return message |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
541 return t.lgettext(message) |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
542 |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
543 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
|
544 try: |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
545 t = translation(domain, _localedirs.get(domain, None), |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
546 codeset=_localecodesets.get(domain)) |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
547 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
|
548 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
|
549 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
|
550 else: |
|
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
551 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
|
552 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
|
553 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
554 def ldngettext(domain, msgid1, msgid2, n): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
555 try: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
556 t = translation(domain, _localedirs.get(domain, None), |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
557 codeset=_localecodesets.get(domain)) |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
558 except IOError: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
559 if n == 1: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
560 return msgid1 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
561 else: |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
562 return msgid2 |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
563 return t.lngettext(msgid1, msgid2, n) |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
564 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
565 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
|
566 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
|
567 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
568 def lgettext(message): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
569 return ldgettext(_current_domain, message) |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
570 |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
571 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
|
572 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
|
573 |
|
32794
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
574 def lngettext(msgid1, msgid2, n): |
|
0f9c39b3e5f0
This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
31967
diff
changeset
|
575 return ldngettext(_current_domain, msgid1, msgid2, n) |
|
26327
ea1d2050d7da
Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents:
24812
diff
changeset
|
576 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
577 # 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
|
578 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
579 # 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
|
580 # was: |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
581 # |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
582 # 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
|
583 # 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
|
584 # _ = 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
|
585 # 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
|
586 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
587 # 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
|
588 # 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
|
589 # gettext. |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
590 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
591 Catalog = translation |
