def test_ngettext_with_variables(self): self.assertEqual(i18n.ngettext('One foo %(foo)s', 'Many foos %(foo)s', 1), u'One foo %(foo)s') self.assertEqual(i18n.ngettext('One foo %(foo)s', 'Many foos %(foo)s', 2), u'Many foos %(foo)s') self.assertEqual(i18n.ngettext('One foo %(foo)s', 'Many foos %(foo)s', 1, foo='bar'), u'One foo bar') self.assertEqual(i18n.ngettext('One foo %(foo)s', 'Many foos %(foo)s', 2, foo='bar'), u'Many foos bar') self.assertEqual(i18n.ngettext('One foo %(foo)s', 'Many foos %(foo)s', 1) % {'foo': 'bar'}, u'One foo bar') self.assertEqual(i18n.ngettext('One foo %(foo)s', 'Many foos %(foo)s', 2) % {'foo': 'bar'}, u'Many foos bar')
def timesince(d, now=None): """ Takes two datetime objects and returns the time between d and now as a nicely formatted string, e.g. "10 minutes". If d occurs after now, then "0 minutes" is returned. Units used are years, months, weeks, days, hours, and minutes. Seconds and microseconds are ignored. Up to two adjacent units will be displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not. Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since """ chunks = ( (60 * 60 * 24 * 365, lambda n: i18n.ngettext('year', 'years', n)), (60 * 60 * 24 * 30, lambda n: i18n.ngettext('month', 'months', n)), (60 * 60 * 24 * 7, lambda n : i18n.ngettext('week', 'weeks', n)), (60 * 60 * 24, lambda n : i18n.ngettext('day', 'days', n)), (60 * 60, lambda n: i18n.ngettext('hour', 'hours', n)), (60, lambda n: i18n.ngettext('minute', 'minutes', n)) ) # Convert datetime.date to datetime.datetime for comparison. if not isinstance(d, datetime.datetime): d = datetime.datetime(d.year, d.month, d.day) if now and not isinstance(now, datetime.datetime): now = datetime.datetime(now.year, now.month, now.day) if not now: if d.tzinfo: now = datetime.datetime.now(LocalTimezone(d)) else: now = datetime.datetime.now() # ignore microsecond part of 'd' since we removed it from 'now' delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) since = delta.days * 24 * 60 * 60 + delta.seconds if since <= 0: # d is in the future compared to now, stop processing. return u'0 ' + 'minutes' for i, (seconds, name) in enumerate(chunks): count = since // seconds if count != 0: break s = '%(number)d %(type)s'% {'number': count, 'type': name(count)} return s
def timesince(d, now=None): """ Takes two datetime objects and returns the time between d and now as a nicely formatted string, e.g. "10 minutes". If d occurs after now, then "0 minutes" is returned. Units used are years, months, weeks, days, hours, and minutes. Seconds and microseconds are ignored. Up to two adjacent units will be displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not. Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since """ chunks = ((60 * 60 * 24 * 365, lambda n: i18n.ngettext('year', 'years', n)), (60 * 60 * 24 * 30, lambda n: i18n.ngettext('month', 'months', n)), (60 * 60 * 24 * 7, lambda n: i18n.ngettext('week', 'weeks', n)), (60 * 60 * 24, lambda n: i18n.ngettext('day', 'days', n)), (60 * 60, lambda n: i18n.ngettext('hour', 'hours', n)), (60, lambda n: i18n.ngettext('minute', 'minutes', n))) # Convert datetime.date to datetime.datetime for comparison. if not isinstance(d, datetime.datetime): d = datetime.datetime(d.year, d.month, d.day) if now and not isinstance(now, datetime.datetime): now = datetime.datetime(now.year, now.month, now.day) if not now: if d.tzinfo: now = datetime.datetime.now(LocalTimezone(d)) else: now = datetime.datetime.now() # ignore microsecond part of 'd' since we removed it from 'now' delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) since = delta.days * 24 * 60 * 60 + delta.seconds if since <= 0: # d is in the future compared to now, stop processing. return u'0 ' + 'minutes' for i, (seconds, name) in enumerate(chunks): count = since // seconds if count != 0: break s = '%(number)d %(type)s' % {'number': count, 'type': name(count)} return s
def test_ngettext(self): self.assertEqual(i18n.ngettext('One foo', 'Many foos', 1), u'One foo') self.assertEqual(i18n.ngettext('One foo', 'Many foos', 2), u'Many foos')