Example #1
0
def date_diff(timestamp, to=None):
    if not timestamp:
        return ""

    compare_with = to or datetime.date.today()
    delta = timestamp - compare_with

    if delta.days == 0:
        return u"today"
    elif delta.days == -1:
        return u"yesterday"
    elif delta.days == 1:
        return u"tomorrow"

    chunks = (
        (365.0, lambda n: ungettext('year', 'years', n)),
        (30.0, lambda n: ungettext('month', 'months', n)),
        (7.0, lambda n: ungettext('week', 'weeks', n)),
        (1.0, lambda n: ungettext('day', 'days', n)),
    )

    for i, (chunk, name) in enumerate(chunks):
        if abs(delta.days) >= chunk:
            count = abs(round(delta.days / chunk, 0))
            break

    date_str = (ugettext('%(number)d %(type)s') % {
        'number': count,
        'type': name(count)
    })

    if delta.days > 0:
        return "in " + date_str
    else:
        return date_str + " ago"
Example #2
0
def date_diff(timestamp, to=None):
    if not timestamp:
        return ""

    compare_with = to or datetime.date.today()
    delta = timestamp - compare_with

    if delta.days == 0:
        return u"today"
    elif delta.days == -1:
        return u"yesterday"
    elif delta.days == 1:
        return u"tomorrow"

    chunks = (
        (365.0, lambda n: ungettext('year', 'years', n)),
        (30.0, lambda n: ungettext('month', 'months', n)),
        (7.0, lambda n: ungettext('week', 'weeks', n)),
        (1.0, lambda n: ungettext('day', 'days', n)),
    )

    for i, (chunk, name) in enumerate(chunks):
        if abs(delta.days) >= chunk:
            count = abs(round(delta.days / chunk, 0))
            break

    date_str = (ugettext('%(number)d %(type)s') %
                        {'number': count, 'type': name(count)})

    if delta.days > 0:
        return "in " + date_str
    else:
        return date_str + " ago"
Example #3
0
    def handle_builtin(self, license):
        import amo
        # License.builtin is off by one!
        data = amo.LICENSE_IDS[license.builtin - 1]
        license.url = data.url
        license.on_form = data.on_form
        if data.icons:
            license.icons = ' '.join(data.icons)
        license.some_rights = bool(data.linktext)

        if data.shortname:
            license.text = license_text(data.shortname)

        # Gather all the translated names.
        activate('en-us')
        license.name = en_name = unicode(data.name)
        names = {}
        for lang in settings.AMO_LANGUAGES:
            activate(lang)
            trans = ugettext(en_name)
            if trans and trans != en_name:
                names[lang] = trans
        activate('en-us')
        license.name = names
        license.save()
Example #4
0
def date_diff(timestamp, to=None):
    if not timestamp:
        return ""

    compare_with = to or datetime.date.today()
    delta = timestamp - compare_with

    if delta.days == 0:
        return u"today"
    elif delta.days == -1:
        return u"yesterday"
    elif delta.days == 1:
        return u"tomorrow"

    chunks = (
        (365.0, lambda n: ungettext("year", "years", n)),
        (30.0, lambda n: ungettext("month", "months", n)),
        (7.0, lambda n: ungettext("week", "weeks", n)),
        (1.0, lambda n: ungettext("day", "days", n)),
    )

    for i, (chunk, name) in enumerate(chunks):
        if abs(delta.days) >= chunk:
            count = abs(round(delta.days / chunk, 0))
            break

    date_str = ugettext("%(number)d %(type)s") % {"number": count, "type": name(count)}

    if delta.days > 0:
        return "in " + date_str
    else:
        return date_str + " ago"
Example #5
0
def ugettext_locale(message, locale):
    """Translate a message in a specific locale."""
    old_locale = get_language()
    tower.activate(locale)
    text = tower.ugettext(message)
    tower.activate(old_locale)

    return text