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