def timesince(dt, format='EEEE, d MMMM yyyy HH:mm UTC', default=None): now = datetime.utcnow() diff = now - dt periods = ( ( diff.days/3, format_datetime(dt, format) ), ( diff.days, ngettext(u"%(num)s day ago", u"%(num)s days ago", diff.days) ), ( diff.seconds/3600, ngettext(u"%(num)s hour ago", u"%(num)s hours ago", diff.seconds/3600) ), ( diff.seconds/60, ngettext(u"%(num)s minute ago", u"%(num)s minutes ago", diff.seconds/60) ), ( diff.seconds, ngettext(u"%(num)s second ago", u"%(num)s seconds ago", diff.seconds) ), ) for period, result in periods: if period: return result return default or _(u"just now")
def index(): brittany = _('Brittany') france = _('France') return render_template('index.html', some_text=_("I am a sausage."), best_part=_("%(part)s is the best part of %(country)s.", part=brittany, country=france), singular=ngettext('I bought a garlic glove this morning.', 'I bought %(num)d garlic gloves this morning.', 1), plural=ngettext('I bought a garlic glove this morning.', 'I bought %(num)d garlic gloves this morning.', 42))
def index(): brittany = _('Brittany') france = _('France') return render_template( 'index.html', some_text=_("I am a sausage."), best_part=_("%(part)s is the best part of %(country)s.", part=brittany, country=france), singular=ngettext('I bought a garlic glove this morning.', 'I bought %(num)d garlic gloves this morning.', 1), plural=ngettext('I bought a garlic glove this morning.', 'I bought %(num)d garlic gloves this morning.', 42))
def plurals(key=None, n=1): plurals = { # Number Formatting "T": ngettext("Trillion", "Trillions", n), "B": ngettext("Billion", "Billions", n), "M": ngettext("Million", "Millions", n), "k": ngettext("Thousand", "Thousands", n), } if key: return unicode(plurals[key]) if key in plurals else None return plurals
def timesince(dt, default=None): if default is None: default = gettext('just now') now = datetime.now() diff = now - dt periods = ( (diff.days / 365, 'year', 'years'), (diff.days / 30, 'month', 'months'), (diff.days / 7, 'week', 'weeks'), (diff.days, 'day', 'days'), (diff.seconds / 3600, 'hour', 'hours'), (diff.seconds / 60, 'minute', 'minutes'), (diff.seconds, 'second', 'seconds'), ) for period, singular, plural in periods: if not period: continue if period < 0: break singular = u'%%(num)d %s ago' % singular plural = u'%%(num)d %s ago' % plural return ngettext(singular, plural, num=period) return default
def __init__(self, *args, **kwargs): super(TrackingDelaySelectField, self).__init__(*args, **kwargs) self.coerce = int self.choices = [(0, l_('None'))] for x in range(1, 10) + range(10, 30, 5) + range(30, 61, 15): self.choices.append((x, ngettext(u'%(num)u minute', u'%(num)u minutes', x)))
def timesince(dt, default=None): """ Returns string representing "time since" e.g. 3 days ago, 5 hours ago etc. """ if default is None: default = gettext("just now") now = datetime.utcnow() diff = now - dt periods = ( (diff.days / 365, "year", "years"), (diff.days / 30, "month", "months"), (diff.days / 7, "week", "weeks"), (diff.days, "day", "days"), (diff.seconds / 3600, "hour", "hours"), (diff.seconds / 60, "minute", "minutes"), (diff.seconds, "second", "seconds"), ) for period, singular, plural in periods: if not period: continue singular = u"%%(num)d %s ago" % singular plural = u"%%(num)d %s ago" % plural return ngettext(singular, plural, num=period) return default
def pretty_date(dt, default=None): """ Returns string representing "time since" e.g. 3 days ago, 5 hours ago etc. NB: when/if Babel 1.0 released use format_timedelta/timedeltaformat instead """ if default is None: default = gettext("just now") now = datetime.utcnow() diff = now - dt years = diff.days / 365 months = diff.days / 30 weeks = diff.days / 7 days = diff.days hours = diff.seconds / 3600 minutes = diff.seconds / 60 seconds = diff.seconds periods = ( (years, ngettext("%(num)s year", "%(num)s years", num=years)), (months, ngettext("%(num)s month", "%(num)s months", num=months)), (weeks, ngettext("%(num)s week", "%(num)s weeks", num=weeks)), (days, ngettext("%(num)s day", "%(num)s days", num=days)), (hours, ngettext("%(num)s hour", "%(num)s hours", num=hours)), (minutes, ngettext("%(num)s minute", "%(num)s minutes", num=minutes)), (seconds, ngettext("%(num)s second", "%(num)s seconds", num=seconds)), ) for period, trans in periods: if period: return gettext("%(period)s ago", period=trans) return default
def prettify_time_ago(t): """ Converts seconds to a meaningful string. INPUT - t -- time in seconds """ if t < 60: s = int(t) return ngettext('%(num)d second', '%(num)d seconds', s) if t < 3600: m = int(t/60) return ngettext('%(num)d minute', '%(num)d minutes', m) if t < 3600*24: h = int(t/3600) return ngettext('%(num)d hour', '%(num)d hours', h) d = int(t/(3600*24)) return ngettext('%(num)d day', '%(num)d days', d)
def unfollow(): ids = request.form.getlist('course_id') if not ids: flash(gettext('No course selected'), 'warning') return redirect(url_for('main.courses')) deleted = 0 for c in Course.query.filter(Course.id.in_(ids)).all(): deleted += current_user.unfollow(c) if deleted > 0: flash(ngettext('%(num)s course deleted', '%(num)s courses deleted', deleted), 'danger') else: flash(gettext('No course deleted'), 'info') return redirect(url_for('main.courses'))
def follow(): ids = request.form.getlist('course_id') if not ids: flash(gettext('No course selected'), 'warning') return redirect(url_for('main.courses')) added = 0 for c in Course.query.filter(Course.id.in_(ids)).all(): added += current_user.follow(c) if added > 0: flash(ngettext('%(num)s new course added', '%(num)s new courses added', added), 'success') else: flash(gettext('No new course added'), 'warning') return redirect(url_for('main.courses'))
def index(): # Can supply additional arguments to the string: spinal_tap = _('Ours goes to %(limit)s', limit=11) vizzini_says = _('Inconceivable!') inigo_says = _('Hello! My name is Inigo Montoya') princess_bride = _('Vizzini: %(viz)s; Inigo: %(im)s', viz=vizzini_says, im=inigo_says) # Variant of 'gettext' for singular/plural blind_mice = ngettext('%(num)d blind mouse', '%(num)d blind mice', 3) return render_template('index.html', messages=[ _('Hello, World'), _('Josh has an attractive hat on today'), spinal_tap, princess_bride, blind_mice, _('Fred lives in Peru') ])
def timesince(self, dt=None): if dt is None: dt = Datetime2() diff = dt - self if diff.days // 365 > 0: return ngettext('%(num)s year ago', '%(num)s years ago', num=diff.days // 365) elif diff.days // 30 > 0: return ngettext('%(num)s month ago', '%(num)s months ago', num=diff.days // 30) elif diff.days // 7 > 0: return ngettext('%(num)s week ago', '%(num)s weeks ago', num=diff.days // 7) elif diff.days > 0: return ngettext('%(num)s day ago', '%(num)s days ago', num=diff.days) elif diff.seconds // 3600 > 0: return ngettext('%(num)s hour ago', '%(num)s hours ago', num=diff.seconds // 3600) elif diff.seconds // 60 > 0: return ngettext('%(num)s minute ago', '%(num)s minutes ago', num=diff.seconds // 60) else: return gettext('just now')
def subtitle(self): return ngettext('%(num)d room', '%(num)d rooms', num=len(self.rooms))
def subtitle(self): return ngettext('%(num)d POI', '%(num)d POIs', num=len(self.pois))
""" labs para https://pythonhosted.org/Flask-Babel/ """ from flask import Flask from flask.ext.babel import Babel from flask.ext.babel import gettext, ngettext app = Flask(__name__) app.config.from_pyfile('mysettings.cfg') #Babel(app=None, default_locale='en', default_timezone='UTC', date_formats=None, configure_jinja=True) babel = Babel(app, default_locale='pt_BR', default_timezone='BRST') gettext(u'A simple string') gettext(u'Value: %(value)s', value=42) ngettext(u'%(num)s Apple', u'%(num)s Apples', number_of_apples) #$ pybabel extract -F babel.cfg -o messages.pot #$ pybabel init -i messages.pot -d translations -l de #$ pybabel compile -d translations
def ngettext(self, singular, pluaral, n): return ngettext(singular, pluaral, n)
def _n(*args, **kwargs): return ngettext(*args, **kwargs)
def translator(): return '<br>'.join( (gettext(u'A simple string'), gettext(u'Value: %(value)s', value=42), ngettext(u'%(num)s Apple', u'%(num)s Apples', 59)))