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