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', |
Andrew Kuchling | 2ca7bb0 | 2015-04-13 09:58:36 -0400 | [diff] [blame] | 55 | 'bind_textdomain_codeset', |
| 56 | 'dgettext', 'dngettext', 'gettext', 'lgettext', 'ldgettext', |
| 57 | 'ldngettext', 'lngettext', 'ngettext', |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 58 | ] |
Skip Montanaro | 2dd4276 | 2001-01-23 15:35:05 +0000 | [diff] [blame] | 59 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 60 | _default_localedir = os.path.join(sys.prefix, 'share', 'locale') |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 61 | |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 62 | # Expression parsing for plural form selection. |
| 63 | # |
| 64 | # The gettext library supports a small subset of C syntax. The only |
| 65 | # incompatible difference is that integer literals starting with zero are |
| 66 | # decimal. |
| 67 | # |
| 68 | # https://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms |
| 69 | # http://git.savannah.gnu.org/cgit/gettext.git/tree/gettext-runtime/intl/plural.y |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 70 | |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 71 | _token_pattern = re.compile(r""" |
| 72 | (?P<WHITESPACES>[ \t]+) | # spaces and horizontal tabs |
| 73 | (?P<NUMBER>[0-9]+\b) | # decimal integer |
| 74 | (?P<NAME>n\b) | # only n is allowed |
| 75 | (?P<PARENTHESIS>[()]) | |
| 76 | (?P<OPERATOR>[-*/%+?:]|[><!]=?|==|&&|\|\|) | # !, *, /, %, +, -, <, >, |
| 77 | # <=, >=, ==, !=, &&, ||, |
| 78 | # ? : |
| 79 | # unary and bitwise ops |
| 80 | # not allowed |
| 81 | (?P<INVALID>\w+|.) # invalid token |
| 82 | """, re.VERBOSE|re.DOTALL) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 83 | |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 84 | def _tokenize(plural): |
| 85 | for mo in re.finditer(_token_pattern, plural): |
| 86 | kind = mo.lastgroup |
| 87 | if kind == 'WHITESPACES': |
| 88 | continue |
| 89 | value = mo.group(kind) |
| 90 | if kind == 'INVALID': |
| 91 | raise ValueError('invalid token in plural form: %s' % value) |
| 92 | yield value |
| 93 | yield '' |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 94 | |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 95 | def _error(value): |
| 96 | if value: |
| 97 | return ValueError('unexpected token in plural form: %s' % value) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 98 | else: |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 99 | return ValueError('unexpected end of plural form') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 100 | |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 101 | _binary_ops = ( |
| 102 | ('||',), |
| 103 | ('&&',), |
| 104 | ('==', '!='), |
| 105 | ('<', '>', '<=', '>='), |
| 106 | ('+', '-'), |
| 107 | ('*', '/', '%'), |
| 108 | ) |
| 109 | _binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops} |
| 110 | _c2py_ops = {'||': 'or', '&&': 'and', '/': '//'} |
| 111 | |
| 112 | def _parse(tokens, priority=-1): |
| 113 | result = '' |
| 114 | nexttok = next(tokens) |
| 115 | while nexttok == '!': |
| 116 | result += 'not ' |
| 117 | nexttok = next(tokens) |
| 118 | |
| 119 | if nexttok == '(': |
| 120 | sub, nexttok = _parse(tokens) |
| 121 | result = '%s(%s)' % (result, sub) |
| 122 | if nexttok != ')': |
| 123 | raise ValueError('unbalanced parenthesis in plural form') |
| 124 | elif nexttok == 'n': |
| 125 | result = '%s%s' % (result, nexttok) |
| 126 | else: |
| 127 | try: |
| 128 | value = int(nexttok, 10) |
| 129 | except ValueError: |
| 130 | raise _error(nexttok) |
| 131 | result = '%s%d' % (result, value) |
| 132 | nexttok = next(tokens) |
| 133 | |
| 134 | j = 100 |
| 135 | while nexttok in _binary_ops: |
| 136 | i = _binary_ops[nexttok] |
| 137 | if i < priority: |
| 138 | break |
| 139 | # Break chained comparisons |
| 140 | if i in (3, 4) and j in (3, 4): # '==', '!=', '<', '>', '<=', '>=' |
| 141 | result = '(%s)' % result |
| 142 | # Replace some C operators by their Python equivalents |
| 143 | op = _c2py_ops.get(nexttok, nexttok) |
| 144 | right, nexttok = _parse(tokens, i + 1) |
| 145 | result = '%s %s %s' % (result, op, right) |
| 146 | j = i |
| 147 | if j == priority == 4: # '<', '>', '<=', '>=' |
| 148 | result = '(%s)' % result |
| 149 | |
| 150 | if nexttok == '?' and priority <= 0: |
| 151 | if_true, nexttok = _parse(tokens, 0) |
| 152 | if nexttok != ':': |
| 153 | raise _error(nexttok) |
| 154 | if_false, nexttok = _parse(tokens) |
| 155 | result = '%s if %s else %s' % (if_true, result, if_false) |
| 156 | if priority == 0: |
| 157 | result = '(%s)' % result |
| 158 | |
| 159 | return result, nexttok |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 160 | |
| 161 | def c2py(plural): |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 162 | """Gets a C expression as used in PO files for plural forms and returns a |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 163 | Python function that implements an equivalent expression. |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 164 | """ |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 165 | |
| 166 | if len(plural) > 1000: |
| 167 | raise ValueError('plural form expression is too long') |
Raymond Hettinger | a617271 | 2004-12-31 19:15:26 +0000 | [diff] [blame] | 168 | try: |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 169 | result, nexttok = _parse(_tokenize(plural)) |
| 170 | if nexttok: |
| 171 | raise _error(nexttok) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 172 | |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 173 | depth = 0 |
| 174 | for c in result: |
| 175 | if c == '(': |
| 176 | depth += 1 |
| 177 | if depth > 20: |
| 178 | # Python compiler limit is about 90. |
| 179 | # The most complex example has 2. |
| 180 | raise ValueError('plural form expression is too complex') |
| 181 | elif c == ')': |
| 182 | depth -= 1 |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 183 | |
Serhiy Storchaka | a876027 | 2016-11-08 21:15:55 +0200 | [diff] [blame] | 184 | ns = {} |
| 185 | exec('''if 1: |
| 186 | def func(n): |
| 187 | if not isinstance(n, int): |
| 188 | raise ValueError('Plural value must be an integer.') |
| 189 | return int(%s) |
| 190 | ''' % result, ns) |
| 191 | return ns['func'] |
| 192 | except RuntimeError: |
| 193 | # Recursion error can be raised in _parse() or exec(). |
| 194 | raise ValueError('plural form expression is too complex') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 195 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 196 | |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 197 | def _expand_lang(locale): |
| 198 | from locale import normalize |
| 199 | locale = normalize(locale) |
| 200 | COMPONENT_CODESET = 1 << 0 |
| 201 | COMPONENT_TERRITORY = 1 << 1 |
| 202 | COMPONENT_MODIFIER = 1 << 2 |
| 203 | # split up the locale into its base components |
| 204 | mask = 0 |
| 205 | pos = locale.find('@') |
| 206 | if pos >= 0: |
| 207 | modifier = locale[pos:] |
| 208 | locale = locale[:pos] |
| 209 | mask |= COMPONENT_MODIFIER |
| 210 | else: |
| 211 | modifier = '' |
| 212 | pos = locale.find('.') |
| 213 | if pos >= 0: |
| 214 | codeset = locale[pos:] |
| 215 | locale = locale[:pos] |
| 216 | mask |= COMPONENT_CODESET |
| 217 | else: |
| 218 | codeset = '' |
| 219 | pos = locale.find('_') |
| 220 | if pos >= 0: |
| 221 | territory = locale[pos:] |
| 222 | locale = locale[:pos] |
| 223 | mask |= COMPONENT_TERRITORY |
| 224 | else: |
| 225 | territory = '' |
| 226 | language = locale |
| 227 | ret = [] |
| 228 | for i in range(mask+1): |
| 229 | if not (i & ~mask): # if all components for this combo exist ... |
| 230 | val = language |
| 231 | if i & COMPONENT_TERRITORY: val += territory |
| 232 | if i & COMPONENT_CODESET: val += codeset |
| 233 | if i & COMPONENT_MODIFIER: val += modifier |
| 234 | ret.append(val) |
| 235 | ret.reverse() |
| 236 | return ret |
| 237 | |
| 238 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 239 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 240 | class NullTranslations: |
| 241 | def __init__(self, fp=None): |
| 242 | self._info = {} |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 243 | self._charset = None |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 244 | self._output_charset = None |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 245 | self._fallback = None |
Raymond Hettinger | 094662a | 2002-06-01 01:29:16 +0000 | [diff] [blame] | 246 | if fp is not None: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 247 | self._parse(fp) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 248 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 249 | def _parse(self, fp): |
| 250 | pass |
| 251 | |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 252 | def add_fallback(self, fallback): |
| 253 | if self._fallback: |
| 254 | self._fallback.add_fallback(fallback) |
| 255 | else: |
| 256 | self._fallback = fallback |
| 257 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 258 | def gettext(self, message): |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 259 | if self._fallback: |
| 260 | return self._fallback.gettext(message) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 261 | return message |
| 262 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 263 | def lgettext(self, message): |
| 264 | if self._fallback: |
| 265 | return self._fallback.lgettext(message) |
| 266 | return message |
| 267 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 268 | def ngettext(self, msgid1, msgid2, n): |
| 269 | if self._fallback: |
| 270 | return self._fallback.ngettext(msgid1, msgid2, n) |
| 271 | if n == 1: |
| 272 | return msgid1 |
| 273 | else: |
| 274 | return msgid2 |
| 275 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 276 | def lngettext(self, msgid1, msgid2, n): |
| 277 | if self._fallback: |
| 278 | return self._fallback.lngettext(msgid1, msgid2, n) |
| 279 | if n == 1: |
| 280 | return msgid1 |
| 281 | else: |
| 282 | return msgid2 |
| 283 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 284 | def ugettext(self, message): |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 285 | if self._fallback: |
| 286 | return self._fallback.ugettext(message) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 287 | return unicode(message) |
| 288 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 289 | def ungettext(self, msgid1, msgid2, n): |
| 290 | if self._fallback: |
| 291 | return self._fallback.ungettext(msgid1, msgid2, n) |
| 292 | if n == 1: |
| 293 | return unicode(msgid1) |
| 294 | else: |
| 295 | return unicode(msgid2) |
| 296 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 297 | def info(self): |
| 298 | return self._info |
| 299 | |
| 300 | def charset(self): |
| 301 | return self._charset |
| 302 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 303 | def output_charset(self): |
| 304 | return self._output_charset |
| 305 | |
| 306 | def set_output_charset(self, charset): |
| 307 | self._output_charset = charset |
| 308 | |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 309 | def install(self, unicode=False, names=None): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 310 | import __builtin__ |
| 311 | __builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 312 | if hasattr(names, "__contains__"): |
| 313 | if "gettext" in names: |
| 314 | __builtin__.__dict__['gettext'] = __builtin__.__dict__['_'] |
| 315 | if "ngettext" in names: |
| 316 | __builtin__.__dict__['ngettext'] = (unicode and self.ungettext |
| 317 | or self.ngettext) |
| 318 | if "lgettext" in names: |
| 319 | __builtin__.__dict__['lgettext'] = self.lgettext |
| 320 | if "lngettext" in names: |
| 321 | __builtin__.__dict__['lngettext'] = self.lngettext |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 322 | |
| 323 | |
| 324 | class GNUTranslations(NullTranslations): |
| 325 | # Magic number of .mo files |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 326 | LE_MAGIC = 0x950412deL |
| 327 | BE_MAGIC = 0xde120495L |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 328 | |
| 329 | def _parse(self, fp): |
| 330 | """Override this method to support alternative .mo formats.""" |
| 331 | unpack = struct.unpack |
| 332 | filename = getattr(fp, 'name', '') |
| 333 | # Parse the .mo file header, which consists of 5 little endian 32 |
| 334 | # bit words. |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 335 | self._catalog = catalog = {} |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 336 | self.plural = lambda n: int(n != 1) # germanic plural by default |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 337 | buf = fp.read() |
Barry Warsaw | 9a2d9d7 | 2000-08-31 23:28:52 +0000 | [diff] [blame] | 338 | buflen = len(buf) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 339 | # Are we big endian or little endian? |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 340 | magic = unpack('<I', buf[:4])[0] |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 341 | if magic == self.LE_MAGIC: |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 342 | version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20]) |
| 343 | ii = '<II' |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 344 | elif magic == self.BE_MAGIC: |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 345 | version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20]) |
| 346 | ii = '>II' |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 347 | else: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 348 | raise IOError(0, 'Bad magic number', filename) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 349 | # Now put all messages from the .mo file buffer into the catalog |
| 350 | # dictionary. |
| 351 | for i in xrange(0, msgcount): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 352 | mlen, moff = unpack(ii, buf[masteridx:masteridx+8]) |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 353 | mend = moff + mlen |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 354 | tlen, toff = unpack(ii, buf[transidx:transidx+8]) |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 355 | tend = toff + tlen |
Barry Warsaw | 9a2d9d7 | 2000-08-31 23:28:52 +0000 | [diff] [blame] | 356 | if mend < buflen and tend < buflen: |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 357 | msg = buf[moff:mend] |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 358 | tmsg = buf[toff:tend] |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 359 | else: |
| 360 | raise IOError(0, 'File is corrupt', filename) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 361 | # See if we're looking at GNU .mo conventions for metadata |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 362 | if mlen == 0: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 363 | # Catalog description |
Andrew Kuchling | 270b058 | 2015-04-14 10:03:35 -0400 | [diff] [blame] | 364 | lastk = None |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 365 | for item in tmsg.splitlines(): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 366 | item = item.strip() |
| 367 | if not item: |
| 368 | continue |
Andrew Kuchling | 270b058 | 2015-04-14 10:03:35 -0400 | [diff] [blame] | 369 | k = v = None |
Barry Warsaw | 7de63f5 | 2003-05-20 17:26:48 +0000 | [diff] [blame] | 370 | if ':' in item: |
| 371 | k, v = item.split(':', 1) |
| 372 | k = k.strip().lower() |
| 373 | v = v.strip() |
| 374 | self._info[k] = v |
| 375 | lastk = k |
| 376 | elif lastk: |
| 377 | self._info[lastk] += '\n' + item |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 378 | if k == 'content-type': |
| 379 | self._charset = v.split('charset=')[1] |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 380 | elif k == 'plural-forms': |
| 381 | v = v.split(';') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 382 | plural = v[1].split('plural=')[1] |
| 383 | self.plural = c2py(plural) |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 384 | # Note: we unconditionally convert both msgids and msgstrs to |
| 385 | # Unicode using the character encoding specified in the charset |
| 386 | # parameter of the Content-Type header. The gettext documentation |
Ezio Melotti | 24b07bc | 2011-03-15 18:55:01 +0200 | [diff] [blame] | 387 | # strongly encourages msgids to be us-ascii, but some applications |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 388 | # require alternative encodings (e.g. Zope's ZCML and ZPT). For |
| 389 | # traditional gettext applications, the msgid conversion will |
| 390 | # cause no problems since us-ascii should always be a subset of |
| 391 | # the charset encoding. We may want to fall back to 8-bit msgids |
| 392 | # if the Unicode conversion fails. |
Raymond Hettinger | bac788a | 2004-05-04 09:21:43 +0000 | [diff] [blame] | 393 | if '\x00' in msg: |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 394 | # Plural forms |
| 395 | msgid1, msgid2 = msg.split('\x00') |
| 396 | tmsg = tmsg.split('\x00') |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 397 | if self._charset: |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 398 | msgid1 = unicode(msgid1, self._charset) |
| 399 | tmsg = [unicode(x, self._charset) for x in tmsg] |
| 400 | for i in range(len(tmsg)): |
| 401 | catalog[(msgid1, i)] = tmsg[i] |
| 402 | else: |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 403 | if self._charset: |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 404 | msg = unicode(msg, self._charset) |
| 405 | tmsg = unicode(tmsg, self._charset) |
| 406 | catalog[msg] = tmsg |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 407 | # advance to next entry in the seek tables |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 408 | masteridx += 8 |
| 409 | transidx += 8 |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 410 | |
| 411 | def gettext(self, message): |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 412 | missing = object() |
| 413 | tmsg = self._catalog.get(message, missing) |
| 414 | if tmsg is missing: |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 415 | if self._fallback: |
| 416 | return self._fallback.gettext(message) |
| 417 | return message |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 418 | # Encode the Unicode tmsg back to an 8-bit string, if possible |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 419 | if self._output_charset: |
| 420 | return tmsg.encode(self._output_charset) |
| 421 | elif self._charset: |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 422 | return tmsg.encode(self._charset) |
| 423 | return tmsg |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 424 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 425 | def lgettext(self, message): |
| 426 | missing = object() |
| 427 | tmsg = self._catalog.get(message, missing) |
| 428 | if tmsg is missing: |
| 429 | if self._fallback: |
| 430 | return self._fallback.lgettext(message) |
| 431 | return message |
| 432 | if self._output_charset: |
| 433 | return tmsg.encode(self._output_charset) |
| 434 | return tmsg.encode(locale.getpreferredencoding()) |
| 435 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 436 | def ngettext(self, msgid1, msgid2, n): |
| 437 | try: |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 438 | tmsg = self._catalog[(msgid1, self.plural(n))] |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 439 | if self._output_charset: |
| 440 | return tmsg.encode(self._output_charset) |
| 441 | elif self._charset: |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 442 | return tmsg.encode(self._charset) |
| 443 | return tmsg |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 444 | except KeyError: |
| 445 | if self._fallback: |
| 446 | return self._fallback.ngettext(msgid1, msgid2, n) |
| 447 | if n == 1: |
| 448 | return msgid1 |
| 449 | else: |
| 450 | return msgid2 |
| 451 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 452 | def lngettext(self, msgid1, msgid2, n): |
| 453 | try: |
| 454 | tmsg = self._catalog[(msgid1, self.plural(n))] |
| 455 | if self._output_charset: |
| 456 | return tmsg.encode(self._output_charset) |
| 457 | return tmsg.encode(locale.getpreferredencoding()) |
| 458 | except KeyError: |
| 459 | if self._fallback: |
| 460 | return self._fallback.lngettext(msgid1, msgid2, n) |
| 461 | if n == 1: |
| 462 | return msgid1 |
| 463 | else: |
| 464 | return msgid2 |
| 465 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 466 | def ugettext(self, message): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 467 | missing = object() |
| 468 | tmsg = self._catalog.get(message, missing) |
| 469 | if tmsg is missing: |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 470 | if self._fallback: |
| 471 | return self._fallback.ugettext(message) |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 472 | return unicode(message) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 473 | return tmsg |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 474 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 475 | def ungettext(self, msgid1, msgid2, n): |
| 476 | try: |
| 477 | tmsg = self._catalog[(msgid1, self.plural(n))] |
| 478 | except KeyError: |
| 479 | if self._fallback: |
| 480 | return self._fallback.ungettext(msgid1, msgid2, n) |
| 481 | if n == 1: |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 482 | tmsg = unicode(msgid1) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 483 | else: |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 484 | tmsg = unicode(msgid2) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 485 | return tmsg |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 486 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 487 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 488 | # Locate a .mo file using the gettext strategy |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 489 | def find(domain, localedir=None, languages=None, all=0): |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 490 | # Get some reasonable defaults for arguments that were not supplied |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 491 | if localedir is None: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 492 | localedir = _default_localedir |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 493 | if languages is None: |
| 494 | languages = [] |
| 495 | for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): |
| 496 | val = os.environ.get(envar) |
| 497 | if val: |
| 498 | languages = val.split(':') |
| 499 | break |
| 500 | if 'C' not in languages: |
| 501 | languages.append('C') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 502 | # now normalize and expand the languages |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 503 | nelangs = [] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 504 | for lang in languages: |
| 505 | for nelang in _expand_lang(lang): |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 506 | if nelang not in nelangs: |
| 507 | nelangs.append(nelang) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 508 | # select a language |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 509 | if all: |
| 510 | result = [] |
| 511 | else: |
| 512 | result = None |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 513 | for lang in nelangs: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 514 | if lang == 'C': |
| 515 | break |
Barry Warsaw | 84314b7 | 2000-08-25 19:53:17 +0000 | [diff] [blame] | 516 | mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 517 | if os.path.exists(mofile): |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 518 | if all: |
| 519 | result.append(mofile) |
| 520 | else: |
| 521 | return mofile |
| 522 | return result |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 523 | |
| 524 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 525 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 526 | # a mapping between absolute .mo file path and Translation object |
| 527 | _translations = {} |
| 528 | |
Martin v. Löwis | 1be6419 | 2002-01-11 06:33:28 +0000 | [diff] [blame] | 529 | def translation(domain, localedir=None, languages=None, |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 530 | class_=None, fallback=False, codeset=None): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 531 | if class_ is None: |
| 532 | class_ = GNUTranslations |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 533 | mofiles = find(domain, localedir, languages, all=1) |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 534 | if not mofiles: |
Martin v. Löwis | 1be6419 | 2002-01-11 06:33:28 +0000 | [diff] [blame] | 535 | if fallback: |
| 536 | return NullTranslations() |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 537 | raise IOError(ENOENT, 'No translation file found for domain', domain) |
Barry Warsaw | 293b03f | 2000-10-05 18:48:12 +0000 | [diff] [blame] | 538 | # Avoid opening, reading, and parsing the .mo file after it's been done |
| 539 | # once. |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 540 | result = None |
| 541 | for mofile in mofiles: |
Éric Araujo | c17776f | 2010-10-04 23:59:35 +0000 | [diff] [blame] | 542 | key = (class_, os.path.abspath(mofile)) |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 543 | t = _translations.get(key) |
| 544 | if t is None: |
Benjamin Peterson | 14c7bc2 | 2009-05-10 01:38:02 +0000 | [diff] [blame] | 545 | with open(mofile, 'rb') as fp: |
| 546 | t = _translations.setdefault(key, class_(fp)) |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 547 | # Copy the translation object to allow setting fallbacks and |
| 548 | # output charset. All other instance data is shared with the |
| 549 | # cached object. |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 550 | t = copy.copy(t) |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 551 | if codeset: |
| 552 | t.set_output_charset(codeset) |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 553 | if result is None: |
| 554 | result = t |
| 555 | else: |
| 556 | result.add_fallback(t) |
| 557 | return result |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 558 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 559 | |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 560 | def install(domain, localedir=None, unicode=False, codeset=None, names=None): |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 561 | t = translation(domain, localedir, fallback=True, codeset=codeset) |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 562 | t.install(unicode, names) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 563 | |
| 564 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 565 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 566 | # a mapping b/w domains and locale directories |
| 567 | _localedirs = {} |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 568 | # a mapping b/w domains and codesets |
| 569 | _localecodesets = {} |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 570 | # current global domain, `messages' used for compatibility w/ GNU gettext |
| 571 | _current_domain = 'messages' |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 572 | |
| 573 | |
| 574 | def textdomain(domain=None): |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 575 | global _current_domain |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 576 | if domain is not None: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 577 | _current_domain = domain |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 578 | return _current_domain |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 579 | |
| 580 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 581 | def bindtextdomain(domain, localedir=None): |
| 582 | global _localedirs |
| 583 | if localedir is not None: |
| 584 | _localedirs[domain] = localedir |
| 585 | return _localedirs.get(domain, _default_localedir) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 586 | |
| 587 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 588 | def bind_textdomain_codeset(domain, codeset=None): |
| 589 | global _localecodesets |
| 590 | if codeset is not None: |
| 591 | _localecodesets[domain] = codeset |
| 592 | return _localecodesets.get(domain) |
| 593 | |
| 594 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 595 | def dgettext(domain, message): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 596 | try: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 597 | t = translation(domain, _localedirs.get(domain, None), |
| 598 | codeset=_localecodesets.get(domain)) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 599 | except IOError: |
| 600 | return message |
| 601 | return t.gettext(message) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 602 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 603 | def ldgettext(domain, message): |
| 604 | try: |
| 605 | t = translation(domain, _localedirs.get(domain, None), |
| 606 | codeset=_localecodesets.get(domain)) |
| 607 | except IOError: |
| 608 | return message |
| 609 | return t.lgettext(message) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 610 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 611 | def dngettext(domain, msgid1, msgid2, n): |
| 612 | try: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 613 | t = translation(domain, _localedirs.get(domain, None), |
| 614 | codeset=_localecodesets.get(domain)) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 615 | except IOError: |
| 616 | if n == 1: |
| 617 | return msgid1 |
| 618 | else: |
| 619 | return msgid2 |
| 620 | return t.ngettext(msgid1, msgid2, n) |
| 621 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 622 | def ldngettext(domain, msgid1, msgid2, n): |
| 623 | try: |
| 624 | t = translation(domain, _localedirs.get(domain, None), |
| 625 | codeset=_localecodesets.get(domain)) |
| 626 | except IOError: |
| 627 | if n == 1: |
| 628 | return msgid1 |
| 629 | else: |
| 630 | return msgid2 |
| 631 | return t.lngettext(msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 632 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 633 | def gettext(message): |
| 634 | return dgettext(_current_domain, message) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 635 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 636 | def lgettext(message): |
| 637 | return ldgettext(_current_domain, message) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 638 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 639 | def ngettext(msgid1, msgid2, n): |
| 640 | return dngettext(_current_domain, msgid1, msgid2, n) |
| 641 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 642 | def lngettext(msgid1, msgid2, n): |
| 643 | return ldngettext(_current_domain, msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 644 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 645 | # dcgettext() has been deemed unnecessary and is not implemented. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 646 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 647 | # James Henstridge's Catalog constructor from GNOME gettext. Documented usage |
| 648 | # was: |
| 649 | # |
| 650 | # import gettext |
| 651 | # cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR) |
| 652 | # _ = cat.gettext |
| 653 | # print _('Hello World') |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 654 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 655 | # The resulting catalog object currently don't support access through a |
| 656 | # dictionary API, which was supported (but apparently unused) in GNOME |
| 657 | # gettext. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 658 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 659 | Catalog = translation |