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