Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 1 | """Internationalization and localization support. |
| 2 | |
| 3 | This module provides internationalization (I18N) and localization (L10N) |
| 4 | support for your Python programs by providing an interface to the GNU gettext |
| 5 | message catalog library. |
| 6 | |
| 7 | I18N refers to the operation by which a program is made aware of multiple |
| 8 | languages. L10N refers to the adaptation of your program, once |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 9 | internationalized, to the local language and cultural habits. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 10 | |
| 11 | """ |
| 12 | |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 13 | # This module represents the integration of work, contributions, feedback, and |
| 14 | # suggestions from the following people: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 15 | # |
| 16 | # Martin von Loewis, who wrote the initial implementation of the underlying |
| 17 | # C-based libintlmodule (later renamed _gettext), along with a skeletal |
| 18 | # gettext.py implementation. |
| 19 | # |
| 20 | # Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule, |
| 21 | # which also included a pure-Python implementation to read .mo files if |
| 22 | # intlmodule wasn't available. |
| 23 | # |
| 24 | # James Henstridge, who also wrote a gettext.py module, which has some |
| 25 | # interesting, but currently unsupported experimental features: the notion of |
| 26 | # a Catalog class and instances, and the ability to add to a catalog file via |
| 27 | # a Python API. |
| 28 | # |
| 29 | # Barry Warsaw integrated these modules, wrote the .install() API and code, |
| 30 | # and conformed all C and Python code to Python's coding standards. |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 31 | # |
| 32 | # Francois Pinard and Marc-Andre Lemburg also contributed valuably to this |
| 33 | # module. |
| 34 | # |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 35 | # J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs. |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 36 | # |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 37 | # TODO: |
| 38 | # - Lazy loading of .mo files. Currently the entire catalog is loaded into |
| 39 | # memory, but that's probably bad for large translated programs. Instead, |
| 40 | # the lexical sort of original strings in GNU .mo files should be exploited |
| 41 | # to do binary searches and lazy initializations. Or you might want to use |
| 42 | # the undocumented double-hash algorithm for .mo files with hash tables, but |
| 43 | # you'll need to study the GNU gettext code to do this. |
| 44 | # |
| 45 | # - Support Solaris .mo file formats. Unfortunately, we've been unable to |
| 46 | # find this format documented anywhere. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 48 | |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 49 | import locale, copy, io, os, re, struct, sys |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 50 | from errno import ENOENT |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 51 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 52 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 53 | __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', |
| 54 | 'find', 'translation', 'install', 'textdomain', 'bindtextdomain', |
| 55 | 'dgettext', 'dngettext', 'gettext', 'ngettext', |
| 56 | ] |
Skip Montanaro | 2dd4276 | 2001-01-23 15:35:05 +0000 | [diff] [blame] | 57 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 58 | _default_localedir = os.path.join(sys.prefix, 'share', 'locale') |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 59 | |
| 60 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 61 | def c2py(plural): |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 62 | """Gets a C expression as used in PO files for plural forms and returns a |
| 63 | Python lambda function that implements an equivalent expression. |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 64 | """ |
| 65 | # Security check, allow only the "n" identifier |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 66 | import token, tokenize |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 67 | tokens = tokenize.generate_tokens(io.StringIO(plural).readline) |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 68 | try: |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 69 | danger = [x for x in tokens if x[0] == token.NAME and x[1] != 'n'] |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 70 | except tokenize.TokenError: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 71 | raise ValueError('plural forms expression error, maybe unbalanced parenthesis') |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 72 | else: |
| 73 | if danger: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 74 | raise ValueError('plural forms expression could be dangerous') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 75 | |
| 76 | # Replace some C operators by their Python equivalents |
| 77 | plural = plural.replace('&&', ' and ') |
| 78 | plural = plural.replace('||', ' or ') |
| 79 | |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 80 | expr = re.compile(r'\!([^=])') |
| 81 | plural = expr.sub(' not \\1', plural) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 82 | |
| 83 | # Regular expression and replacement function used to transform |
Benjamin Peterson | bc78e37 | 2010-12-23 23:45:39 +0000 | [diff] [blame] | 84 | # "a?b:c" to "b if a else c". |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 85 | expr = re.compile(r'(.*?)\?(.*?):(.*)') |
| 86 | def repl(x): |
Benjamin Peterson | a91dd1e | 2010-12-23 22:49:38 +0000 | [diff] [blame] | 87 | return "(%s if %s else %s)" % (x.group(2), x.group(1), |
| 88 | expr.sub(repl, x.group(3))) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 89 | |
| 90 | # Code to transform the plural expression, taking care of parentheses |
| 91 | stack = [''] |
| 92 | for c in plural: |
| 93 | if c == '(': |
| 94 | stack.append('') |
| 95 | elif c == ')': |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 96 | if len(stack) == 1: |
| 97 | # Actually, we never reach this code, because unbalanced |
| 98 | # parentheses get caught in the security check at the |
| 99 | # beginning. |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 100 | raise ValueError('unbalanced parenthesis in plural form') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 101 | s = expr.sub(repl, stack.pop()) |
| 102 | stack[-1] += '(%s)' % s |
| 103 | else: |
| 104 | stack[-1] += c |
| 105 | plural = expr.sub(repl, stack.pop()) |
| 106 | |
| 107 | return eval('lambda n: int(%s)' % plural) |
| 108 | |
| 109 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 110 | |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 111 | def _expand_lang(loc): |
| 112 | loc = locale.normalize(loc) |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 113 | COMPONENT_CODESET = 1 << 0 |
| 114 | COMPONENT_TERRITORY = 1 << 1 |
| 115 | COMPONENT_MODIFIER = 1 << 2 |
| 116 | # split up the locale into its base components |
| 117 | mask = 0 |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 118 | pos = loc.find('@') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 119 | if pos >= 0: |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 120 | modifier = loc[pos:] |
| 121 | loc = loc[:pos] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 122 | mask |= COMPONENT_MODIFIER |
| 123 | else: |
| 124 | modifier = '' |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 125 | pos = loc.find('.') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 126 | if pos >= 0: |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 127 | codeset = loc[pos:] |
| 128 | loc = loc[:pos] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 129 | mask |= COMPONENT_CODESET |
| 130 | else: |
| 131 | codeset = '' |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 132 | pos = loc.find('_') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 133 | if pos >= 0: |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 134 | territory = loc[pos:] |
| 135 | loc = loc[:pos] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 136 | mask |= COMPONENT_TERRITORY |
| 137 | else: |
| 138 | territory = '' |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 139 | language = loc |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 140 | ret = [] |
| 141 | for i in range(mask+1): |
| 142 | if not (i & ~mask): # if all components for this combo exist ... |
| 143 | val = language |
| 144 | if i & COMPONENT_TERRITORY: val += territory |
| 145 | if i & COMPONENT_CODESET: val += codeset |
| 146 | if i & COMPONENT_MODIFIER: val += modifier |
| 147 | ret.append(val) |
| 148 | ret.reverse() |
| 149 | return ret |
| 150 | |
| 151 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 152 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 153 | class NullTranslations: |
| 154 | def __init__(self, fp=None): |
| 155 | self._info = {} |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 156 | self._charset = None |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 157 | self._output_charset = None |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 158 | self._fallback = None |
Raymond Hettinger | 094662a | 2002-06-01 01:29:16 +0000 | [diff] [blame] | 159 | if fp is not None: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 160 | self._parse(fp) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 161 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 162 | def _parse(self, fp): |
| 163 | pass |
| 164 | |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 165 | def add_fallback(self, fallback): |
| 166 | if self._fallback: |
| 167 | self._fallback.add_fallback(fallback) |
| 168 | else: |
| 169 | self._fallback = fallback |
| 170 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 171 | def gettext(self, message): |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 172 | if self._fallback: |
| 173 | return self._fallback.gettext(message) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 174 | return message |
| 175 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 176 | def lgettext(self, message): |
| 177 | if self._fallback: |
| 178 | return self._fallback.lgettext(message) |
| 179 | return message |
| 180 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 181 | def ngettext(self, msgid1, msgid2, n): |
| 182 | if self._fallback: |
| 183 | return self._fallback.ngettext(msgid1, msgid2, n) |
| 184 | if n == 1: |
| 185 | return msgid1 |
| 186 | else: |
| 187 | return msgid2 |
| 188 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 189 | def lngettext(self, msgid1, msgid2, n): |
| 190 | if self._fallback: |
| 191 | return self._fallback.lngettext(msgid1, msgid2, n) |
| 192 | if n == 1: |
| 193 | return msgid1 |
| 194 | else: |
| 195 | return msgid2 |
| 196 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 197 | def info(self): |
| 198 | return self._info |
| 199 | |
| 200 | def charset(self): |
| 201 | return self._charset |
| 202 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 203 | def output_charset(self): |
| 204 | return self._output_charset |
| 205 | |
| 206 | def set_output_charset(self, charset): |
| 207 | self._output_charset = charset |
| 208 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 209 | def install(self, names=None): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 210 | import builtins |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 211 | builtins.__dict__['_'] = self.gettext |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 212 | if hasattr(names, "__contains__"): |
| 213 | if "gettext" in names: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 214 | builtins.__dict__['gettext'] = builtins.__dict__['_'] |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 215 | if "ngettext" in names: |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 216 | builtins.__dict__['ngettext'] = self.ngettext |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 217 | if "lgettext" in names: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 218 | builtins.__dict__['lgettext'] = self.lgettext |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 219 | if "lngettext" in names: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 220 | builtins.__dict__['lngettext'] = self.lngettext |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 221 | |
| 222 | |
| 223 | class GNUTranslations(NullTranslations): |
| 224 | # Magic number of .mo files |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 225 | LE_MAGIC = 0x950412de |
| 226 | BE_MAGIC = 0xde120495 |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 227 | |
| 228 | def _parse(self, fp): |
| 229 | """Override this method to support alternative .mo formats.""" |
| 230 | unpack = struct.unpack |
| 231 | filename = getattr(fp, 'name', '') |
| 232 | # Parse the .mo file header, which consists of 5 little endian 32 |
| 233 | # bit words. |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 234 | self._catalog = catalog = {} |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 235 | self.plural = lambda n: int(n != 1) # germanic plural by default |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 236 | buf = fp.read() |
Barry Warsaw | 9a2d9d7 | 2000-08-31 23:28:52 +0000 | [diff] [blame] | 237 | buflen = len(buf) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 238 | # Are we big endian or little endian? |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 239 | magic = unpack('<I', buf[:4])[0] |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 240 | if magic == self.LE_MAGIC: |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 241 | version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20]) |
| 242 | ii = '<II' |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 243 | elif magic == self.BE_MAGIC: |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 244 | version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20]) |
| 245 | ii = '>II' |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 246 | else: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 247 | raise IOError(0, 'Bad magic number', filename) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 248 | # Now put all messages from the .mo file buffer into the catalog |
| 249 | # dictionary. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 250 | for i in range(0, msgcount): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 251 | mlen, moff = unpack(ii, buf[masteridx:masteridx+8]) |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 252 | mend = moff + mlen |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 253 | tlen, toff = unpack(ii, buf[transidx:transidx+8]) |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 254 | tend = toff + tlen |
Barry Warsaw | 9a2d9d7 | 2000-08-31 23:28:52 +0000 | [diff] [blame] | 255 | if mend < buflen and tend < buflen: |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 256 | msg = buf[moff:mend] |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 257 | tmsg = buf[toff:tend] |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 258 | else: |
| 259 | raise IOError(0, 'File is corrupt', filename) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 260 | # See if we're looking at GNU .mo conventions for metadata |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 261 | if mlen == 0: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 262 | # Catalog description |
Barry Warsaw | b8c7876 | 2003-10-04 02:28:31 +0000 | [diff] [blame] | 263 | lastk = k = None |
Christian Heimes | 6ae5d7f | 2007-10-31 18:53:44 +0000 | [diff] [blame] | 264 | for b_item in tmsg.split('\n'.encode("ascii")): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 265 | item = b_item.decode().strip() |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 266 | if not item: |
| 267 | continue |
Barry Warsaw | 7de63f5 | 2003-05-20 17:26:48 +0000 | [diff] [blame] | 268 | if ':' in item: |
| 269 | k, v = item.split(':', 1) |
| 270 | k = k.strip().lower() |
| 271 | v = v.strip() |
| 272 | self._info[k] = v |
| 273 | lastk = k |
| 274 | elif lastk: |
| 275 | self._info[lastk] += '\n' + item |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 276 | if k == 'content-type': |
| 277 | self._charset = v.split('charset=')[1] |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 278 | elif k == 'plural-forms': |
| 279 | v = v.split(';') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 280 | plural = v[1].split('plural=')[1] |
| 281 | self.plural = c2py(plural) |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 282 | # Note: we unconditionally convert both msgids and msgstrs to |
| 283 | # Unicode using the character encoding specified in the charset |
| 284 | # parameter of the Content-Type header. The gettext documentation |
| 285 | # strongly encourages msgids to be us-ascii, but some appliations |
| 286 | # require alternative encodings (e.g. Zope's ZCML and ZPT). For |
| 287 | # traditional gettext applications, the msgid conversion will |
| 288 | # cause no problems since us-ascii should always be a subset of |
| 289 | # the charset encoding. We may want to fall back to 8-bit msgids |
| 290 | # if the Unicode conversion fails. |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 291 | charset = self._charset or 'ascii' |
Guido van Rossum | 652f446 | 2007-07-12 08:04:06 +0000 | [diff] [blame] | 292 | if b'\x00' in msg: |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 293 | # Plural forms |
Guido van Rossum | 9600f93 | 2007-08-29 03:08:55 +0000 | [diff] [blame] | 294 | msgid1, msgid2 = msg.split(b'\x00') |
| 295 | tmsg = tmsg.split(b'\x00') |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 296 | msgid1 = str(msgid1, charset) |
| 297 | for i, x in enumerate(tmsg): |
| 298 | catalog[(msgid1, i)] = str(x, charset) |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 299 | else: |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 300 | catalog[str(msg, charset)] = str(tmsg, charset) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 301 | # advance to next entry in the seek tables |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 302 | masteridx += 8 |
| 303 | transidx += 8 |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 304 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 305 | def lgettext(self, message): |
| 306 | missing = object() |
| 307 | tmsg = self._catalog.get(message, missing) |
| 308 | if tmsg is missing: |
| 309 | if self._fallback: |
| 310 | return self._fallback.lgettext(message) |
| 311 | return message |
| 312 | if self._output_charset: |
| 313 | return tmsg.encode(self._output_charset) |
| 314 | return tmsg.encode(locale.getpreferredencoding()) |
| 315 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 316 | def lngettext(self, msgid1, msgid2, n): |
| 317 | try: |
| 318 | tmsg = self._catalog[(msgid1, self.plural(n))] |
| 319 | if self._output_charset: |
| 320 | return tmsg.encode(self._output_charset) |
| 321 | return tmsg.encode(locale.getpreferredencoding()) |
| 322 | except KeyError: |
| 323 | if self._fallback: |
| 324 | return self._fallback.lngettext(msgid1, msgid2, n) |
| 325 | if n == 1: |
| 326 | return msgid1 |
| 327 | else: |
| 328 | return msgid2 |
| 329 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 330 | def gettext(self, message): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 331 | missing = object() |
| 332 | tmsg = self._catalog.get(message, missing) |
| 333 | if tmsg is missing: |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 334 | if self._fallback: |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 335 | return self._fallback.gettext(message) |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 336 | return message |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 337 | return tmsg |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 338 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 339 | def ngettext(self, msgid1, msgid2, n): |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 340 | try: |
| 341 | tmsg = self._catalog[(msgid1, self.plural(n))] |
| 342 | except KeyError: |
| 343 | if self._fallback: |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 344 | return self._fallback.ngettext(msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 345 | if n == 1: |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 346 | tmsg = msgid1 |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 347 | else: |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 348 | tmsg = msgid2 |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 349 | return tmsg |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 350 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 351 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 352 | # Locate a .mo file using the gettext strategy |
Georg Brandl | cd86925 | 2009-05-17 12:50:58 +0000 | [diff] [blame] | 353 | def find(domain, localedir=None, languages=None, all=False): |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 354 | # Get some reasonable defaults for arguments that were not supplied |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 355 | if localedir is None: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 356 | localedir = _default_localedir |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 357 | if languages is None: |
| 358 | languages = [] |
| 359 | for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): |
| 360 | val = os.environ.get(envar) |
| 361 | if val: |
| 362 | languages = val.split(':') |
| 363 | break |
| 364 | if 'C' not in languages: |
| 365 | languages.append('C') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 366 | # now normalize and expand the languages |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 367 | nelangs = [] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 368 | for lang in languages: |
| 369 | for nelang in _expand_lang(lang): |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 370 | if nelang not in nelangs: |
| 371 | nelangs.append(nelang) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 372 | # select a language |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 373 | if all: |
| 374 | result = [] |
| 375 | else: |
| 376 | result = None |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 377 | for lang in nelangs: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 378 | if lang == 'C': |
| 379 | break |
Barry Warsaw | 84314b7 | 2000-08-25 19:53:17 +0000 | [diff] [blame] | 380 | mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 381 | if os.path.exists(mofile): |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 382 | if all: |
| 383 | result.append(mofile) |
| 384 | else: |
| 385 | return mofile |
| 386 | return result |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 387 | |
| 388 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 389 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 390 | # a mapping between absolute .mo file path and Translation object |
| 391 | _translations = {} |
| 392 | |
Martin v. Löwis | 1be6419 | 2002-01-11 06:33:28 +0000 | [diff] [blame] | 393 | def translation(domain, localedir=None, languages=None, |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 394 | class_=None, fallback=False, codeset=None): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 395 | if class_ is None: |
| 396 | class_ = GNUTranslations |
Georg Brandl | cd86925 | 2009-05-17 12:50:58 +0000 | [diff] [blame] | 397 | mofiles = find(domain, localedir, languages, all=True) |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 398 | if not mofiles: |
Martin v. Löwis | 1be6419 | 2002-01-11 06:33:28 +0000 | [diff] [blame] | 399 | if fallback: |
| 400 | return NullTranslations() |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 401 | raise IOError(ENOENT, 'No translation file found for domain', domain) |
Barry Warsaw | 293b03f | 2000-10-05 18:48:12 +0000 | [diff] [blame] | 402 | # Avoid opening, reading, and parsing the .mo file after it's been done |
| 403 | # once. |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 404 | result = None |
| 405 | for mofile in mofiles: |
Éric Araujo | 6108bf5 | 2010-10-04 23:52:37 +0000 | [diff] [blame] | 406 | key = (class_, os.path.abspath(mofile)) |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 407 | t = _translations.get(key) |
| 408 | if t is None: |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 409 | with open(mofile, 'rb') as fp: |
| 410 | t = _translations.setdefault(key, class_(fp)) |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 411 | # Copy the translation object to allow setting fallbacks and |
| 412 | # output charset. All other instance data is shared with the |
| 413 | # cached object. |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 414 | t = copy.copy(t) |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 415 | if codeset: |
| 416 | t.set_output_charset(codeset) |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 417 | if result is None: |
| 418 | result = t |
| 419 | else: |
| 420 | result.add_fallback(t) |
| 421 | return result |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 422 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 423 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 424 | def install(domain, localedir=None, codeset=None, names=None): |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 425 | t = translation(domain, localedir, fallback=True, codeset=codeset) |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 426 | t.install(names) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 427 | |
| 428 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 429 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 430 | # a mapping b/w domains and locale directories |
| 431 | _localedirs = {} |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 432 | # a mapping b/w domains and codesets |
| 433 | _localecodesets = {} |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 434 | # current global domain, `messages' used for compatibility w/ GNU gettext |
| 435 | _current_domain = 'messages' |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 436 | |
| 437 | |
| 438 | def textdomain(domain=None): |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 439 | global _current_domain |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 440 | if domain is not None: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 441 | _current_domain = domain |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 442 | return _current_domain |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 443 | |
| 444 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 445 | def bindtextdomain(domain, localedir=None): |
| 446 | global _localedirs |
| 447 | if localedir is not None: |
| 448 | _localedirs[domain] = localedir |
| 449 | return _localedirs.get(domain, _default_localedir) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 450 | |
| 451 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 452 | def bind_textdomain_codeset(domain, codeset=None): |
| 453 | global _localecodesets |
| 454 | if codeset is not None: |
| 455 | _localecodesets[domain] = codeset |
| 456 | return _localecodesets.get(domain) |
| 457 | |
| 458 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 459 | def dgettext(domain, message): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 460 | try: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 461 | t = translation(domain, _localedirs.get(domain, None), |
| 462 | codeset=_localecodesets.get(domain)) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 463 | except IOError: |
| 464 | return message |
| 465 | return t.gettext(message) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 466 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 467 | def ldgettext(domain, message): |
| 468 | try: |
| 469 | t = translation(domain, _localedirs.get(domain, None), |
| 470 | codeset=_localecodesets.get(domain)) |
| 471 | except IOError: |
| 472 | return message |
| 473 | return t.lgettext(message) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 474 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 475 | def dngettext(domain, msgid1, msgid2, n): |
| 476 | try: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 477 | t = translation(domain, _localedirs.get(domain, None), |
| 478 | codeset=_localecodesets.get(domain)) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 479 | except IOError: |
| 480 | if n == 1: |
| 481 | return msgid1 |
| 482 | else: |
| 483 | return msgid2 |
| 484 | return t.ngettext(msgid1, msgid2, n) |
| 485 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 486 | def ldngettext(domain, msgid1, msgid2, n): |
| 487 | try: |
| 488 | t = translation(domain, _localedirs.get(domain, None), |
| 489 | codeset=_localecodesets.get(domain)) |
| 490 | except IOError: |
| 491 | if n == 1: |
| 492 | return msgid1 |
| 493 | else: |
| 494 | return msgid2 |
| 495 | return t.lngettext(msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 496 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 497 | def gettext(message): |
| 498 | return dgettext(_current_domain, message) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 499 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 500 | def lgettext(message): |
| 501 | return ldgettext(_current_domain, message) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 502 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 503 | def ngettext(msgid1, msgid2, n): |
| 504 | return dngettext(_current_domain, msgid1, msgid2, n) |
| 505 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 506 | def lngettext(msgid1, msgid2, n): |
| 507 | return ldngettext(_current_domain, msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 508 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 509 | # dcgettext() has been deemed unnecessary and is not implemented. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 510 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 511 | # James Henstridge's Catalog constructor from GNOME gettext. Documented usage |
| 512 | # was: |
| 513 | # |
| 514 | # import gettext |
| 515 | # cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR) |
| 516 | # _ = cat.gettext |
| 517 | # print _('Hello World') |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 518 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 519 | # The resulting catalog object currently don't support access through a |
| 520 | # dictionary API, which was supported (but apparently unused) in GNOME |
| 521 | # gettext. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 522 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 523 | Catalog = translation |