/usr/lib/python2.7/dist-packages/flufl/i18n/_translator.py is in python-flufl.i18n 1.1.3-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | # Copyright (C) 2009-2014 by Barry A. Warsaw
#
# This file is part of flufl.i18n
#
# flufl.i18n is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# flufl.i18n is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with flufl.i18n. If not, see <http://www.gnu.org/licenses/>.
"""Basic translation context class."""
from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
'Translator',
]
import sys
import textwrap
from flufl.i18n._substitute import attrdict, Template
from flufl.i18n._expand import expand
try:
unicode_type = unicode
except NameError:
# Python 3
unicode_type = str
class Translator:
"""A translation context."""
def __init__(self, catalog, dedent=True, depth=2):
"""Create a translation context.
:param catalog: The translation catalog.
:type catalog: `gettext.NullTranslations` or subclass
:param dedent: Whether the input string should be dedented.
:type dedent: bool
:param depth: Number of stack frames to call sys._getframe() with.
:type depth: int
"""
self._catalog = catalog
self.dedent = dedent
self.depth = depth
# Python 3's .gettext() returns unicodes.
try:
self._gettext = self._catalog.ugettext
except AttributeError:
# Must be Python 3, right?
self._gettext = self._catalog.gettext
def translate(self, original, extras=None):
"""Translate the string.
:param original: The original string to translate.
:type original: string
:param extras: Extra substitution mapping, elements of which override
the locals and globals.
:return: The translated string.
:rtype: string
"""
if original == '':
return ''
assert original, 'Cannot translate: {0}'.format(original)
# Because the original string is what the text extractors put into the
# catalog, we must first look up the original unadulterated string in
# the catalog. Use the global translation context for this.
#
# Translations must be unicode safe internally. The translation
# service is one boundary to the outside world, so to honor this
# constraint, make sure that all strings to come out of this are
# unicodes, even if the translated string or dictionary values are
# 8-bit strings.
tns = self._gettext(original)
charset = self._catalog.charset() or 'us-ascii'
# Do PEP 292 style $-string interpolation into the resulting string.
#
# This lets you write something like:
#
# now = time.ctime(time.time())
# print _('The current time is: $now')
#
# and have it Just Work. Key precedence is:
#
# extras > locals > globals
#
# Get the frame of the caller.
frame = sys._getframe(self.depth)
# Create the raw dictionary of substitutions.
raw_dict = frame.f_globals.copy()
raw_dict.update(frame.f_locals)
if extras is not None:
raw_dict.update(extras)
# Python 2 requires ** dictionaries to have str, not unicode keys.
# For our purposes, keys should always be ascii. Values though should
# be unicode.
translated_string = expand(tns, attrdict(raw_dict), Template)
# Use the bytes type here instead of str for better compatibility with
# 2to3, which transforms this code into trying to decode a unicode to
# a unicode.
if isinstance(translated_string, bytes):
translated_string = unicode_type(translated_string, charset)
# Dedent the string if so desired.
if self.dedent:
translated_string = textwrap.dedent(translated_string)
return translated_string
|