Mercurial > cpython-lru_cache-weakref
annotate Lib/gettext.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 4027361e7956 |
| children | a9210bec3b5a 7531cc592e46 |
| 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 # |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
35 # TODO: |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
36 # - 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
|
37 # 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
|
38 # 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
|
39 # 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
|
40 # 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
|
41 # 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
|
42 # |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
43 # - 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
|
44 # 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
|
45 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
46 import os |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
47 import sys |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
48 import struct |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
49 from errno import ENOENT |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
50 |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
51 __all__ = ["bindtextdomain","textdomain","gettext","dgettext", |
|
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
52 "find","translation","install","Catalog"] |
|
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
53 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
54 _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
|
55 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
56 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
57 |
|
14194
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
58 def _expand_lang(locale): |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
59 from locale import normalize |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
60 locale = normalize(locale) |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
61 COMPONENT_CODESET = 1 << 0 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
62 COMPONENT_TERRITORY = 1 << 1 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
63 COMPONENT_MODIFIER = 1 << 2 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
64 # 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
|
65 mask = 0 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
66 pos = locale.find('@') |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
67 if pos >= 0: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
68 modifier = locale[pos:] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
69 locale = locale[:pos] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
70 mask |= COMPONENT_MODIFIER |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
71 else: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
72 modifier = '' |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
73 pos = locale.find('.') |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
74 if pos >= 0: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
75 codeset = locale[pos:] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
76 locale = locale[:pos] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
77 mask |= COMPONENT_CODESET |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
78 else: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
79 codeset = '' |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
80 pos = locale.find('_') |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
81 if pos >= 0: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
82 territory = locale[pos:] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
83 locale = locale[:pos] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
84 mask |= COMPONENT_TERRITORY |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
85 else: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
86 territory = '' |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
87 language = locale |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
88 ret = [] |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
89 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
|
90 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
|
91 val = language |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
92 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
|
93 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
|
94 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
|
95 ret.append(val) |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
96 ret.reverse() |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
97 return ret |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
98 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
99 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
100 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
101 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
|
102 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
|
103 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
|
104 self._charset = None |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
105 if fp: |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
106 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
|
107 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
108 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
|
109 pass |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
110 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
111 def gettext(self, message): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
112 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
|
113 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
114 def ugettext(self, message): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
115 return unicode(message) |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
116 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
117 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
|
118 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
|
119 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
120 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
|
121 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
|
122 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
123 def install(self, unicode=0): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
124 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
|
125 __builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
126 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
127 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
128 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
|
129 # Magic number of .mo files |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
130 LE_MAGIC = 0x950412de |
|
14292
11a893cbc1a0
GNUTranslations.BE_MAGIC: don't be so clever in calculating this from
Barry Warsaw <barry@python.org>
parents:
14255
diff
changeset
|
131 BE_MAGIC = 0xde120495 |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
132 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
133 def _parse(self, fp): |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
134 """Override this method to support alternative .mo formats.""" |
| 14326 | 135 # We need to & all 32 bit unsigned integers with 0xffffffff for |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
136 # portability to 64 bit machines. |
|
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
137 MASK = 0xffffffff |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
138 unpack = struct.unpack |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
139 filename = getattr(fp, 'name', '') |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
140 # 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
|
141 # 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
|
142 self._catalog = catalog = {} |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
143 buf = fp.read() |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
144 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
|
145 # Are we big endian or little endian? |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
146 magic = unpack('<i', buf[:4])[0] & MASK |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
147 if magic == self.LE_MAGIC: |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
148 version, msgcount, masteridx, transidx = unpack('<4i', buf[4:20]) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
149 ii = '<ii' |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
150 elif magic == self.BE_MAGIC: |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
151 version, msgcount, masteridx, transidx = unpack('>4i', buf[4:20]) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
152 ii = '>ii' |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
153 else: |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
154 raise IOError(0, 'Bad magic number', filename) |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
155 # more unsigned ints |
|
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
156 msgcount &= MASK |
|
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
157 masteridx &= MASK |
|
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
158 transidx &= MASK |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
159 # 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
|
160 # dictionary. |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
161 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
|
162 mlen, moff = unpack(ii, buf[masteridx:masteridx+8]) |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
163 moff &= MASK |
|
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
164 mend = moff + (mlen & MASK) |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
165 tlen, toff = unpack(ii, buf[transidx:transidx+8]) |
|
14317
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
166 toff &= MASK |
|
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
167 tend = toff + (tlen & MASK) |
|
2dcfad3b2115
GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents:
14292
diff
changeset
|
168 if mend < buflen and tend < buflen: |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
169 tmsg = buf[toff:tend] |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
170 catalog[buf[moff:mend]] = tmsg |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
171 else: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
172 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
|
173 # See if we're looking at GNU .mo conventions for metadata |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
174 if mlen == 0 and tmsg.lower().startswith('project-id-version:'): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
175 # Catalog description |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
176 for item in tmsg.split('\n'): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
177 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
|
178 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
|
179 continue |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
180 k, v = item.split(':', 1) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
181 k = k.strip().lower() |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
182 v = v.strip() |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
183 self._info[k] = v |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
184 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
|
185 self._charset = v.split('charset=')[1] |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
186 # 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
|
187 masteridx += 8 |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
188 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
|
189 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
190 def gettext(self, message): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
191 return self._catalog.get(message, message) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
192 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
193 def ugettext(self, message): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
194 tmsg = self._catalog.get(message, 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 return unicode(tmsg, self._charset) |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
196 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
197 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
198 |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
199 # Locate a .mo file using the gettext strategy |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
200 def find(domain, localedir=None, languages=None): |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
201 # 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
|
202 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
|
203 localedir = _default_localedir |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
204 if languages is None: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
205 languages = [] |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
206 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
|
207 val = os.environ.get(envar) |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
208 if val: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
209 languages = val.split(':') |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
210 break |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
211 if 'C' not in languages: |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
212 languages.append('C') |
|
14194
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
213 # 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
|
214 nelangs = [] |
|
14194
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
215 for lang in languages: |
|
9d8ce6ed4a29
_expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents:
14192
diff
changeset
|
216 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
|
217 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
|
218 nelangs.append(nelang) |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
219 # select a language |
|
15244
71ea2bb01889
find(): Application of (slightly modified) SF patch #101928 by Ulf
Barry Warsaw <barry@python.org>
parents:
15035
diff
changeset
|
220 for lang in nelangs: |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
221 if lang == 'C': |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
222 break |
|
14192
9bf159ff4912
Group consensus is that supporting alternative locale categories is
Barry Warsaw <barry@python.org>
parents:
14187
diff
changeset
|
223 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
|
224 if os.path.exists(mofile): |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
225 return mofile |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
226 return None |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
227 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
228 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
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 # 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
|
231 _translations = {} |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
232 |
|
21703
4027361e7956
Add fallback argument to translation(). Set fallback to 1 for install().
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
233 def translation(domain, localedir=None, languages=None, |
|
4027361e7956
Add fallback argument to translation(). Set fallback to 1 for install().
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
234 class_=None, fallback=0): |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
235 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
|
236 class_ = GNUTranslations |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
237 mofile = find(domain, localedir, languages) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
238 if mofile is None: |
|
21703
4027361e7956
Add fallback argument to translation(). Set fallback to 1 for install().
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
239 if fallback: |
|
4027361e7956
Add fallback argument to translation(). Set fallback to 1 for install().
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
240 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
|
241 raise IOError(ENOENT, 'No translation file found for domain', domain) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
242 key = os.path.abspath(mofile) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
243 # TBD: do we need to worry about the file pointer getting collected? |
|
15035
894dca225d24
translation(): Minor optimization patch which avoids instantiating the
Barry Warsaw <barry@python.org>
parents:
14326
diff
changeset
|
244 # 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
|
245 # once. |
|
894dca225d24
translation(): Minor optimization patch which avoids instantiating the
Barry Warsaw <barry@python.org>
parents:
14326
diff
changeset
|
246 t = _translations.get(key) |
|
894dca225d24
translation(): Minor optimization patch which avoids instantiating the
Barry Warsaw <barry@python.org>
parents:
14326
diff
changeset
|
247 if t is None: |
|
894dca225d24
translation(): Minor optimization patch which avoids instantiating the
Barry Warsaw <barry@python.org>
parents:
14326
diff
changeset
|
248 t = _translations.setdefault(key, class_(open(mofile, 'rb'))) |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
249 return t |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
250 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
251 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
252 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
253 def install(domain, localedir=None, unicode=0): |
|
21703
4027361e7956
Add fallback argument to translation(). Set fallback to 1 for install().
Martin v. Löwis <martin@v.loewis.de>
parents:
16160
diff
changeset
|
254 translation(domain, localedir, fallback=1).install(unicode) |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
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 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
257 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
258 # 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
|
259 _localedirs = {} |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
260 # 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
|
261 _current_domain = 'messages' |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
262 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
263 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
264 def textdomain(domain=None): |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
265 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
|
266 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
|
267 _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
|
268 return _current_domain |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
269 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
270 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
271 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
|
272 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
|
273 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
|
274 _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
|
275 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
|
276 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
277 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
278 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
|
279 try: |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
280 t = translation(domain, _localedirs.get(domain, None)) |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
281 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
|
282 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
|
283 return t.gettext(message) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15244
diff
changeset
|
284 |
|
14187
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
285 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
286 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
|
287 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
|
288 |
|
581460fcf142
Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
289 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
290 # 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
|
291 |
|
14255
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
292 # 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
|
293 # was: |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
294 # |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
295 # 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
|
296 # 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
|
297 # _ = 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
|
298 # 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
|
299 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
300 # 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
|
301 # 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
|
302 # gettext. |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
303 |
|
cdd95d67a714
Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents:
14194
diff
changeset
|
304 Catalog = translation |
