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