/usr/share/pyshared/pylons/i18n/translation.py is in python-pylons 1.0.1-1.
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | """Translation/Localization functions.
Provides :mod:`gettext` translation functions via an app's
``pylons.translator`` and get/set_lang for changing the language
translated to.
"""
import os
from gettext import NullTranslations, translation
import pylons
__all__ = ['_', 'add_fallback', 'get_lang', 'gettext', 'gettext_noop',
'lazy_gettext', 'lazy_ngettext', 'lazy_ugettext', 'lazy_ungettext',
'ngettext', 'set_lang', 'ugettext', 'ungettext', 'LanguageError',
'N_']
class LanguageError(Exception):
"""Exception raised when a problem occurs with changing languages"""
pass
class LazyString(object):
"""Has a number of lazily evaluated functions replicating a
string. Just override the eval() method to produce the actual value.
This method copied from TurboGears.
"""
def __init__(self, func, *args, **kwargs):
self.func = func
self.args = args
self.kwargs = kwargs
def eval(self):
return self.func(*self.args, **self.kwargs)
def __unicode__(self):
return unicode(self.eval())
def __str__(self):
return str(self.eval())
def __mod__(self, other):
return self.eval() % other
def format(self, *args):
return self.eval().format(*args)
def lazify(func):
"""Decorator to return a lazy-evaluated version of the original"""
def newfunc(*args, **kwargs):
return LazyString(func, *args, **kwargs)
newfunc.__name__ = 'lazy_%s' % func.__name__
newfunc.__doc__ = 'Lazy-evaluated version of the %s function\n\n%s' % \
(func.__name__, func.__doc__)
return newfunc
def gettext_noop(value):
"""Mark a string for translation without translating it. Returns
value.
Used for global strings, e.g.::
foo = N_('Hello')
class Bar:
def __init__(self):
self.local_foo = _(foo)
h.set_lang('fr')
assert Bar().local_foo == 'Bonjour'
h.set_lang('es')
assert Bar().local_foo == 'Hola'
assert foo == 'Hello'
"""
return value
N_ = gettext_noop
def gettext(value):
"""Mark a string for translation. Returns the localized string of
value.
Mark a string to be localized as follows::
gettext('This should be in lots of languages')
"""
return pylons.translator.gettext(value)
lazy_gettext = lazify(gettext)
def ugettext(value):
"""Mark a string for translation. Returns the localized unicode
string of value.
Mark a string to be localized as follows::
_('This should be in lots of languages')
"""
return pylons.translator.ugettext(value)
_ = ugettext
lazy_ugettext = lazify(ugettext)
def ngettext(singular, plural, n):
"""Mark a string for translation. Returns the localized string of
the pluralized value.
This does a plural-forms lookup of a message id. ``singular`` is
used as the message id for purposes of lookup in the catalog, while
``n`` is used to determine which plural form to use. The returned
message is a string.
Mark a string to be localized as follows::
ngettext('There is %(num)d file here', 'There are %(num)d files here',
n) % {'num': n}
"""
return pylons.translator.ngettext(singular, plural, n)
lazy_ngettext = lazify(ngettext)
def ungettext(singular, plural, n):
"""Mark a string for translation. Returns the localized unicode
string of the pluralized value.
This does a plural-forms lookup of a message id. ``singular`` is
used as the message id for purposes of lookup in the catalog, while
``n`` is used to determine which plural form to use. The returned
message is a Unicode string.
Mark a string to be localized as follows::
ungettext('There is %(num)d file here', 'There are %(num)d files here',
n) % {'num': n}
"""
return pylons.translator.ungettext(singular, plural, n)
lazy_ungettext = lazify(ungettext)
def _get_translator(lang, **kwargs):
"""Utility method to get a valid translator object from a language
name"""
if not lang:
return NullTranslations()
if 'pylons_config' in kwargs:
conf = kwargs.pop('pylons_config')
else:
conf = pylons.config.current_conf()
localedir = os.path.join(conf['pylons.paths']['root'], 'i18n')
if not isinstance(lang, list):
lang = [lang]
try:
translator = translation(conf['pylons.package'], localedir,
languages=lang, **kwargs)
except IOError, ioe:
raise LanguageError('IOError: %s' % ioe)
translator.pylons_lang = lang
return translator
def set_lang(lang, set_environ=True, **kwargs):
"""Set the current language used for translations.
``lang`` should be a string or a list of strings. If a list of
strings, the first language is set as the main and the subsequent
languages are added as fallbacks.
"""
translator = _get_translator(lang, **kwargs)
if not set_environ:
return translator
environ = pylons.request.environ
environ['pylons.pylons'].translator = translator
if 'paste.registry' in environ:
environ['paste.registry'].replace(pylons.translator, translator)
def get_lang():
"""Return the current i18n language used"""
return getattr(pylons.translator, 'pylons_lang', None)
def add_fallback(lang, **kwargs):
"""Add a fallback language from which words not matched in other
languages will be translated to.
This fallback will be associated with the currently selected
language -- that is, resetting the language via set_lang() resets
the current fallbacks.
This function can be called multiple times to add multiple
fallbacks.
"""
return pylons.translator.add_fallback(_get_translator(lang, **kwargs))
|