2000-08-26 03:13:37 +08:00
|
|
|
"""Internationalization and localization support.
|
|
|
|
|
|
|
|
This module provides internationalization (I18N) and localization (L10N)
|
|
|
|
support for your Python programs by providing an interface to the GNU gettext
|
|
|
|
message catalog library.
|
|
|
|
|
|
|
|
I18N refers to the operation by which a program is made aware of multiple
|
|
|
|
languages. L10N refers to the adaptation of your program, once
|
2000-08-30 11:29:58 +08:00
|
|
|
internationalized, to the local language and cultural habits.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2000-08-26 04:26:43 +08:00
|
|
|
# This module represents the integration of work, contributions, feedback, and
|
|
|
|
# suggestions from the following people:
|
2000-08-26 03:13:37 +08:00
|
|
|
#
|
|
|
|
# Martin von Loewis, who wrote the initial implementation of the underlying
|
|
|
|
# C-based libintlmodule (later renamed _gettext), along with a skeletal
|
|
|
|
# gettext.py implementation.
|
|
|
|
#
|
|
|
|
# Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule,
|
|
|
|
# which also included a pure-Python implementation to read .mo files if
|
|
|
|
# intlmodule wasn't available.
|
|
|
|
#
|
|
|
|
# James Henstridge, who also wrote a gettext.py module, which has some
|
|
|
|
# interesting, but currently unsupported experimental features: the notion of
|
|
|
|
# a Catalog class and instances, and the ability to add to a catalog file via
|
|
|
|
# a Python API.
|
|
|
|
#
|
|
|
|
# Barry Warsaw integrated these modules, wrote the .install() API and code,
|
|
|
|
# and conformed all C and Python code to Python's coding standards.
|
2000-08-30 11:29:58 +08:00
|
|
|
#
|
|
|
|
# Francois Pinard and Marc-Andre Lemburg also contributed valuably to this
|
|
|
|
# module.
|
|
|
|
#
|
2003-03-11 00:01:43 +08:00
|
|
|
# J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs.
|
2002-11-22 05:45:32 +08:00
|
|
|
#
|
2000-08-30 11:29:58 +08:00
|
|
|
# TODO:
|
|
|
|
# - Lazy loading of .mo files. Currently the entire catalog is loaded into
|
|
|
|
# memory, but that's probably bad for large translated programs. Instead,
|
|
|
|
# the lexical sort of original strings in GNU .mo files should be exploited
|
|
|
|
# to do binary searches and lazy initializations. Or you might want to use
|
|
|
|
# the undocumented double-hash algorithm for .mo files with hash tables, but
|
|
|
|
# you'll need to study the GNU gettext code to do this.
|
|
|
|
#
|
|
|
|
# - Support Solaris .mo file formats. Unfortunately, we've been unable to
|
|
|
|
# find this format documented anywhere.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
|
2017-03-27 22:05:26 +08:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
|
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
- Expose NullTranslations and GNUTranslations to __all__
- Set the default charset to iso-8859-1. It used to be None, which
would cause problems with .ugettext() if the file had no charset
parameter. Arguably, the po/mo file would be broken, but I still think
iso-8859-1 is a reasonable default.
- Add a "coerce" default argument to GNUTranslations's constructor. The
reason for this is that in Zope, we want all msgids and msgstrs to be
Unicode. For the latter, we could use .ugettext() but there isn't
currently a mechanism for Unicode-ifying msgids.
The plan then is that the charset parameter specifies the encoding for
both the msgids and msgstrs, and both are decoded to Unicode when read.
For example, we might encode po files with utf-8. I think the GNU
gettext tools don't care.
Since this could potentially break code [*] that wants to use the
encoded interface .gettext(), the constructor flag is added, defaulting
to False. Most code I suspect will want to set this to True and use
.ugettext().
- A few other minor changes from the Zope project, including asserting
that a zero-length msgid must have a Project-ID-Version header for it to
be counted as the metadata record.
2003-04-12 02:36:43 +08:00
|
|
|
__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
|
|
|
|
'find', 'translation', 'install', 'textdomain', 'bindtextdomain',
|
2015-04-13 21:58:36 +08:00
|
|
|
'bind_textdomain_codeset',
|
|
|
|
'dgettext', 'dngettext', 'gettext', 'lgettext', 'ldgettext',
|
|
|
|
'ldngettext', 'lngettext', 'ngettext',
|
2018-11-07 22:12:20 +08:00
|
|
|
'pgettext', 'dpgettext', 'npgettext', 'dnpgettext',
|
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
- Expose NullTranslations and GNUTranslations to __all__
- Set the default charset to iso-8859-1. It used to be None, which
would cause problems with .ugettext() if the file had no charset
parameter. Arguably, the po/mo file would be broken, but I still think
iso-8859-1 is a reasonable default.
- Add a "coerce" default argument to GNUTranslations's constructor. The
reason for this is that in Zope, we want all msgids and msgstrs to be
Unicode. For the latter, we could use .ugettext() but there isn't
currently a mechanism for Unicode-ifying msgids.
The plan then is that the charset parameter specifies the encoding for
both the msgids and msgstrs, and both are decoded to Unicode when read.
For example, we might encode po files with utf-8. I think the GNU
gettext tools don't care.
Since this could potentially break code [*] that wants to use the
encoded interface .gettext(), the constructor flag is added, defaulting
to False. Most code I suspect will want to set this to True and use
.ugettext().
- A few other minor changes from the Zope project, including asserting
that a zero-length msgid must have a Project-ID-Version header for it to
be counted as the metadata record.
2003-04-12 02:36:43 +08:00
|
|
|
]
|
2001-01-23 23:35:05 +08:00
|
|
|
|
2012-05-26 10:45:29 +08:00
|
|
|
_default_localedir = os.path.join(sys.base_prefix, 'share', 'locale')
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2016-11-09 03:17:46 +08:00
|
|
|
# Expression parsing for plural form selection.
|
|
|
|
#
|
|
|
|
# The gettext library supports a small subset of C syntax. The only
|
|
|
|
# incompatible difference is that integer literals starting with zero are
|
|
|
|
# decimal.
|
|
|
|
#
|
|
|
|
# https://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms
|
|
|
|
# http://git.savannah.gnu.org/cgit/gettext.git/tree/gettext-runtime/intl/plural.y
|
|
|
|
|
|
|
|
_token_pattern = re.compile(r"""
|
|
|
|
(?P<WHITESPACES>[ \t]+) | # spaces and horizontal tabs
|
|
|
|
(?P<NUMBER>[0-9]+\b) | # decimal integer
|
|
|
|
(?P<NAME>n\b) | # only n is allowed
|
|
|
|
(?P<PARENTHESIS>[()]) |
|
|
|
|
(?P<OPERATOR>[-*/%+?:]|[><!]=?|==|&&|\|\|) | # !, *, /, %, +, -, <, >,
|
|
|
|
# <=, >=, ==, !=, &&, ||,
|
|
|
|
# ? :
|
|
|
|
# unary and bitwise ops
|
|
|
|
# not allowed
|
|
|
|
(?P<INVALID>\w+|.) # invalid token
|
|
|
|
""", re.VERBOSE|re.DOTALL)
|
|
|
|
|
|
|
|
def _tokenize(plural):
|
|
|
|
for mo in re.finditer(_token_pattern, plural):
|
|
|
|
kind = mo.lastgroup
|
|
|
|
if kind == 'WHITESPACES':
|
|
|
|
continue
|
|
|
|
value = mo.group(kind)
|
|
|
|
if kind == 'INVALID':
|
|
|
|
raise ValueError('invalid token in plural form: %s' % value)
|
|
|
|
yield value
|
|
|
|
yield ''
|
|
|
|
|
|
|
|
def _error(value):
|
|
|
|
if value:
|
|
|
|
return ValueError('unexpected token in plural form: %s' % value)
|
|
|
|
else:
|
|
|
|
return ValueError('unexpected end of plural form')
|
|
|
|
|
|
|
|
_binary_ops = (
|
|
|
|
('||',),
|
|
|
|
('&&',),
|
|
|
|
('==', '!='),
|
|
|
|
('<', '>', '<=', '>='),
|
|
|
|
('+', '-'),
|
|
|
|
('*', '/', '%'),
|
|
|
|
)
|
|
|
|
_binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops}
|
|
|
|
_c2py_ops = {'||': 'or', '&&': 'and', '/': '//'}
|
|
|
|
|
|
|
|
def _parse(tokens, priority=-1):
|
|
|
|
result = ''
|
|
|
|
nexttok = next(tokens)
|
|
|
|
while nexttok == '!':
|
|
|
|
result += 'not '
|
|
|
|
nexttok = next(tokens)
|
|
|
|
|
|
|
|
if nexttok == '(':
|
|
|
|
sub, nexttok = _parse(tokens)
|
|
|
|
result = '%s(%s)' % (result, sub)
|
|
|
|
if nexttok != ')':
|
|
|
|
raise ValueError('unbalanced parenthesis in plural form')
|
|
|
|
elif nexttok == 'n':
|
|
|
|
result = '%s%s' % (result, nexttok)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
value = int(nexttok, 10)
|
|
|
|
except ValueError:
|
|
|
|
raise _error(nexttok) from None
|
|
|
|
result = '%s%d' % (result, value)
|
|
|
|
nexttok = next(tokens)
|
|
|
|
|
|
|
|
j = 100
|
|
|
|
while nexttok in _binary_ops:
|
|
|
|
i = _binary_ops[nexttok]
|
|
|
|
if i < priority:
|
|
|
|
break
|
|
|
|
# Break chained comparisons
|
|
|
|
if i in (3, 4) and j in (3, 4): # '==', '!=', '<', '>', '<=', '>='
|
|
|
|
result = '(%s)' % result
|
|
|
|
# Replace some C operators by their Python equivalents
|
|
|
|
op = _c2py_ops.get(nexttok, nexttok)
|
|
|
|
right, nexttok = _parse(tokens, i + 1)
|
|
|
|
result = '%s %s %s' % (result, op, right)
|
|
|
|
j = i
|
|
|
|
if j == priority == 4: # '<', '>', '<=', '>='
|
|
|
|
result = '(%s)' % result
|
|
|
|
|
|
|
|
if nexttok == '?' and priority <= 0:
|
|
|
|
if_true, nexttok = _parse(tokens, 0)
|
|
|
|
if nexttok != ':':
|
|
|
|
raise _error(nexttok)
|
|
|
|
if_false, nexttok = _parse(tokens)
|
|
|
|
result = '%s if %s else %s' % (if_true, result, if_false)
|
|
|
|
if priority == 0:
|
|
|
|
result = '(%s)' % result
|
|
|
|
|
|
|
|
return result, nexttok
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2016-11-15 01:22:12 +08:00
|
|
|
def _as_int(n):
|
|
|
|
try:
|
|
|
|
i = round(n)
|
|
|
|
except TypeError:
|
|
|
|
raise TypeError('Plural value must be an integer, got %s' %
|
|
|
|
(n.__class__.__name__,)) from None
|
2017-03-12 19:15:01 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('Plural value must be an integer, got %s' %
|
|
|
|
(n.__class__.__name__,),
|
|
|
|
DeprecationWarning, 4)
|
2016-11-15 01:22:12 +08:00
|
|
|
return n
|
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def c2py(plural):
|
2003-04-25 02:13:39 +08:00
|
|
|
"""Gets a C expression as used in PO files for plural forms and returns a
|
2016-11-09 03:17:46 +08:00
|
|
|
Python function that implements an equivalent expression.
|
2002-11-22 05:45:32 +08:00
|
|
|
"""
|
|
|
|
|
2016-11-09 03:17:46 +08:00
|
|
|
if len(plural) > 1000:
|
|
|
|
raise ValueError('plural form expression is too long')
|
|
|
|
try:
|
|
|
|
result, nexttok = _parse(_tokenize(plural))
|
|
|
|
if nexttok:
|
|
|
|
raise _error(nexttok)
|
|
|
|
|
|
|
|
depth = 0
|
|
|
|
for c in result:
|
|
|
|
if c == '(':
|
|
|
|
depth += 1
|
|
|
|
if depth > 20:
|
|
|
|
# Python compiler limit is about 90.
|
|
|
|
# The most complex example has 2.
|
|
|
|
raise ValueError('plural form expression is too complex')
|
|
|
|
elif c == ')':
|
|
|
|
depth -= 1
|
|
|
|
|
2016-11-15 01:22:12 +08:00
|
|
|
ns = {'_as_int': _as_int}
|
2016-11-09 03:17:46 +08:00
|
|
|
exec('''if True:
|
|
|
|
def func(n):
|
|
|
|
if not isinstance(n, int):
|
2016-11-15 01:22:12 +08:00
|
|
|
n = _as_int(n)
|
2016-11-09 03:17:46 +08:00
|
|
|
return int(%s)
|
|
|
|
''' % result, ns)
|
|
|
|
return ns['func']
|
2016-11-09 03:26:14 +08:00
|
|
|
except RecursionError:
|
2016-11-09 03:17:46 +08:00
|
|
|
# Recursion error can be raised in _parse() or exec().
|
|
|
|
raise ValueError('plural form expression is too complex')
|
2002-11-22 05:45:32 +08:00
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2010-12-24 06:53:42 +08:00
|
|
|
def _expand_lang(loc):
|
2020-05-14 09:22:30 +08:00
|
|
|
import locale
|
2010-12-24 06:53:42 +08:00
|
|
|
loc = locale.normalize(loc)
|
2000-08-26 04:26:43 +08:00
|
|
|
COMPONENT_CODESET = 1 << 0
|
|
|
|
COMPONENT_TERRITORY = 1 << 1
|
|
|
|
COMPONENT_MODIFIER = 1 << 2
|
|
|
|
# split up the locale into its base components
|
|
|
|
mask = 0
|
2010-12-24 06:53:42 +08:00
|
|
|
pos = loc.find('@')
|
2000-08-26 04:26:43 +08:00
|
|
|
if pos >= 0:
|
2010-12-24 06:53:42 +08:00
|
|
|
modifier = loc[pos:]
|
|
|
|
loc = loc[:pos]
|
2000-08-26 04:26:43 +08:00
|
|
|
mask |= COMPONENT_MODIFIER
|
|
|
|
else:
|
|
|
|
modifier = ''
|
2010-12-24 06:53:42 +08:00
|
|
|
pos = loc.find('.')
|
2000-08-26 04:26:43 +08:00
|
|
|
if pos >= 0:
|
2010-12-24 06:53:42 +08:00
|
|
|
codeset = loc[pos:]
|
|
|
|
loc = loc[:pos]
|
2000-08-26 04:26:43 +08:00
|
|
|
mask |= COMPONENT_CODESET
|
|
|
|
else:
|
|
|
|
codeset = ''
|
2010-12-24 06:53:42 +08:00
|
|
|
pos = loc.find('_')
|
2000-08-26 04:26:43 +08:00
|
|
|
if pos >= 0:
|
2010-12-24 06:53:42 +08:00
|
|
|
territory = loc[pos:]
|
|
|
|
loc = loc[:pos]
|
2000-08-26 04:26:43 +08:00
|
|
|
mask |= COMPONENT_TERRITORY
|
|
|
|
else:
|
|
|
|
territory = ''
|
2010-12-24 06:53:42 +08:00
|
|
|
language = loc
|
2000-08-26 04:26:43 +08:00
|
|
|
ret = []
|
|
|
|
for i in range(mask+1):
|
|
|
|
if not (i & ~mask): # if all components for this combo exist ...
|
|
|
|
val = language
|
|
|
|
if i & COMPONENT_TERRITORY: val += territory
|
|
|
|
if i & COMPONENT_CODESET: val += codeset
|
|
|
|
if i & COMPONENT_MODIFIER: val += modifier
|
|
|
|
ret.append(val)
|
|
|
|
ret.reverse()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
class NullTranslations:
|
|
|
|
def __init__(self, fp=None):
|
|
|
|
self._info = {}
|
2003-04-12 04:26:47 +08:00
|
|
|
self._charset = None
|
2004-07-23 02:44:01 +08:00
|
|
|
self._output_charset = None
|
2002-01-11 14:58:49 +08:00
|
|
|
self._fallback = None
|
2002-06-01 09:29:16 +08:00
|
|
|
if fp is not None:
|
2000-08-30 11:29:58 +08:00
|
|
|
self._parse(fp)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def _parse(self, fp):
|
|
|
|
pass
|
|
|
|
|
2002-01-11 14:58:49 +08:00
|
|
|
def add_fallback(self, fallback):
|
|
|
|
if self._fallback:
|
|
|
|
self._fallback.add_fallback(fallback)
|
|
|
|
else:
|
|
|
|
self._fallback = fallback
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def gettext(self, message):
|
2002-01-11 14:58:49 +08:00
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.gettext(message)
|
2000-08-30 11:29:58 +08:00
|
|
|
return message
|
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def lgettext(self, message):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('lgettext() is deprecated, use gettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
2020-05-14 09:22:30 +08:00
|
|
|
import locale
|
2004-07-23 02:44:01 +08:00
|
|
|
if self._fallback:
|
2018-10-27 13:00:41 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\blgettext\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
return self._fallback.lgettext(message)
|
2017-06-20 22:13:29 +08:00
|
|
|
if self._output_charset:
|
|
|
|
return message.encode(self._output_charset)
|
|
|
|
return message.encode(locale.getpreferredencoding())
|
2004-07-23 02:44:01 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def ngettext(self, msgid1, msgid2, n):
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.ngettext(msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
|
|
|
return msgid1
|
|
|
|
else:
|
|
|
|
return msgid2
|
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def lngettext(self, msgid1, msgid2, n):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('lngettext() is deprecated, use ngettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
2020-05-14 09:22:30 +08:00
|
|
|
import locale
|
2004-07-23 02:44:01 +08:00
|
|
|
if self._fallback:
|
2018-10-27 13:00:41 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\blngettext\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
return self._fallback.lngettext(msgid1, msgid2, n)
|
2004-07-23 02:44:01 +08:00
|
|
|
if n == 1:
|
2017-06-20 22:13:29 +08:00
|
|
|
tmsg = msgid1
|
2004-07-23 02:44:01 +08:00
|
|
|
else:
|
2017-06-20 22:13:29 +08:00
|
|
|
tmsg = msgid2
|
|
|
|
if self._output_charset:
|
|
|
|
return tmsg.encode(self._output_charset)
|
|
|
|
return tmsg.encode(locale.getpreferredencoding())
|
2004-07-23 02:44:01 +08:00
|
|
|
|
2018-11-07 22:12:20 +08:00
|
|
|
def pgettext(self, context, message):
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.pgettext(context, message)
|
|
|
|
return message
|
|
|
|
|
|
|
|
def npgettext(self, context, msgid1, msgid2, n):
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.npgettext(context, msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
|
|
|
return msgid1
|
|
|
|
else:
|
|
|
|
return msgid2
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def info(self):
|
|
|
|
return self._info
|
|
|
|
|
|
|
|
def charset(self):
|
|
|
|
return self._charset
|
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def output_charset(self):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('output_charset() is deprecated',
|
|
|
|
DeprecationWarning, 2)
|
2004-07-23 02:44:01 +08:00
|
|
|
return self._output_charset
|
|
|
|
|
|
|
|
def set_output_charset(self, charset):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('set_output_charset() is deprecated',
|
|
|
|
DeprecationWarning, 2)
|
2004-07-23 02:44:01 +08:00
|
|
|
self._output_charset = charset
|
|
|
|
|
2008-07-14 22:32:15 +08:00
|
|
|
def install(self, names=None):
|
2007-12-02 17:40:06 +08:00
|
|
|
import builtins
|
2008-07-14 22:32:15 +08:00
|
|
|
builtins.__dict__['_'] = self.gettext
|
2018-11-07 22:12:20 +08:00
|
|
|
if names is not None:
|
|
|
|
allowed = {'gettext', 'lgettext', 'lngettext',
|
|
|
|
'ngettext', 'npgettext', 'pgettext'}
|
|
|
|
for name in allowed & set(names):
|
|
|
|
builtins.__dict__[name] = getattr(self, name)
|
2000-08-30 11:29:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GNUTranslations(NullTranslations):
|
|
|
|
# Magic number of .mo files
|
2007-01-16 00:59:06 +08:00
|
|
|
LE_MAGIC = 0x950412de
|
|
|
|
BE_MAGIC = 0xde120495
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2018-11-07 22:12:20 +08:00
|
|
|
# The encoding of a msgctxt and a msgid in a .mo file is
|
|
|
|
# msgctxt + "\x04" + msgid (gettext version >= 0.15)
|
|
|
|
CONTEXT = "%s\x04%s"
|
|
|
|
|
2014-10-29 03:17:51 +08:00
|
|
|
# Acceptable .mo versions
|
|
|
|
VERSIONS = (0, 1)
|
|
|
|
|
|
|
|
def _get_versions(self, version):
|
|
|
|
"""Returns a tuple of major version, minor version"""
|
|
|
|
return (version >> 16, version & 0xffff)
|
|
|
|
|
2000-08-26 03:13:37 +08:00
|
|
|
def _parse(self, fp):
|
|
|
|
"""Override this method to support alternative .mo formats."""
|
2017-09-26 05:55:55 +08:00
|
|
|
# Delay struct import for speeding up gettext import when .mo files
|
|
|
|
# are not used.
|
|
|
|
from struct import unpack
|
2000-08-26 03:13:37 +08:00
|
|
|
filename = getattr(fp, 'name', '')
|
|
|
|
# Parse the .mo file header, which consists of 5 little endian 32
|
|
|
|
# bit words.
|
2000-08-30 11:29:58 +08:00
|
|
|
self._catalog = catalog = {}
|
2003-03-11 00:01:43 +08:00
|
|
|
self.plural = lambda n: int(n != 1) # germanic plural by default
|
2000-08-26 03:13:37 +08:00
|
|
|
buf = fp.read()
|
2000-09-01 07:28:52 +08:00
|
|
|
buflen = len(buf)
|
2000-08-30 11:29:58 +08:00
|
|
|
# Are we big endian or little endian?
|
2002-08-14 23:09:12 +08:00
|
|
|
magic = unpack('<I', buf[:4])[0]
|
2000-08-30 11:29:58 +08:00
|
|
|
if magic == self.LE_MAGIC:
|
2002-08-14 23:09:12 +08:00
|
|
|
version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
|
|
|
|
ii = '<II'
|
2000-08-30 11:29:58 +08:00
|
|
|
elif magic == self.BE_MAGIC:
|
2002-08-14 23:09:12 +08:00
|
|
|
version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
|
|
|
|
ii = '>II'
|
2000-08-30 11:29:58 +08:00
|
|
|
else:
|
2012-12-25 22:47:37 +08:00
|
|
|
raise OSError(0, 'Bad magic number', filename)
|
2014-10-29 03:17:51 +08:00
|
|
|
|
|
|
|
major_version, minor_version = self._get_versions(version)
|
|
|
|
|
|
|
|
if major_version not in self.VERSIONS:
|
|
|
|
raise OSError(0, 'Bad version number ' + str(major_version), filename)
|
|
|
|
|
2000-08-26 03:13:37 +08:00
|
|
|
# Now put all messages from the .mo file buffer into the catalog
|
|
|
|
# dictionary.
|
2007-05-08 06:24:25 +08:00
|
|
|
for i in range(0, msgcount):
|
2000-08-30 11:29:58 +08:00
|
|
|
mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
|
2002-08-14 23:09:12 +08:00
|
|
|
mend = moff + mlen
|
2000-08-30 11:29:58 +08:00
|
|
|
tlen, toff = unpack(ii, buf[transidx:transidx+8])
|
2002-08-14 23:09:12 +08:00
|
|
|
tend = toff + tlen
|
2000-09-01 07:28:52 +08:00
|
|
|
if mend < buflen and tend < buflen:
|
2002-11-22 05:45:32 +08:00
|
|
|
msg = buf[moff:mend]
|
2000-08-30 11:29:58 +08:00
|
|
|
tmsg = buf[toff:tend]
|
2000-08-26 03:13:37 +08:00
|
|
|
else:
|
2012-12-25 22:47:37 +08:00
|
|
|
raise OSError(0, 'File is corrupt', filename)
|
2000-08-30 11:29:58 +08:00
|
|
|
# See if we're looking at GNU .mo conventions for metadata
|
2003-04-12 04:26:47 +08:00
|
|
|
if mlen == 0:
|
2000-08-30 11:29:58 +08:00
|
|
|
# Catalog description
|
2015-04-13 22:38:56 +08:00
|
|
|
lastk = None
|
2017-06-20 22:13:29 +08:00
|
|
|
for b_item in tmsg.split(b'\n'):
|
2007-11-07 05:34:58 +08:00
|
|
|
item = b_item.decode().strip()
|
2000-08-30 11:29:58 +08:00
|
|
|
if not item:
|
|
|
|
continue
|
2019-05-09 22:22:15 +08:00
|
|
|
# Skip over comment lines:
|
|
|
|
if item.startswith('#-#-#-#-#') and item.endswith('#-#-#-#-#'):
|
|
|
|
continue
|
2015-04-13 22:38:56 +08:00
|
|
|
k = v = None
|
2003-05-21 01:26:48 +08:00
|
|
|
if ':' in item:
|
|
|
|
k, v = item.split(':', 1)
|
|
|
|
k = k.strip().lower()
|
|
|
|
v = v.strip()
|
|
|
|
self._info[k] = v
|
|
|
|
lastk = k
|
|
|
|
elif lastk:
|
|
|
|
self._info[lastk] += '\n' + item
|
2000-08-30 11:29:58 +08:00
|
|
|
if k == 'content-type':
|
|
|
|
self._charset = v.split('charset=')[1]
|
2002-11-22 05:45:32 +08:00
|
|
|
elif k == 'plural-forms':
|
|
|
|
v = v.split(';')
|
|
|
|
plural = v[1].split('plural=')[1]
|
|
|
|
self.plural = c2py(plural)
|
2003-04-25 02:13:39 +08:00
|
|
|
# Note: we unconditionally convert both msgids and msgstrs to
|
|
|
|
# Unicode using the character encoding specified in the charset
|
|
|
|
# parameter of the Content-Type header. The gettext documentation
|
2011-03-15 11:18:48 +08:00
|
|
|
# strongly encourages msgids to be us-ascii, but some applications
|
2003-04-25 02:13:39 +08:00
|
|
|
# require alternative encodings (e.g. Zope's ZCML and ZPT). For
|
|
|
|
# traditional gettext applications, the msgid conversion will
|
|
|
|
# cause no problems since us-ascii should always be a subset of
|
|
|
|
# the charset encoding. We may want to fall back to 8-bit msgids
|
|
|
|
# if the Unicode conversion fails.
|
2008-07-18 02:15:35 +08:00
|
|
|
charset = self._charset or 'ascii'
|
2007-07-12 16:04:06 +08:00
|
|
|
if b'\x00' in msg:
|
2003-04-12 04:26:47 +08:00
|
|
|
# Plural forms
|
2007-08-29 11:08:55 +08:00
|
|
|
msgid1, msgid2 = msg.split(b'\x00')
|
|
|
|
tmsg = tmsg.split(b'\x00')
|
2008-07-18 02:15:35 +08:00
|
|
|
msgid1 = str(msgid1, charset)
|
|
|
|
for i, x in enumerate(tmsg):
|
|
|
|
catalog[(msgid1, i)] = str(x, charset)
|
2003-04-12 04:26:47 +08:00
|
|
|
else:
|
2008-07-18 02:15:35 +08:00
|
|
|
catalog[str(msg, charset)] = str(tmsg, charset)
|
2000-08-26 03:13:37 +08:00
|
|
|
# advance to next entry in the seek tables
|
2000-08-26 04:26:43 +08:00
|
|
|
masteridx += 8
|
|
|
|
transidx += 8
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def lgettext(self, message):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('lgettext() is deprecated, use gettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
2020-05-14 09:22:30 +08:00
|
|
|
import locale
|
2004-07-23 02:44:01 +08:00
|
|
|
missing = object()
|
|
|
|
tmsg = self._catalog.get(message, missing)
|
|
|
|
if tmsg is missing:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.lgettext(message)
|
2017-06-20 22:13:29 +08:00
|
|
|
tmsg = message
|
2004-07-23 02:44:01 +08:00
|
|
|
if self._output_charset:
|
|
|
|
return tmsg.encode(self._output_charset)
|
|
|
|
return tmsg.encode(locale.getpreferredencoding())
|
|
|
|
|
|
|
|
def lngettext(self, msgid1, msgid2, n):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('lngettext() is deprecated, use ngettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
2020-05-14 09:22:30 +08:00
|
|
|
import locale
|
2004-07-23 02:44:01 +08:00
|
|
|
try:
|
|
|
|
tmsg = self._catalog[(msgid1, self.plural(n))]
|
|
|
|
except KeyError:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.lngettext(msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
2017-06-20 22:13:29 +08:00
|
|
|
tmsg = msgid1
|
2004-07-23 02:44:01 +08:00
|
|
|
else:
|
2017-06-20 22:13:29 +08:00
|
|
|
tmsg = msgid2
|
|
|
|
if self._output_charset:
|
|
|
|
return tmsg.encode(self._output_charset)
|
|
|
|
return tmsg.encode(locale.getpreferredencoding())
|
2004-07-23 02:44:01 +08:00
|
|
|
|
2008-07-14 22:32:15 +08:00
|
|
|
def gettext(self, message):
|
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
- Expose NullTranslations and GNUTranslations to __all__
- Set the default charset to iso-8859-1. It used to be None, which
would cause problems with .ugettext() if the file had no charset
parameter. Arguably, the po/mo file would be broken, but I still think
iso-8859-1 is a reasonable default.
- Add a "coerce" default argument to GNUTranslations's constructor. The
reason for this is that in Zope, we want all msgids and msgstrs to be
Unicode. For the latter, we could use .ugettext() but there isn't
currently a mechanism for Unicode-ifying msgids.
The plan then is that the charset parameter specifies the encoding for
both the msgids and msgstrs, and both are decoded to Unicode when read.
For example, we might encode po files with utf-8. I think the GNU
gettext tools don't care.
Since this could potentially break code [*] that wants to use the
encoded interface .gettext(), the constructor flag is added, defaulting
to False. Most code I suspect will want to set this to True and use
.ugettext().
- A few other minor changes from the Zope project, including asserting
that a zero-length msgid must have a Project-ID-Version header for it to
be counted as the metadata record.
2003-04-12 02:36:43 +08:00
|
|
|
missing = object()
|
|
|
|
tmsg = self._catalog.get(message, missing)
|
|
|
|
if tmsg is missing:
|
2002-01-11 14:58:49 +08:00
|
|
|
if self._fallback:
|
2008-07-14 22:32:15 +08:00
|
|
|
return self._fallback.gettext(message)
|
2008-07-18 02:15:35 +08:00
|
|
|
return message
|
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
- Expose NullTranslations and GNUTranslations to __all__
- Set the default charset to iso-8859-1. It used to be None, which
would cause problems with .ugettext() if the file had no charset
parameter. Arguably, the po/mo file would be broken, but I still think
iso-8859-1 is a reasonable default.
- Add a "coerce" default argument to GNUTranslations's constructor. The
reason for this is that in Zope, we want all msgids and msgstrs to be
Unicode. For the latter, we could use .ugettext() but there isn't
currently a mechanism for Unicode-ifying msgids.
The plan then is that the charset parameter specifies the encoding for
both the msgids and msgstrs, and both are decoded to Unicode when read.
For example, we might encode po files with utf-8. I think the GNU
gettext tools don't care.
Since this could potentially break code [*] that wants to use the
encoded interface .gettext(), the constructor flag is added, defaulting
to False. Most code I suspect will want to set this to True and use
.ugettext().
- A few other minor changes from the Zope project, including asserting
that a zero-length msgid must have a Project-ID-Version header for it to
be counted as the metadata record.
2003-04-12 02:36:43 +08:00
|
|
|
return tmsg
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2008-07-14 22:32:15 +08:00
|
|
|
def ngettext(self, msgid1, msgid2, n):
|
2002-11-22 05:45:32 +08:00
|
|
|
try:
|
|
|
|
tmsg = self._catalog[(msgid1, self.plural(n))]
|
|
|
|
except KeyError:
|
|
|
|
if self._fallback:
|
2008-07-14 22:32:15 +08:00
|
|
|
return self._fallback.ngettext(msgid1, msgid2, n)
|
2002-11-22 05:45:32 +08:00
|
|
|
if n == 1:
|
2008-07-18 02:15:35 +08:00
|
|
|
tmsg = msgid1
|
2002-11-22 05:45:32 +08:00
|
|
|
else:
|
2008-07-18 02:15:35 +08:00
|
|
|
tmsg = msgid2
|
From http://mail.python.org/pipermail/i18n-sig/2003-April/001557.html
- Expose NullTranslations and GNUTranslations to __all__
- Set the default charset to iso-8859-1. It used to be None, which
would cause problems with .ugettext() if the file had no charset
parameter. Arguably, the po/mo file would be broken, but I still think
iso-8859-1 is a reasonable default.
- Add a "coerce" default argument to GNUTranslations's constructor. The
reason for this is that in Zope, we want all msgids and msgstrs to be
Unicode. For the latter, we could use .ugettext() but there isn't
currently a mechanism for Unicode-ifying msgids.
The plan then is that the charset parameter specifies the encoding for
both the msgids and msgstrs, and both are decoded to Unicode when read.
For example, we might encode po files with utf-8. I think the GNU
gettext tools don't care.
Since this could potentially break code [*] that wants to use the
encoded interface .gettext(), the constructor flag is added, defaulting
to False. Most code I suspect will want to set this to True and use
.ugettext().
- A few other minor changes from the Zope project, including asserting
that a zero-length msgid must have a Project-ID-Version header for it to
be counted as the metadata record.
2003-04-12 02:36:43 +08:00
|
|
|
return tmsg
|
2002-11-22 05:45:32 +08:00
|
|
|
|
2018-11-07 22:12:20 +08:00
|
|
|
def pgettext(self, context, message):
|
|
|
|
ctxt_msg_id = self.CONTEXT % (context, message)
|
|
|
|
missing = object()
|
|
|
|
tmsg = self._catalog.get(ctxt_msg_id, missing)
|
|
|
|
if tmsg is missing:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.pgettext(context, message)
|
|
|
|
return message
|
|
|
|
return tmsg
|
|
|
|
|
|
|
|
def npgettext(self, context, msgid1, msgid2, n):
|
|
|
|
ctxt_msg_id = self.CONTEXT % (context, msgid1)
|
|
|
|
try:
|
|
|
|
tmsg = self._catalog[ctxt_msg_id, self.plural(n)]
|
|
|
|
except KeyError:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.npgettext(context, msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
|
|
|
tmsg = msgid1
|
|
|
|
else:
|
|
|
|
tmsg = msgid2
|
|
|
|
return tmsg
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-26 03:13:37 +08:00
|
|
|
# Locate a .mo file using the gettext strategy
|
2009-05-17 20:50:58 +08:00
|
|
|
def find(domain, localedir=None, languages=None, all=False):
|
2000-08-26 03:13:37 +08:00
|
|
|
# Get some reasonable defaults for arguments that were not supplied
|
|
|
|
if localedir is None:
|
2000-08-30 11:29:58 +08:00
|
|
|
localedir = _default_localedir
|
2000-08-26 03:13:37 +08:00
|
|
|
if languages is None:
|
|
|
|
languages = []
|
|
|
|
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
|
|
|
|
val = os.environ.get(envar)
|
|
|
|
if val:
|
|
|
|
languages = val.split(':')
|
|
|
|
break
|
|
|
|
if 'C' not in languages:
|
|
|
|
languages.append('C')
|
2000-08-26 04:26:43 +08:00
|
|
|
# now normalize and expand the languages
|
2000-10-16 23:47:50 +08:00
|
|
|
nelangs = []
|
2000-08-26 04:26:43 +08:00
|
|
|
for lang in languages:
|
|
|
|
for nelang in _expand_lang(lang):
|
2000-10-16 23:47:50 +08:00
|
|
|
if nelang not in nelangs:
|
|
|
|
nelangs.append(nelang)
|
2000-08-26 03:13:37 +08:00
|
|
|
# select a language
|
2002-01-11 14:58:49 +08:00
|
|
|
if all:
|
|
|
|
result = []
|
|
|
|
else:
|
|
|
|
result = None
|
2000-10-16 23:47:50 +08:00
|
|
|
for lang in nelangs:
|
2000-08-26 03:13:37 +08:00
|
|
|
if lang == 'C':
|
|
|
|
break
|
2000-08-26 03:53:17 +08:00
|
|
|
mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
|
2000-08-30 11:29:58 +08:00
|
|
|
if os.path.exists(mofile):
|
2002-01-11 14:58:49 +08:00
|
|
|
if all:
|
|
|
|
result.append(mofile)
|
|
|
|
else:
|
|
|
|
return mofile
|
|
|
|
return result
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# a mapping between absolute .mo file path and Translation object
|
|
|
|
_translations = {}
|
2018-10-27 13:00:41 +08:00
|
|
|
_unspecified = ['unspecified']
|
2000-08-30 11:29:58 +08:00
|
|
|
|
2002-01-11 14:33:28 +08:00
|
|
|
def translation(domain, localedir=None, languages=None,
|
2018-10-27 13:00:41 +08:00
|
|
|
class_=None, fallback=False, codeset=_unspecified):
|
2000-08-30 11:29:58 +08:00
|
|
|
if class_ is None:
|
|
|
|
class_ = GNUTranslations
|
2009-05-17 20:50:58 +08:00
|
|
|
mofiles = find(domain, localedir, languages, all=True)
|
2003-04-25 02:13:39 +08:00
|
|
|
if not mofiles:
|
2002-01-11 14:33:28 +08:00
|
|
|
if fallback:
|
|
|
|
return NullTranslations()
|
2017-09-26 05:55:55 +08:00
|
|
|
from errno import ENOENT
|
|
|
|
raise FileNotFoundError(ENOENT,
|
|
|
|
'No translation file found for domain', domain)
|
2000-10-06 02:48:12 +08:00
|
|
|
# Avoid opening, reading, and parsing the .mo file after it's been done
|
|
|
|
# once.
|
2002-01-11 14:58:49 +08:00
|
|
|
result = None
|
|
|
|
for mofile in mofiles:
|
2010-10-05 07:52:37 +08:00
|
|
|
key = (class_, os.path.abspath(mofile))
|
2002-01-11 14:58:49 +08:00
|
|
|
t = _translations.get(key)
|
|
|
|
if t is None:
|
Merged revisions 72506,72525-72526,72551,72558,72616,72654-72655,72689,72745,72750,72802,72812,72822,72824,72826-72827,72833,72876,72890,72923,72946,73026,73042,73045,73047,73065,73068-73069 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72506 | vinay.sajip | 2009-05-09 07:07:17 -0500 (Sat, 09 May 2009) | 1 line
Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
........
r72525 | benjamin.peterson | 2009-05-09 20:38:02 -0500 (Sat, 09 May 2009) | 1 line
close file explicitly
........
r72526 | benjamin.peterson | 2009-05-09 21:29:00 -0500 (Sat, 09 May 2009) | 1 line
make sure files are closed using the with statement
........
r72551 | benjamin.peterson | 2009-05-10 09:16:47 -0500 (Sun, 10 May 2009) | 1 line
use isinstance
........
r72558 | benjamin.peterson | 2009-05-10 18:52:09 -0500 (Sun, 10 May 2009) | 1 line
sys.setdefaultencoding() strikes me as a bad example
........
r72616 | benjamin.peterson | 2009-05-13 19:33:10 -0500 (Wed, 13 May 2009) | 1 line
importlib.import_module is better these days
........
r72654 | benjamin.peterson | 2009-05-14 17:37:49 -0500 (Thu, 14 May 2009) | 1 line
prevent refleaks from threads
........
r72655 | benjamin.peterson | 2009-05-14 17:40:34 -0500 (Thu, 14 May 2009) | 1 line
a useful decorator for cleaning up threads
........
r72689 | benjamin.peterson | 2009-05-16 13:44:34 -0500 (Sat, 16 May 2009) | 1 line
use skipTest()
........
r72745 | benjamin.peterson | 2009-05-17 09:16:29 -0500 (Sun, 17 May 2009) | 1 line
ignore .rst files in sphinx its self
........
r72750 | benjamin.peterson | 2009-05-17 11:59:27 -0500 (Sun, 17 May 2009) | 1 line
chop off slash
........
r72802 | georg.brandl | 2009-05-20 13:35:27 -0500 (Wed, 20 May 2009) | 1 line
#6051: refer to email examples for better way to construct email messages.
........
r72812 | michael.foord | 2009-05-21 17:57:02 -0500 (Thu, 21 May 2009) | 1 line
Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
........
r72822 | georg.brandl | 2009-05-22 04:33:25 -0500 (Fri, 22 May 2009) | 1 line
#6084: fix example.
........
r72824 | georg.brandl | 2009-05-22 04:43:17 -0500 (Fri, 22 May 2009) | 1 line
Fix references to file-related functions and methods (os.* vs file.*).
........
r72826 | georg.brandl | 2009-05-22 04:49:42 -0500 (Fri, 22 May 2009) | 1 line
Fix confusing wording.
........
r72827 | georg.brandl | 2009-05-22 04:50:30 -0500 (Fri, 22 May 2009) | 1 line
s/use/call/
........
r72833 | georg.brandl | 2009-05-22 12:00:17 -0500 (Fri, 22 May 2009) | 1 line
#6078: _warnings is a builtin module and has no standard init_warnings function.
........
r72876 | benjamin.peterson | 2009-05-23 15:59:09 -0500 (Sat, 23 May 2009) | 1 line
remove mention of old ctypes version
........
r72890 | gregory.p.smith | 2009-05-24 13:00:13 -0500 (Sun, 24 May 2009) | 2 lines
add a versionadded tag for set_tunnel
........
r72923 | michael.foord | 2009-05-25 15:36:56 -0500 (Mon, 25 May 2009) | 1 line
Make assertSequenceEqual error messages less cryptic, particularly for nested sequences.
........
r72946 | ronald.oussoren | 2009-05-26 13:44:48 -0500 (Tue, 26 May 2009) | 2 lines
Fixes issue 6110
........
r73026 | r.david.murray | 2009-05-29 14:30:27 -0500 (Fri, 29 May 2009) | 3 lines
Issue 6141: document that the first item of args is still the
command name even when executable is specified.
........
r73042 | benjamin.peterson | 2009-05-29 22:10:52 -0500 (Fri, 29 May 2009) | 1 line
no fdatasync on macos
........
r73045 | georg.brandl | 2009-05-30 02:26:04 -0500 (Sat, 30 May 2009) | 1 line
#6146: fix markup bug.
........
r73047 | georg.brandl | 2009-05-30 05:33:23 -0500 (Sat, 30 May 2009) | 1 line
Fix some more small markup problems.
........
r73065 | antoine.pitrou | 2009-05-30 16:39:25 -0500 (Sat, 30 May 2009) | 3 lines
The test for #5330 wasn't correct.
........
r73068 | antoine.pitrou | 2009-05-30 16:45:40 -0500 (Sat, 30 May 2009) | 3 lines
Update ACKS
........
r73069 | benjamin.peterson | 2009-05-30 19:42:42 -0500 (Sat, 30 May 2009) | 1 line
fix signature
........
2009-06-02 06:42:33 +08:00
|
|
|
with open(mofile, 'rb') as fp:
|
|
|
|
t = _translations.setdefault(key, class_(fp))
|
2004-07-23 02:44:01 +08:00
|
|
|
# Copy the translation object to allow setting fallbacks and
|
|
|
|
# output charset. All other instance data is shared with the
|
|
|
|
# cached object.
|
2017-09-26 05:55:55 +08:00
|
|
|
# Delay copy import for speeding up gettext import when .mo files
|
|
|
|
# are not used.
|
|
|
|
import copy
|
2002-01-11 14:58:49 +08:00
|
|
|
t = copy.copy(t)
|
2018-10-27 13:00:41 +08:00
|
|
|
if codeset is not _unspecified:
|
|
|
|
import warnings
|
|
|
|
warnings.warn('parameter codeset is deprecated',
|
|
|
|
DeprecationWarning, 2)
|
|
|
|
if codeset:
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\bset_output_charset\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
t.set_output_charset(codeset)
|
2002-01-11 14:58:49 +08:00
|
|
|
if result is None:
|
|
|
|
result = t
|
|
|
|
else:
|
|
|
|
result.add_fallback(t)
|
|
|
|
return result
|
2000-08-30 11:29:58 +08:00
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2018-10-27 13:00:41 +08:00
|
|
|
def install(domain, localedir=None, codeset=_unspecified, names=None):
|
2004-07-23 02:44:01 +08:00
|
|
|
t = translation(domain, localedir, fallback=True, codeset=codeset)
|
2008-07-14 22:32:15 +08:00
|
|
|
t.install(names)
|
2000-08-30 11:29:58 +08:00
|
|
|
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# a mapping b/w domains and locale directories
|
|
|
|
_localedirs = {}
|
2004-07-23 02:44:01 +08:00
|
|
|
# a mapping b/w domains and codesets
|
|
|
|
_localecodesets = {}
|
2000-08-30 11:29:58 +08:00
|
|
|
# current global domain, `messages' used for compatibility w/ GNU gettext
|
|
|
|
_current_domain = 'messages'
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def textdomain(domain=None):
|
|
|
|
global _current_domain
|
2000-08-30 11:29:58 +08:00
|
|
|
if domain is not None:
|
2000-08-26 03:13:37 +08:00
|
|
|
_current_domain = domain
|
2000-08-30 11:29:58 +08:00
|
|
|
return _current_domain
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def bindtextdomain(domain, localedir=None):
|
|
|
|
global _localedirs
|
|
|
|
if localedir is not None:
|
|
|
|
_localedirs[domain] = localedir
|
|
|
|
return _localedirs.get(domain, _default_localedir)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def bind_textdomain_codeset(domain, codeset=None):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('bind_textdomain_codeset() is deprecated',
|
|
|
|
DeprecationWarning, 2)
|
2004-07-23 02:44:01 +08:00
|
|
|
global _localecodesets
|
|
|
|
if codeset is not None:
|
|
|
|
_localecodesets[domain] = codeset
|
|
|
|
return _localecodesets.get(domain)
|
|
|
|
|
|
|
|
|
2000-08-26 03:13:37 +08:00
|
|
|
def dgettext(domain, message):
|
2000-08-30 11:29:58 +08:00
|
|
|
try:
|
2018-10-27 13:00:41 +08:00
|
|
|
t = translation(domain, _localedirs.get(domain, None))
|
2012-12-25 22:47:37 +08:00
|
|
|
except OSError:
|
2000-08-30 11:29:58 +08:00
|
|
|
return message
|
|
|
|
return t.gettext(message)
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def ldgettext(domain, message):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('ldgettext() is deprecated, use dgettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
2020-05-14 09:22:30 +08:00
|
|
|
import locale
|
2017-06-20 22:13:29 +08:00
|
|
|
codeset = _localecodesets.get(domain)
|
2004-07-23 02:44:01 +08:00
|
|
|
try:
|
2018-10-27 13:00:41 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\bparameter codeset\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
t = translation(domain, _localedirs.get(domain, None), codeset=codeset)
|
2012-12-25 22:47:37 +08:00
|
|
|
except OSError:
|
2017-06-20 22:13:29 +08:00
|
|
|
return message.encode(codeset or locale.getpreferredencoding())
|
2018-10-27 13:00:41 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\blgettext\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
return t.lgettext(message)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def dngettext(domain, msgid1, msgid2, n):
|
|
|
|
try:
|
2018-10-27 13:00:41 +08:00
|
|
|
t = translation(domain, _localedirs.get(domain, None))
|
2012-12-25 22:47:37 +08:00
|
|
|
except OSError:
|
2002-11-22 05:45:32 +08:00
|
|
|
if n == 1:
|
|
|
|
return msgid1
|
|
|
|
else:
|
|
|
|
return msgid2
|
|
|
|
return t.ngettext(msgid1, msgid2, n)
|
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def ldngettext(domain, msgid1, msgid2, n):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('ldngettext() is deprecated, use dngettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
2020-05-14 09:22:30 +08:00
|
|
|
import locale
|
2017-06-20 22:13:29 +08:00
|
|
|
codeset = _localecodesets.get(domain)
|
2004-07-23 02:44:01 +08:00
|
|
|
try:
|
2018-10-27 13:00:41 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\bparameter codeset\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
t = translation(domain, _localedirs.get(domain, None), codeset=codeset)
|
2012-12-25 22:47:37 +08:00
|
|
|
except OSError:
|
2004-07-23 02:44:01 +08:00
|
|
|
if n == 1:
|
2017-06-20 22:13:29 +08:00
|
|
|
tmsg = msgid1
|
2004-07-23 02:44:01 +08:00
|
|
|
else:
|
2017-06-20 22:13:29 +08:00
|
|
|
tmsg = msgid2
|
|
|
|
return tmsg.encode(codeset or locale.getpreferredencoding())
|
2018-10-27 13:00:41 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\blngettext\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
return t.lngettext(msgid1, msgid2, n)
|
2002-11-22 05:45:32 +08:00
|
|
|
|
2018-11-07 22:12:20 +08:00
|
|
|
|
|
|
|
def dpgettext(domain, context, message):
|
|
|
|
try:
|
|
|
|
t = translation(domain, _localedirs.get(domain, None))
|
|
|
|
except OSError:
|
|
|
|
return message
|
|
|
|
return t.pgettext(context, message)
|
|
|
|
|
|
|
|
|
|
|
|
def dnpgettext(domain, context, msgid1, msgid2, n):
|
|
|
|
try:
|
|
|
|
t = translation(domain, _localedirs.get(domain, None))
|
|
|
|
except OSError:
|
|
|
|
if n == 1:
|
|
|
|
return msgid1
|
|
|
|
else:
|
|
|
|
return msgid2
|
|
|
|
return t.npgettext(context, msgid1, msgid2, n)
|
|
|
|
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def gettext(message):
|
|
|
|
return dgettext(_current_domain, message)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def lgettext(message):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('lgettext() is deprecated, use gettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\bldgettext\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
return ldgettext(_current_domain, message)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def ngettext(msgid1, msgid2, n):
|
|
|
|
return dngettext(_current_domain, msgid1, msgid2, n)
|
|
|
|
|
2004-07-23 02:44:01 +08:00
|
|
|
def lngettext(msgid1, msgid2, n):
|
2018-10-27 13:00:41 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn('lngettext() is deprecated, use ngettext() instead',
|
|
|
|
DeprecationWarning, 2)
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore', r'.*\bldngettext\b.*',
|
|
|
|
DeprecationWarning)
|
|
|
|
return ldngettext(_current_domain, msgid1, msgid2, n)
|
2002-11-22 05:45:32 +08:00
|
|
|
|
2018-11-07 22:12:20 +08:00
|
|
|
|
|
|
|
def pgettext(context, message):
|
|
|
|
return dpgettext(_current_domain, context, message)
|
|
|
|
|
|
|
|
|
|
|
|
def npgettext(context, msgid1, msgid2, n):
|
|
|
|
return dnpgettext(_current_domain, context, msgid1, msgid2, n)
|
|
|
|
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# dcgettext() has been deemed unnecessary and is not implemented.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# James Henstridge's Catalog constructor from GNOME gettext. Documented usage
|
|
|
|
# was:
|
|
|
|
#
|
|
|
|
# import gettext
|
|
|
|
# cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR)
|
|
|
|
# _ = cat.gettext
|
|
|
|
# print _('Hello World')
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# The resulting catalog object currently don't support access through a
|
|
|
|
# dictionary API, which was supported (but apparently unused) in GNOME
|
|
|
|
# gettext.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
Catalog = translation
|