Example #1
0
 def dtgettext(self, domain, string, **kwargs):
     def _dtgettext():
         trans = self.active.dugettext(domain, string)
         return _tag_kwargs(trans, kwargs) if kwargs else trans
     if not self.isactive:
         return LazyProxy(_dtgettext)
     return _dtgettext()
Example #2
0
def gettext_lazy(message, domain=DEFAULT_DOMAIN):
    """Mark a message as translatable, but delay the translation until the message is used.

    Sometimes, there are some messages that need to be translated, but the translation
    can't be done at the point the message itself is written. For example, the names of
    the fields in a Model can't be translated at the point they are written, otherwise
    the translation would be done when the file is imported, long before a user even connects.
    To avoid this, `gettext_lazy` should be used. For example:


    .. code-block:: python

        from zengine.lib.translation import gettext_lazy, InstalledLocale
        from pyoko import model, fields
        class User(model.Model):
             name = fields.String(gettext_lazy('User Name'))
        print(User.name.title)
        'User Name'
        
        InstalledLocale.install_language('tr')
        print(User.name.title)
        'Kullanıcı Adı'

    Args:
        message (basestring, unicode): The input message.
        domain (basestring): The domain of the message. Defaults to 'messages', which
            is the domain where all application messages should be located.
    Returns:
        unicode: The translated message, with the translation itself being delayed until
            the text is actually used.

    """
    return LazyProxy(gettext, message, domain=domain, enable_cache=False)
Example #3
0
def ngettext(string, plural, num, **kwargs):
    """Translates the given string with current locale and passes the given
    keyword variables as a mapping to format the string.

    It does a plural-forms lookup of a given string depending on the `num` and
    uses `plural` instead of `string` if `num` represents plural in the current
    locale.

    The returned value is a lazy instance which will be translated when it is
    actually used.

    Example::

        ngettext('%(num)d Apple', '%(num)d Apples!', num=len(apples))

    :param string: the string to be translated, singular form
    :param plural: the plural form of the string
    :param num: value of num placeholder
    :param kwargs: mapping to the format place holders
    :returns: a lazy instance to delay actual translation, translation will be
              performed when result is actually used.
    """
    def lazy(s, p, n, **kw):
        kw.setdefault('num', n)
        return get_translations().ungettext(s, p, n) % kw
    return LazyProxy(lazy, string, plural, num, **kwargs)
Example #4
0
 def tgettext(self, string, **kwargs):
     def _tgettext():
         trans = self.active.ugettext(string)
         return _tag_kwargs(trans, kwargs) if kwargs else trans
     if not self.isactive:
         return LazyProxy(_tgettext)
     return _tgettext()
Example #5
0
        def gettext(self, string, **kwargs):
            def _gettext():
                return safefmt(self.active.ugettext(string), kwargs)

            if not self.isactive:
                return LazyProxy(_gettext)
            return _gettext()
Example #6
0
        def dgettext(self, domain, string, **kwargs):
            def _dgettext():
                return safefmt(self.active.dugettext(domain, string), kwargs)

            if not self.isactive:
                return LazyProxy(_dgettext)
            return _dgettext()
Example #7
0
 def tngettext(self, singular, plural, num, **kwargs):
     kwargs = kwargs.copy()
     kwargs.setdefault('num', num)
     def _tngettext():
         trans = self.active.ungettext(singular, plural, num)
         return _tag_kwargs(trans, kwargs)
     if not self.isactive:
         return LazyProxy(_tngettext)
     return _tngettext()
Example #8
0
 def dngettext(self, domain, singular, plural, num, **kwargs):
     kwargs = kwargs.copy()
     kwargs.setdefault('num', num)
     def _dngettext():
         trans = self.active.dungettext(domain, singular, plural, num)
         return safefmt(trans, kwargs)
     if not self.isactive:
         return LazyProxy(_dngettext)
     return _dngettext()
Example #9
0
def ungettext_lazy(singular, plural, num):
    """Like ungettext, but lazy.

    :returns: A proxy for the translation object.
    :rtype: LazyProxy
    """
    def get_translation():
        return cherrypy.response.i18n.trans.ungettext(singular, plural, num)
    return LazyProxy(get_translation)
Example #10
0
def ugettext_lazy(message):
    """Like ugettext, but lazy.

    :returns: A proxy for the translation object.
    :rtype: LazyProxy
    """
    def get_translation():
        return cherrypy.response.i18n.trans.ugettext(message)
    return LazyProxy(get_translation)
Example #11
0
 def dtngettext(self, domain, singular, plural, num, **kwargs):
     kwargs = kwargs.copy()
     def _dtngettext():
         trans = self.active.dungettext(domain, singular, plural, num)
         if '%(num)' in trans:
             kwargs.update(num=num)
         return _tag_kwargs(trans, kwargs) if kwargs else trans
     if not self.isactive:
         return LazyProxy(_dtngettext)
     return _dtngettext()
Example #12
0
 def lazy_ngettext(self, singular: str, plural: str, lang: str = None, n=1):
     if not babel_imported:
         raise RuntimeError(
             'babel module is not imported. Check that you installed it.')
     return LazyProxy(self.ngettext,
                      singular,
                      plural,
                      lang,
                      n,
                      enable_cache=False)
Example #13
0
    def lazy_gettext(self, singular, plural=None, n=1, locale=None, enable_cache=False) -> LazyProxy:
        """
        Lazy get text

        :param singular:
        :param plural:
        :param n:
        :param locale:
        :param enable_cache:
        :return:
        """
        return LazyProxy(self.gettext, singular, plural, n, locale, enable_cache=enable_cache)
Example #14
0
 def gettext_lazy(self,
                  singular,
                  plural=None,
                  n=1,
                  locale=None,
                  enable_cache=True):
     return LazyProxy(self.gettext,
                      singular,
                      plural,
                      n,
                      locale,
                      enable_cache=enable_cache)
Example #15
0
def ngettext_lazy(singular, plural, n, domain=DEFAULT_DOMAIN):
    """Mark a message with plural forms translateable, and delay the translation
    until the message is used.

    Works the same was a `ngettext`, with a delaying functionality similiar to `gettext_lazy`.

    Args:
        singular (unicode): The singular form of the message.
        plural (unicode): The plural form of the message.
        n (int): The number that is used to decide which form should be used.
        domain (basestring): The domain of the message. Defaults to 'messages', which
                             is the domain where all application messages should be located.
    Returns:
        unicode: The correct pluralization, with the translation being
                 delayed until the message is used.
    """
    return LazyProxy(ngettext, singular, plural, n, domain=domain, enable_cache=False)
Example #16
0
def gettext(string, **kwargs):
    """Translates the given string with current locale and passes the given
    keyword variables as a mapping to format the string. The returned value
    is a lazy instance which will be translated when it is actually used.

    Example::

        h1 = gettext('Hello World!')
        h2 = gettext('Hello %(name)s!', name='World')

        @web.route('/say')
        def say():
            return h1

    :param string: the string to be translated
    :param kwargs: mapping to the format place holders
    :returns: a lazy instance to delay actual translation, translation will be
              performed when result is actually used.
    """
    def lazy(s, **kw):
        return get_translations().ugettext(s) % kw
    return LazyProxy(lazy, string, **kwargs)
Example #17
0
 def lazy_gettext(self, *args):
     return LazyProxy(self.gettext, *args, enable_cache=False)
Example #18
0
from spotipo_plugins import get_enabled_plugins

import os
import subprocess
import babel
from unifispot.ext.plugins import plugin_manager


def ugettext(s):
    # we assume a before_request function
    # assigns the correct user-specific
    # translations
    return g.translations.ugettext(s)


ugettext_lazy = LazyProxy(ugettext)

_ = gettext
_l = lazy_gettext
_n = ngettext


def format_datetime(dtime):
    with current_app.app_context():
        return _format_datetime(dtime)


##based on https://github.com/sh4nks/flaskbb/blob/master/flaskbb/utils/translations.py
class SpotipoDomain(Domain):
    def __init__(self, app):
        self.app = app
Example #19
0
 def make_lazy_string(__func, msg):
     """Creates a lazy string by invoking func with args."""
     return LazyProxy(__func, msg, enable_cache=False)
Example #20
0
 def lazy_gettext(self, text: str, lang: str = None):
     if not babel_imported:
         raise RuntimeError(
             'babel module is not imported. Check that you installed it.')
     return LazyProxy(self.gettext, text, lang, enable_cache=False)
Example #21
0
def lazy_ngettext(singular, plural, n):
    """A lazy version of :func:`ngettext`."""
    return LazyProxy(ngettext, singular, plural, n)
Example #22
0
def lazy_gettext(string):
    """A lazy version of :func:`gettext`."""
    return LazyProxy(gettext, string)
Example #23
0
    def validate(self, *args, **kwargs):
        r = super(CoveredArea, self).validate(*args, **kwargs)
        if bool(self.name.data) != bool(self.technologies.data):
            self._fields['name'].errors += [_(u'You must fill both fields')]
            r = False
        return r


class OtherWebsites(InsecureForm):
    name = TextField(_(u'name'), widget=partial(TextInput(), class_='input-small', placeholder=_(u'Name')))
    url = TextField(_(u'url'), widget=partial(TextInput(), class_='input-medium', placeholder=_(u'URL')),
                    validators=[Optional(), URL(require_tld=True)])


STEP_CHOICES = [(k, LazyProxy(lambda k, s: u'%u - %s' % (k, s), k, STEPS[k], enable_cache=False)) for k in STEPS]


class ProjectForm(Form):
    name = TextField(_(u'full name'), description=[_(u'E.g. French Data Network')],
                     validators=[DataRequired(), Length(min=2), Unique(ISP, ISP.name)])
    shortname = TextField(_(u'short name'), description=[_(u'E.g. FDN')],
                          validators=[Optional(), Length(min=2, max=15), Unique(ISP, ISP.shortname)])
    description = TextField(_(u'description'), description=[None, _(u'Short text describing the project')])
    logo_url = TextField(_(u'logo url'), validators=[Optional(), URL(require_tld=True)])
    website = TextField(_(u'website'), validators=[Optional(), URL(require_tld=True)])
    other_websites = FieldList(MyFormField(OtherWebsites, widget=partial(InputListWidget(), class_='formfield')),
                               min_entries=1, widget=InputListWidget(),
                               description=[None, _(u'Additional websites that you host (e.g. wiki, etherpad...)')])
    contact_email = TextField(_(u'contact email'), validators=[Optional(), Email()],
                              description=[None, _(u'General contact email address')])
Example #24
0
def gettext_lazy(string: str) -> LazyProxy:
    return LazyProxy(gettext, string, enable_cache=False)
Example #25
0
def lazy_gettext(s):
    return LazyProxy(_, s, enable_cache=False)