Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 1 | """Internationalization and localization support. |
| 2 | |
| 3 | This module provides internationalization (I18N) and localization (L10N) |
| 4 | support for your Python programs by providing an interface to the GNU gettext |
| 5 | message catalog library. |
| 6 | |
| 7 | I18N refers to the operation by which a program is made aware of multiple |
| 8 | languages. L10N refers to the adaptation of your program, once |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 9 | internationalized, to the local language and cultural habits. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 10 | |
| 11 | """ |
| 12 | |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 13 | # This module represents the integration of work, contributions, feedback, and |
| 14 | # suggestions from the following people: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 15 | # |
| 16 | # Martin von Loewis, who wrote the initial implementation of the underlying |
| 17 | # C-based libintlmodule (later renamed _gettext), along with a skeletal |
| 18 | # gettext.py implementation. |
| 19 | # |
| 20 | # Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule, |
| 21 | # which also included a pure-Python implementation to read .mo files if |
| 22 | # intlmodule wasn't available. |
| 23 | # |
| 24 | # James Henstridge, who also wrote a gettext.py module, which has some |
| 25 | # interesting, but currently unsupported experimental features: the notion of |
| 26 | # a Catalog class and instances, and the ability to add to a catalog file via |
| 27 | # a Python API. |
| 28 | # |
| 29 | # Barry Warsaw integrated these modules, wrote the .install() API and code, |
| 30 | # and conformed all C and Python code to Python's coding standards. |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 31 | # |
| 32 | # Francois Pinard and Marc-Andre Lemburg also contributed valuably to this |
| 33 | # module. |
| 34 | # |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 35 | # J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs. |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 36 | # |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 37 | # TODO: |
| 38 | # - Lazy loading of .mo files. Currently the entire catalog is loaded into |
| 39 | # memory, but that's probably bad for large translated programs. Instead, |
| 40 | # the lexical sort of original strings in GNU .mo files should be exploited |
| 41 | # to do binary searches and lazy initializations. Or you might want to use |
| 42 | # the undocumented double-hash algorithm for .mo files with hash tables, but |
| 43 | # you'll need to study the GNU gettext code to do this. |
| 44 | # |
| 45 | # - Support Solaris .mo file formats. Unfortunately, we've been unable to |
| 46 | # find this format documented anywhere. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 48 | |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 49 | import locale, copy, io, os, re, struct, sys |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 50 | from errno import ENOENT |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 51 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 52 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 53 | __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', |
| 54 | 'find', 'translation', 'install', 'textdomain', 'bindtextdomain', |
Andrew Kuchling | 770b08e | 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 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 60 | _default_localedir = os.path.join(sys.base_prefix, 'share', 'locale') |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 61 | |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +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 |
| 70 | |
| 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) |
| 83 | |
| 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 '' |
| 94 | |
| 95 | def _error(value): |
| 96 | if value: |
| 97 | return ValueError('unexpected token in plural form: %s' % value) |
| 98 | else: |
| 99 | return ValueError('unexpected end of plural form') |
| 100 | |
| 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) from None |
| 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 |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 160 | |
Serhiy Storchaka | 60ac989 | 2016-11-14 19:22:12 +0200 | [diff] [blame] | 161 | def _as_int(n): |
| 162 | try: |
| 163 | i = round(n) |
| 164 | except TypeError: |
| 165 | raise TypeError('Plural value must be an integer, got %s' % |
| 166 | (n.__class__.__name__,)) from None |
Serhiy Storchaka | f659598 | 2017-03-12 13:15:01 +0200 | [diff] [blame^] | 167 | import warnings |
| 168 | warnings.warn('Plural value must be an integer, got %s' % |
| 169 | (n.__class__.__name__,), |
| 170 | DeprecationWarning, 4) |
Serhiy Storchaka | 60ac989 | 2016-11-14 19:22:12 +0200 | [diff] [blame] | 171 | return n |
| 172 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 173 | def c2py(plural): |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 174 | """Gets a C expression as used in PO files for plural forms and returns a |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +0200 | [diff] [blame] | 175 | Python function that implements an equivalent expression. |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 176 | """ |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +0200 | [diff] [blame] | 177 | |
| 178 | if len(plural) > 1000: |
| 179 | raise ValueError('plural form expression is too long') |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 180 | try: |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +0200 | [diff] [blame] | 181 | result, nexttok = _parse(_tokenize(plural)) |
| 182 | if nexttok: |
| 183 | raise _error(nexttok) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 184 | |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +0200 | [diff] [blame] | 185 | depth = 0 |
| 186 | for c in result: |
| 187 | if c == '(': |
| 188 | depth += 1 |
| 189 | if depth > 20: |
| 190 | # Python compiler limit is about 90. |
| 191 | # The most complex example has 2. |
| 192 | raise ValueError('plural form expression is too complex') |
| 193 | elif c == ')': |
| 194 | depth -= 1 |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 195 | |
Serhiy Storchaka | 60ac989 | 2016-11-14 19:22:12 +0200 | [diff] [blame] | 196 | ns = {'_as_int': _as_int} |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +0200 | [diff] [blame] | 197 | exec('''if True: |
| 198 | def func(n): |
| 199 | if not isinstance(n, int): |
Serhiy Storchaka | 60ac989 | 2016-11-14 19:22:12 +0200 | [diff] [blame] | 200 | n = _as_int(n) |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +0200 | [diff] [blame] | 201 | return int(%s) |
| 202 | ''' % result, ns) |
| 203 | return ns['func'] |
Serhiy Storchaka | eb20fca | 2016-11-08 21:26:14 +0200 | [diff] [blame] | 204 | except RecursionError: |
Serhiy Storchaka | 07bcf05 | 2016-11-08 21:17:46 +0200 | [diff] [blame] | 205 | # Recursion error can be raised in _parse() or exec(). |
| 206 | raise ValueError('plural form expression is too complex') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 207 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 208 | |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 209 | def _expand_lang(loc): |
| 210 | loc = locale.normalize(loc) |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 211 | COMPONENT_CODESET = 1 << 0 |
| 212 | COMPONENT_TERRITORY = 1 << 1 |
| 213 | COMPONENT_MODIFIER = 1 << 2 |
| 214 | # split up the locale into its base components |
| 215 | mask = 0 |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 216 | pos = loc.find('@') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 217 | if pos >= 0: |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 218 | modifier = loc[pos:] |
| 219 | loc = loc[:pos] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 220 | mask |= COMPONENT_MODIFIER |
| 221 | else: |
| 222 | modifier = '' |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 223 | pos = loc.find('.') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 224 | if pos >= 0: |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 225 | codeset = loc[pos:] |
| 226 | loc = loc[:pos] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 227 | mask |= COMPONENT_CODESET |
| 228 | else: |
| 229 | codeset = '' |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 230 | pos = loc.find('_') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 231 | if pos >= 0: |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 232 | territory = loc[pos:] |
| 233 | loc = loc[:pos] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 234 | mask |= COMPONENT_TERRITORY |
| 235 | else: |
| 236 | territory = '' |
Benjamin Peterson | 31e8720 | 2010-12-23 22:53:42 +0000 | [diff] [blame] | 237 | language = loc |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 238 | ret = [] |
| 239 | for i in range(mask+1): |
| 240 | if not (i & ~mask): # if all components for this combo exist ... |
| 241 | val = language |
| 242 | if i & COMPONENT_TERRITORY: val += territory |
| 243 | if i & COMPONENT_CODESET: val += codeset |
| 244 | if i & COMPONENT_MODIFIER: val += modifier |
| 245 | ret.append(val) |
| 246 | ret.reverse() |
| 247 | return ret |
| 248 | |
| 249 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 250 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 251 | class NullTranslations: |
| 252 | def __init__(self, fp=None): |
| 253 | self._info = {} |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 254 | self._charset = None |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 255 | self._output_charset = None |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 256 | self._fallback = None |
Raymond Hettinger | 094662a | 2002-06-01 01:29:16 +0000 | [diff] [blame] | 257 | if fp is not None: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 258 | self._parse(fp) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 259 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 260 | def _parse(self, fp): |
| 261 | pass |
| 262 | |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 263 | def add_fallback(self, fallback): |
| 264 | if self._fallback: |
| 265 | self._fallback.add_fallback(fallback) |
| 266 | else: |
| 267 | self._fallback = fallback |
| 268 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 269 | def gettext(self, message): |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 270 | if self._fallback: |
| 271 | return self._fallback.gettext(message) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 272 | return message |
| 273 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 274 | def lgettext(self, message): |
| 275 | if self._fallback: |
| 276 | return self._fallback.lgettext(message) |
| 277 | return message |
| 278 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 279 | def ngettext(self, msgid1, msgid2, n): |
| 280 | if self._fallback: |
| 281 | return self._fallback.ngettext(msgid1, msgid2, n) |
| 282 | if n == 1: |
| 283 | return msgid1 |
| 284 | else: |
| 285 | return msgid2 |
| 286 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 287 | def lngettext(self, msgid1, msgid2, n): |
| 288 | if self._fallback: |
| 289 | return self._fallback.lngettext(msgid1, msgid2, n) |
| 290 | if n == 1: |
| 291 | return msgid1 |
| 292 | else: |
| 293 | return msgid2 |
| 294 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 295 | def info(self): |
| 296 | return self._info |
| 297 | |
| 298 | def charset(self): |
| 299 | return self._charset |
| 300 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 301 | def output_charset(self): |
| 302 | return self._output_charset |
| 303 | |
| 304 | def set_output_charset(self, charset): |
| 305 | self._output_charset = charset |
| 306 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 307 | def install(self, names=None): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 308 | import builtins |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 309 | builtins.__dict__['_'] = self.gettext |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 310 | if hasattr(names, "__contains__"): |
| 311 | if "gettext" in names: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 312 | builtins.__dict__['gettext'] = builtins.__dict__['_'] |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 313 | if "ngettext" in names: |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 314 | builtins.__dict__['ngettext'] = self.ngettext |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 315 | if "lgettext" in names: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 316 | builtins.__dict__['lgettext'] = self.lgettext |
Georg Brandl | 602b9ba | 2006-02-19 13:26:36 +0000 | [diff] [blame] | 317 | if "lngettext" in names: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 318 | builtins.__dict__['lngettext'] = self.lngettext |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 319 | |
| 320 | |
| 321 | class GNUTranslations(NullTranslations): |
| 322 | # Magic number of .mo files |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 323 | LE_MAGIC = 0x950412de |
| 324 | BE_MAGIC = 0xde120495 |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | be8d06f | 2014-10-28 20:17:51 +0100 | [diff] [blame] | 326 | # Acceptable .mo versions |
| 327 | VERSIONS = (0, 1) |
| 328 | |
| 329 | def _get_versions(self, version): |
| 330 | """Returns a tuple of major version, minor version""" |
| 331 | return (version >> 16, version & 0xffff) |
| 332 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 333 | def _parse(self, fp): |
| 334 | """Override this method to support alternative .mo formats.""" |
| 335 | unpack = struct.unpack |
| 336 | filename = getattr(fp, 'name', '') |
| 337 | # Parse the .mo file header, which consists of 5 little endian 32 |
| 338 | # bit words. |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 339 | self._catalog = catalog = {} |
Martin v. Löwis | a57dccd | 2003-03-10 16:01:43 +0000 | [diff] [blame] | 340 | self.plural = lambda n: int(n != 1) # germanic plural by default |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 341 | buf = fp.read() |
Barry Warsaw | 9a2d9d7 | 2000-08-31 23:28:52 +0000 | [diff] [blame] | 342 | buflen = len(buf) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 343 | # Are we big endian or little endian? |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 344 | magic = unpack('<I', buf[:4])[0] |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 345 | if magic == self.LE_MAGIC: |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 346 | version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20]) |
| 347 | ii = '<II' |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 348 | elif magic == self.BE_MAGIC: |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 349 | version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20]) |
| 350 | ii = '>II' |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 351 | else: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 352 | raise OSError(0, 'Bad magic number', filename) |
Antoine Pitrou | be8d06f | 2014-10-28 20:17:51 +0100 | [diff] [blame] | 353 | |
| 354 | major_version, minor_version = self._get_versions(version) |
| 355 | |
| 356 | if major_version not in self.VERSIONS: |
| 357 | raise OSError(0, 'Bad version number ' + str(major_version), filename) |
| 358 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 359 | # Now put all messages from the .mo file buffer into the catalog |
| 360 | # dictionary. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 361 | for i in range(0, msgcount): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 362 | mlen, moff = unpack(ii, buf[masteridx:masteridx+8]) |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 363 | mend = moff + mlen |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 364 | tlen, toff = unpack(ii, buf[transidx:transidx+8]) |
Barry Warsaw | 09707e3 | 2002-08-14 15:09:12 +0000 | [diff] [blame] | 365 | tend = toff + tlen |
Barry Warsaw | 9a2d9d7 | 2000-08-31 23:28:52 +0000 | [diff] [blame] | 366 | if mend < buflen and tend < buflen: |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 367 | msg = buf[moff:mend] |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 368 | tmsg = buf[toff:tend] |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 369 | else: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 370 | raise OSError(0, 'File is corrupt', filename) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 371 | # See if we're looking at GNU .mo conventions for metadata |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 372 | if mlen == 0: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 373 | # Catalog description |
Andrew Kuchling | 8b963c5 | 2015-04-13 10:38:56 -0400 | [diff] [blame] | 374 | lastk = None |
Christian Heimes | 6ae5d7f | 2007-10-31 18:53:44 +0000 | [diff] [blame] | 375 | for b_item in tmsg.split('\n'.encode("ascii")): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 376 | item = b_item.decode().strip() |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 377 | if not item: |
| 378 | continue |
Andrew Kuchling | 8b963c5 | 2015-04-13 10:38:56 -0400 | [diff] [blame] | 379 | k = v = None |
Barry Warsaw | 7de63f5 | 2003-05-20 17:26:48 +0000 | [diff] [blame] | 380 | if ':' in item: |
| 381 | k, v = item.split(':', 1) |
| 382 | k = k.strip().lower() |
| 383 | v = v.strip() |
| 384 | self._info[k] = v |
| 385 | lastk = k |
| 386 | elif lastk: |
| 387 | self._info[lastk] += '\n' + item |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 388 | if k == 'content-type': |
| 389 | self._charset = v.split('charset=')[1] |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 390 | elif k == 'plural-forms': |
| 391 | v = v.split(';') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 392 | plural = v[1].split('plural=')[1] |
| 393 | self.plural = c2py(plural) |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 394 | # Note: we unconditionally convert both msgids and msgstrs to |
| 395 | # Unicode using the character encoding specified in the charset |
| 396 | # parameter of the Content-Type header. The gettext documentation |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 397 | # strongly encourages msgids to be us-ascii, but some applications |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 398 | # require alternative encodings (e.g. Zope's ZCML and ZPT). For |
| 399 | # traditional gettext applications, the msgid conversion will |
| 400 | # cause no problems since us-ascii should always be a subset of |
| 401 | # the charset encoding. We may want to fall back to 8-bit msgids |
| 402 | # if the Unicode conversion fails. |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 403 | charset = self._charset or 'ascii' |
Guido van Rossum | 652f446 | 2007-07-12 08:04:06 +0000 | [diff] [blame] | 404 | if b'\x00' in msg: |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 405 | # Plural forms |
Guido van Rossum | 9600f93 | 2007-08-29 03:08:55 +0000 | [diff] [blame] | 406 | msgid1, msgid2 = msg.split(b'\x00') |
| 407 | tmsg = tmsg.split(b'\x00') |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 408 | msgid1 = str(msgid1, charset) |
| 409 | for i, x in enumerate(tmsg): |
| 410 | catalog[(msgid1, i)] = str(x, charset) |
Barry Warsaw | 6008cbd | 2003-04-11 20:26:47 +0000 | [diff] [blame] | 411 | else: |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 412 | catalog[str(msg, charset)] = str(tmsg, charset) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 413 | # advance to next entry in the seek tables |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 414 | masteridx += 8 |
| 415 | transidx += 8 |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 416 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 417 | def lgettext(self, message): |
| 418 | missing = object() |
| 419 | tmsg = self._catalog.get(message, missing) |
| 420 | if tmsg is missing: |
| 421 | if self._fallback: |
| 422 | return self._fallback.lgettext(message) |
| 423 | return message |
| 424 | if self._output_charset: |
| 425 | return tmsg.encode(self._output_charset) |
| 426 | return tmsg.encode(locale.getpreferredencoding()) |
| 427 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 428 | def lngettext(self, msgid1, msgid2, n): |
| 429 | try: |
| 430 | tmsg = self._catalog[(msgid1, self.plural(n))] |
| 431 | if self._output_charset: |
| 432 | return tmsg.encode(self._output_charset) |
| 433 | return tmsg.encode(locale.getpreferredencoding()) |
| 434 | except KeyError: |
| 435 | if self._fallback: |
| 436 | return self._fallback.lngettext(msgid1, msgid2, n) |
| 437 | if n == 1: |
| 438 | return msgid1 |
| 439 | else: |
| 440 | return msgid2 |
| 441 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 442 | def gettext(self, message): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 443 | missing = object() |
| 444 | tmsg = self._catalog.get(message, missing) |
| 445 | if tmsg is missing: |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 446 | if self._fallback: |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 447 | return self._fallback.gettext(message) |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 448 | return message |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 449 | return tmsg |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 450 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 451 | def ngettext(self, msgid1, msgid2, n): |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 452 | try: |
| 453 | tmsg = self._catalog[(msgid1, self.plural(n))] |
| 454 | except KeyError: |
| 455 | if self._fallback: |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 456 | return self._fallback.ngettext(msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 457 | if n == 1: |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 458 | tmsg = msgid1 |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 459 | else: |
Georg Brandl | bded4d3 | 2008-07-17 18:15:35 +0000 | [diff] [blame] | 460 | tmsg = msgid2 |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 461 | return tmsg |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 462 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 463 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 464 | # Locate a .mo file using the gettext strategy |
Georg Brandl | cd86925 | 2009-05-17 12:50:58 +0000 | [diff] [blame] | 465 | def find(domain, localedir=None, languages=None, all=False): |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 466 | # Get some reasonable defaults for arguments that were not supplied |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 467 | if localedir is None: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 468 | localedir = _default_localedir |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 469 | if languages is None: |
| 470 | languages = [] |
| 471 | for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): |
| 472 | val = os.environ.get(envar) |
| 473 | if val: |
| 474 | languages = val.split(':') |
| 475 | break |
| 476 | if 'C' not in languages: |
| 477 | languages.append('C') |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 478 | # now normalize and expand the languages |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 479 | nelangs = [] |
Barry Warsaw | fa488ec | 2000-08-25 20:26:43 +0000 | [diff] [blame] | 480 | for lang in languages: |
| 481 | for nelang in _expand_lang(lang): |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 482 | if nelang not in nelangs: |
| 483 | nelangs.append(nelang) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 484 | # select a language |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 485 | if all: |
| 486 | result = [] |
| 487 | else: |
| 488 | result = None |
Barry Warsaw | 75f8101 | 2000-10-16 15:47:50 +0000 | [diff] [blame] | 489 | for lang in nelangs: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 490 | if lang == 'C': |
| 491 | break |
Barry Warsaw | 84314b7 | 2000-08-25 19:53:17 +0000 | [diff] [blame] | 492 | mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 493 | if os.path.exists(mofile): |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 494 | if all: |
| 495 | result.append(mofile) |
| 496 | else: |
| 497 | return mofile |
| 498 | return result |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 499 | |
| 500 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 501 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 502 | # a mapping between absolute .mo file path and Translation object |
| 503 | _translations = {} |
| 504 | |
Martin v. Löwis | 1be6419 | 2002-01-11 06:33:28 +0000 | [diff] [blame] | 505 | def translation(domain, localedir=None, languages=None, |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 506 | class_=None, fallback=False, codeset=None): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 507 | if class_ is None: |
| 508 | class_ = GNUTranslations |
Georg Brandl | cd86925 | 2009-05-17 12:50:58 +0000 | [diff] [blame] | 509 | mofiles = find(domain, localedir, languages, all=True) |
Barry Warsaw | c4acc2b | 2003-04-24 18:13:39 +0000 | [diff] [blame] | 510 | if not mofiles: |
Martin v. Löwis | 1be6419 | 2002-01-11 06:33:28 +0000 | [diff] [blame] | 511 | if fallback: |
| 512 | return NullTranslations() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 513 | raise OSError(ENOENT, 'No translation file found for domain', domain) |
Barry Warsaw | 293b03f | 2000-10-05 18:48:12 +0000 | [diff] [blame] | 514 | # Avoid opening, reading, and parsing the .mo file after it's been done |
| 515 | # once. |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 516 | result = None |
| 517 | for mofile in mofiles: |
Éric Araujo | 6108bf5 | 2010-10-04 23:52:37 +0000 | [diff] [blame] | 518 | key = (class_, os.path.abspath(mofile)) |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 519 | t = _translations.get(key) |
| 520 | if t is None: |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 521 | with open(mofile, 'rb') as fp: |
| 522 | t = _translations.setdefault(key, class_(fp)) |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 523 | # Copy the translation object to allow setting fallbacks and |
| 524 | # output charset. All other instance data is shared with the |
| 525 | # cached object. |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 526 | t = copy.copy(t) |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 527 | if codeset: |
| 528 | t.set_output_charset(codeset) |
Martin v. Löwis | a55ffae | 2002-01-11 06:58:49 +0000 | [diff] [blame] | 529 | if result is None: |
| 530 | result = t |
| 531 | else: |
| 532 | result.add_fallback(t) |
| 533 | return result |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 534 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 535 | |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 536 | def install(domain, localedir=None, codeset=None, names=None): |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 537 | t = translation(domain, localedir, fallback=True, codeset=codeset) |
Benjamin Peterson | 801844d | 2008-07-14 14:32:15 +0000 | [diff] [blame] | 538 | t.install(names) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 539 | |
| 540 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 541 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 542 | # a mapping b/w domains and locale directories |
| 543 | _localedirs = {} |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 544 | # a mapping b/w domains and codesets |
| 545 | _localecodesets = {} |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 546 | # current global domain, `messages' used for compatibility w/ GNU gettext |
| 547 | _current_domain = 'messages' |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 548 | |
| 549 | |
| 550 | def textdomain(domain=None): |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 551 | global _current_domain |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 552 | if domain is not None: |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 553 | _current_domain = domain |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 554 | return _current_domain |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 555 | |
| 556 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 557 | def bindtextdomain(domain, localedir=None): |
| 558 | global _localedirs |
| 559 | if localedir is not None: |
| 560 | _localedirs[domain] = localedir |
| 561 | return _localedirs.get(domain, _default_localedir) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 562 | |
| 563 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 564 | def bind_textdomain_codeset(domain, codeset=None): |
| 565 | global _localecodesets |
| 566 | if codeset is not None: |
| 567 | _localecodesets[domain] = codeset |
| 568 | return _localecodesets.get(domain) |
| 569 | |
| 570 | |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 571 | def dgettext(domain, message): |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 572 | try: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 573 | t = translation(domain, _localedirs.get(domain, None), |
| 574 | codeset=_localecodesets.get(domain)) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 575 | except OSError: |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 576 | return message |
| 577 | return t.gettext(message) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 578 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 579 | def ldgettext(domain, message): |
| 580 | try: |
| 581 | t = translation(domain, _localedirs.get(domain, None), |
| 582 | codeset=_localecodesets.get(domain)) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 583 | except OSError: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 584 | return message |
| 585 | return t.lgettext(message) |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 586 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 587 | def dngettext(domain, msgid1, msgid2, n): |
| 588 | try: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 589 | t = translation(domain, _localedirs.get(domain, None), |
| 590 | codeset=_localecodesets.get(domain)) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 591 | except OSError: |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 592 | if n == 1: |
| 593 | return msgid1 |
| 594 | else: |
| 595 | return msgid2 |
| 596 | return t.ngettext(msgid1, msgid2, n) |
| 597 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 598 | def ldngettext(domain, msgid1, msgid2, n): |
| 599 | try: |
| 600 | t = translation(domain, _localedirs.get(domain, None), |
| 601 | codeset=_localecodesets.get(domain)) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 602 | except OSError: |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 603 | if n == 1: |
| 604 | return msgid1 |
| 605 | else: |
| 606 | return msgid2 |
| 607 | return t.lngettext(msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 608 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 609 | def gettext(message): |
| 610 | return dgettext(_current_domain, message) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 611 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 612 | def lgettext(message): |
| 613 | return ldgettext(_current_domain, message) |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 614 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 615 | def ngettext(msgid1, msgid2, n): |
| 616 | return dngettext(_current_domain, msgid1, msgid2, n) |
| 617 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 618 | def lngettext(msgid1, msgid2, n): |
| 619 | return ldngettext(_current_domain, msgid1, msgid2, n) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 620 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 621 | # dcgettext() has been deemed unnecessary and is not implemented. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 622 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 623 | # James Henstridge's Catalog constructor from GNOME gettext. Documented usage |
| 624 | # was: |
| 625 | # |
| 626 | # import gettext |
| 627 | # cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR) |
| 628 | # _ = cat.gettext |
| 629 | # print _('Hello World') |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 630 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 631 | # The resulting catalog object currently don't support access through a |
| 632 | # dictionary API, which was supported (but apparently unused) in GNOME |
| 633 | # gettext. |
Barry Warsaw | 95be23d | 2000-08-25 19:13:37 +0000 | [diff] [blame] | 634 | |
Barry Warsaw | 33d8d70 | 2000-08-30 03:29:58 +0000 | [diff] [blame] | 635 | Catalog = translation |