annotate Lib/gettext.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 81d7f78a633a
children 294f8aeb4d4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 """Internationalization and localization support.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
2
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 This module provides internationalization (I18N) and localization (L10N)
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 support for your Python programs by providing an interface to the GNU gettext
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
5 message catalog library.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
6
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
7 I18N refers to the operation by which a program is made aware of multiple
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
8 languages. L10N refers to the adaptation of your program, once
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
9 internationalized, to the local language and cultural habits.
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
10
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
11 """
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
12
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
13 # This module represents the integration of work, contributions, feedback, and
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
14 # suggestions from the following people:
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
16 # Martin von Loewis, who wrote the initial implementation of the underlying
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
17 # C-based libintlmodule (later renamed _gettext), along with a skeletal
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
18 # gettext.py implementation.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
19 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 # Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule,
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
21 # which also included a pure-Python implementation to read .mo files if
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
22 # intlmodule wasn't available.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
24 # James Henstridge, who also wrote a gettext.py module, which has some
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
25 # interesting, but currently unsupported experimental features: the notion of
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
26 # a Catalog class and instances, and the ability to add to a catalog file via
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
27 # a Python API.
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
28 #
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
29 # Barry Warsaw integrated these modules, wrote the .install() API and code,
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
30 # and conformed all C and Python code to Python's coding standards.
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
31 #
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
32 # Francois Pinard and Marc-Andre Lemburg also contributed valuably to this
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
33 # module.
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
34 #
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
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
49 import locale, copy, io, 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 c2py(plural):
28795
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
62 """Gets a C expression as used in PO files for plural forms and returns a
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
63 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
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 # Security check, allow only the "n" identifier
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
66 import token, tokenize
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
67 tokens = tokenize.generate_tokens(io.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
68 try:
28795
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
69 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
70 except tokenize.TokenError:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42968
diff changeset
71 raise ValueError('plural forms expression error, maybe unbalanced parenthesis')
28256
b246ca033e3e Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents: 26327
diff changeset
72 else:
b246ca033e3e Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents: 26327
diff changeset
73 if danger:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42968
diff changeset
74 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
75
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
76 # 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
77 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
78 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
79
28256
b246ca033e3e Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents: 26327
diff changeset
80 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
81 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
82
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
83 # Regular expression and replacement function used to transform
67108
d5faa9ffdf2a update comment
Benjamin Peterson <benjamin@python.org>
parents: 67105
diff changeset
84 # "a?b:c" to "b if a else c".
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
85 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
86 def repl(x):
67104
fdb8b02c1e65 use native tenary condition
Benjamin Peterson <benjamin@python.org>
parents: 65091
diff changeset
87 return "(%s if %s else %s)" % (x.group(2), x.group(1),
fdb8b02c1e65 use native tenary condition
Benjamin Peterson <benjamin@python.org>
parents: 65091
diff changeset
88 expr.sub(repl, x.group(3)))
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
89
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
90 # 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
91 stack = ['']
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
92 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
93 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
94 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
95 elif c == ')':
28256
b246ca033e3e Patch #700839: Fix bugs in the plural handling.
Martin v. Löwis <martin@v.loewis.de>
parents: 26327
diff changeset
96 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
97 # 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
98 # 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
99 # beginning.
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42968
diff changeset
100 raise ValueError('unbalanced parenthesis in plural form')
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 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
102 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
103 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
104 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
105 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
106
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
107 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
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
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
110
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
111 def _expand_lang(loc):
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
112 loc = locale.normalize(loc)
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
113 COMPONENT_CODESET = 1 << 0
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
114 COMPONENT_TERRITORY = 1 << 1
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
115 COMPONENT_MODIFIER = 1 << 2
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
116 # 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
117 mask = 0
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
118 pos = loc.find('@')
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
119 if pos >= 0:
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
120 modifier = loc[pos:]
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
121 loc = loc[:pos]
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
122 mask |= COMPONENT_MODIFIER
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
123 else:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
124 modifier = ''
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
125 pos = loc.find('.')
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
126 if pos >= 0:
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
127 codeset = loc[pos:]
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
128 loc = loc[:pos]
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
129 mask |= COMPONENT_CODESET
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
130 else:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
131 codeset = ''
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
132 pos = loc.find('_')
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
133 if pos >= 0:
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
134 territory = loc[pos:]
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
135 loc = loc[:pos]
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
136 mask |= COMPONENT_TERRITORY
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
137 else:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
138 territory = ''
67105
e1bf3c1c9561 kill some function imports
Benjamin Peterson <benjamin@python.org>
parents: 67104
diff changeset
139 language = loc
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
140 ret = []
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
141 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
142 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
143 val = language
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
144 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
145 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
146 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
147 ret.append(val)
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
148 ret.reverse()
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
149 return ret
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
150
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
151
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
152
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
153 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
154 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
155 self._info = {}
28586
94a9865aa1c6 NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents: 28582
diff changeset
156 self._charset = None
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
157 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
158 self._fallback = None
23612
3caee2958387 Replace boolean test with is None
Raymond Hettinger <python@rcn.com>
parents: 21704
diff changeset
159 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
160 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
161
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
162 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
163 pass
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
164
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
165 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
166 if self._fallback:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
167 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
168 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
169 self._fallback = fallback
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
170
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
171 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
172 if self._fallback:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
173 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
174 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
175
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
176 def lgettext(self, message):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
177 if self._fallback:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
178 return self._fallback.lgettext(message)
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
179 return message
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
180
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
181 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
182 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
183 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
184 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
185 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
186 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
187 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
188
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
189 def lngettext(self, msgid1, msgid2, n):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
190 if self._fallback:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
191 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
192 if n == 1:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
193 return msgid1
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
194 else:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
195 return msgid2
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
196
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
197 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
198 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
199
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 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
201 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
202
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
203 def output_charset(self):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
204 return self._output_charset
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
205
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
206 def set_output_charset(self, charset):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
207 self._output_charset = charset
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
208
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
209 def install(self, names=None):
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
210 import builtins
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
211 builtins.__dict__['_'] = self.gettext
36593
bd7f2d8e8dea Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents: 34293
diff changeset
212 if hasattr(names, "__contains__"):
bd7f2d8e8dea Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents: 34293
diff changeset
213 if "gettext" in names:
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
214 builtins.__dict__['gettext'] = builtins.__dict__['_']
36593
bd7f2d8e8dea Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents: 34293
diff changeset
215 if "ngettext" in names:
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
216 builtins.__dict__['ngettext'] = self.ngettext
36593
bd7f2d8e8dea Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents: 34293
diff changeset
217 if "lgettext" in names:
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
218 builtins.__dict__['lgettext'] = self.lgettext
36593
bd7f2d8e8dea Patch #1349274: gettext.install() now optionally installs additional
Georg Brandl <georg@python.org>
parents: 34293
diff changeset
219 if "lngettext" in names:
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43777
diff changeset
220 builtins.__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
221
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
222
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
223 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
224 # Magic number of .mo files
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 36593
diff changeset
225 LE_MAGIC = 0x950412de
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 36593
diff changeset
226 BE_MAGIC = 0xde120495
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 def _parse(self, fp):
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
229 """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
230 unpack = struct.unpack
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
231 filename = getattr(fp, 'name', '')
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
232 # 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
233 # 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
234 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
235 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
236 buf = fp.read()
14317
2dcfad3b2115 GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents: 14292
diff changeset
237 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
238 # 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
239 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
240 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
241 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
242 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
243 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
244 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
245 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
246 else:
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
247 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
248 # 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
249 # dictionary.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
250 for i in range(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
251 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
252 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
253 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
254 tend = toff + tlen
14317
2dcfad3b2115 GNUTranslations._parse(): Fix portability problems on 64-bit machines
Barry Warsaw <barry@python.org>
parents: 14292
diff changeset
255 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
256 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
257 tmsg = buf[toff:tend]
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
258 else:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
259 raise IOError(0, 'File is corrupt', filename)
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
260 # See if we're looking at GNU .mo conventions for metadata
28586
94a9865aa1c6 NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents: 28582
diff changeset
261 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
262 # Catalog description
30641
71757cea257f GNUTranslations._parse(): Initialize local variable k so that if the
Barry Warsaw <barry@python.org>
parents: 29190
diff changeset
263 lastk = k = None
43722
bcf629e43082 Fixed gettext module for Windows. The metadata lines always end in \n and not in os.linesep
Christian Heimes <christian@cheimes.de>
parents: 43020
diff changeset
264 for b_item in tmsg.split('\n'.encode("ascii")):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43722
diff changeset
265 item = b_item.decode().strip()
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 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
267 continue
29190
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
268 if ':' in item:
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
269 k, v = item.split(':', 1)
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
270 k = k.strip().lower()
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
271 v = v.strip()
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
272 self._info[k] = v
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
273 lastk = k
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
274 elif lastk:
227c6646cc6b GNUTranslations._parse(): Fix SF bug #658233, where continuation lines
Barry Warsaw <barry@python.org>
parents: 28795
diff changeset
275 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
276 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
277 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
278 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
279 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
280 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
281 self.plural = c2py(plural)
28795
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
282 # Note: we unconditionally convert both msgids and msgstrs to
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
283 # Unicode using the character encoding specified in the charset
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
284 # parameter of the Content-Type header. The gettext documentation
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65091
diff changeset
285 # strongly encourages msgids to be us-ascii, but some applications
28795
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
286 # require alternative encodings (e.g. Zope's ZCML and ZPT). For
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
287 # traditional gettext applications, the msgid conversion will
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
288 # cause no problems since us-ascii should always be a subset of
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
289 # the charset encoding. We may want to fall back to 8-bit msgids
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
290 # if the Unicode conversion fails.
48520
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
291 charset = self._charset or 'ascii'
42210
d5cf19169904 Fixes for str/uni/bytes for gettext.py. test_gettext.py passes.
Guido van Rossum <guido@python.org>
parents: 41760
diff changeset
292 if b'\x00' in msg:
28586
94a9865aa1c6 NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents: 28582
diff changeset
293 # Plural forms
42968
713fba34d66b Make gettext work with strict str/bytes.
Guido van Rossum <guido@python.org>
parents: 42210
diff changeset
294 msgid1, msgid2 = msg.split(b'\x00')
713fba34d66b Make gettext work with strict str/bytes.
Guido van Rossum <guido@python.org>
parents: 42210
diff changeset
295 tmsg = tmsg.split(b'\x00')
48520
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
296 msgid1 = str(msgid1, charset)
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
297 for i, x in enumerate(tmsg):
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
298 catalog[(msgid1, i)] = str(x, charset)
28586
94a9865aa1c6 NullTranslations.__init__(): Back out of setting the default charset
Barry Warsaw <barry@python.org>
parents: 28582
diff changeset
299 else:
48520
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
300 catalog[str(msg, charset)] = str(tmsg, charset)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
301 # 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
302 masteridx += 8
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
303 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
304
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
305 def lgettext(self, message):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
306 missing = object()
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
307 tmsg = self._catalog.get(message, missing)
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
308 if tmsg is missing:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
309 if self._fallback:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
310 return self._fallback.lgettext(message)
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
311 return message
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
312 if self._output_charset:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
313 return tmsg.encode(self._output_charset)
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
314 return tmsg.encode(locale.getpreferredencoding())
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
315
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
316 def lngettext(self, msgid1, msgid2, n):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
317 try:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
318 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
319 if self._output_charset:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
320 return tmsg.encode(self._output_charset)
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
321 return tmsg.encode(locale.getpreferredencoding())
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
322 except KeyError:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
323 if self._fallback:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
324 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
325 if n == 1:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
326 return msgid1
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
327 else:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
328 return msgid2
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
329
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
330 def gettext(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
331 missing = object()
03bb0c408d57 From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents: 28256
diff changeset
332 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
333 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
334 if self._fallback:
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
335 return self._fallback.gettext(message)
48520
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
336 return message
28582
03bb0c408d57 From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents: 28256
diff changeset
337 return tmsg
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
338
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
339 def ngettext(self, 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
340 try:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
341 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
342 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
343 if self._fallback:
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
344 return self._fallback.ngettext(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
345 if n == 1:
48520
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
346 tmsg = 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
347 else:
48520
7c6ec7a8016a Make gettext Unicode interface consistent and clean up the docs.
Georg Brandl <georg@python.org>
parents: 48468
diff changeset
348 tmsg = msgid2
28582
03bb0c408d57 From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
Barry Warsaw <barry@python.org>
parents: 28256
diff changeset
349 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
350
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
351
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
352 # Locate a .mo file using the gettext strategy
54171
74b6748b63f5 Use true booleans.
Georg Brandl <georg@python.org>
parents: 48520
diff changeset
353 def find(domain, localedir=None, languages=None, all=False):
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
354 # 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
355 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
356 localedir = _default_localedir
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
357 if languages is None:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
358 languages = []
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
359 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
360 val = os.environ.get(envar)
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
361 if val:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
362 languages = val.split(':')
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
363 break
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
364 if 'C' not in languages:
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
365 languages.append('C')
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
366 # 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
367 nelangs = []
14194
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
368 for lang in languages:
9d8ce6ed4a29 _expand_lang(), _find(): Added support for unaliasing and expanded the
Barry Warsaw <barry@python.org>
parents: 14192
diff changeset
369 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
370 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
371 nelangs.append(nelang)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
372 # 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
373 if all:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
374 result = []
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
375 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
376 result = None
15244
71ea2bb01889 find(): Application of (slightly modified) SF patch #101928 by Ulf
Barry Warsaw <barry@python.org>
parents: 15035
diff changeset
377 for lang in nelangs:
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
378 if lang == 'C':
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
379 break
14192
9bf159ff4912 Group consensus is that supporting alternative locale categories is
Barry Warsaw <barry@python.org>
parents: 14187
diff changeset
380 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
381 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
382 if all:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
383 result.append(mofile)
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
384 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
385 return mofile
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
386 return result
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
387
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
388
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
389
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
390 # 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
391 _translations = {}
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
392
21702
48ecd6061e88 Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents: 16160
diff changeset
393 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
394 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
395 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
396 class_ = GNUTranslations
54171
74b6748b63f5 Use true booleans.
Georg Brandl <georg@python.org>
parents: 48520
diff changeset
397 mofiles = find(domain, localedir, languages, all=True)
28795
725b281ccead GNUTranslations:
Barry Warsaw <barry@python.org>
parents: 28586
diff changeset
398 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
399 if fallback:
48ecd6061e88 Add fallback argument to translation(). Request fallbacks on install.
Martin v. Löwis <martin@v.loewis.de>
parents: 16160
diff changeset
400 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
401 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
402 # 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
403 # once.
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
404 result = None
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
405 for mofile in mofiles:
65091
753bf23e45c5 Fix interaction of custom translation classes and caching (#9042)
Éric Araujo <merwok@netwok.org>
parents: 54501
diff changeset
406 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
407 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
408 if t is None:
54501
ced380b40004 Merged revisions 72506,72525-72526,72551,72558,72616,72654-72655,72689,72745,72750,72802,72812,72822,72824,72826-72827,72833,72876,72890,72923,72946,73026,73042,73045,73047,73065,73068-73069 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54171
diff changeset
409 with open(mofile, 'rb') as fp:
ced380b40004 Merged revisions 72506,72525-72526,72551,72558,72616,72654-72655,72689,72745,72750,72802,72812,72822,72824,72826-72827,72833,72876,72890,72923,72946,73026,73042,73045,73047,73065,73068-73069 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54171
diff changeset
410 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
411 # 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
412 # 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
413 # cached object.
21704
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
414 t = copy.copy(t)
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
415 if codeset:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
416 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
417 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
418 result = t
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
419 else:
7531cc592e46 Add a per-message fallback mechanism for translations.
Martin v. Löwis <martin@v.loewis.de>
parents: 21702
diff changeset
420 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
421 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
422
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
423
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
424 def install(domain, localedir=None, codeset=None, names=None):
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
425 t = translation(domain, localedir, fallback=True, codeset=codeset)
48468
19369c7f5791 #2512 implement the 3.0 gettext API
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
426 t.install(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
427
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
428
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
429
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
430 # 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
431 _localedirs = {}
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
432 # 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
433 _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
434 # 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
435 _current_domain = 'messages'
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
436
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
437
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
438 def textdomain(domain=None):
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
439 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
440 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
441 _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
442 return _current_domain
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
443
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
444
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
445 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
446 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
447 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
448 _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
449 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
450
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
451
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
452 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
453 global _localecodesets
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
454 if codeset is not None:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
455 _localecodesets[domain] = codeset
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
456 return _localecodesets.get(domain)
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
457
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
458
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
459 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
460 try:
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
461 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
462 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
463 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
464 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
465 return t.gettext(message)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15244
diff changeset
466
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
467 def ldgettext(domain, message):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
468 try:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
469 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
470 codeset=_localecodesets.get(domain))
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
471 except IOError:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
472 return message
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
473 return t.lgettext(message)
14187
581460fcf142 Initial revision of gettext support for Python. This will undergo
Barry Warsaw <barry@python.org>
parents:
diff changeset
474
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
475 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
476 try:
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
477 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
478 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
479 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
480 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
481 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
482 else:
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
483 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
484 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
485
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
486 def ldngettext(domain, msgid1, msgid2, n):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
487 try:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
488 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
489 codeset=_localecodesets.get(domain))
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
490 except IOError:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
491 if n == 1:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
492 return msgid1
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
493 else:
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
494 return msgid2
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
495 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
496
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
497 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
498 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
499
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
500 def lgettext(message):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
501 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
502
26327
ea1d2050d7da Patch #633547: Support plural forms. Do TODOs in test suite.
Martin v. Löwis <martin@v.loewis.de>
parents: 24812
diff changeset
503 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
504 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
505
32794
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
506 def lngettext(msgid1, msgid2, n):
0f9c39b3e5f0 This change implements the following gettext features, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 31967
diff changeset
507 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
508
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
509 # 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
510
14255
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
511 # 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
512 # was:
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
513 #
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
514 # 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
515 # 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
516 # _ = 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
517 # 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
518
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
519 # 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
520 # 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
521 # gettext.
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
522
cdd95d67a714 Finalize this module for Python 2.0 based on feedback and input from
Barry Warsaw <barry@python.org>
parents: 14194
diff changeset
523 Catalog = translation