##// END OF EJS Templates
utils: switched age function to use lazy translated pyramid translation mechanism.
marcink -
r1317:3fc8c7de default
parent child Browse files
Show More
@@ -44,6 +44,7 b' import webob'
44 import routes.util
44 import routes.util
45
45
46 import rhodecode
46 import rhodecode
47 from rhodecode.translation import _, _pluralize
47
48
48
49
49 def md5(s):
50 def md5(s):
@@ -380,7 +381,6 b' def age(prevdate, now=None, show_short_v'
380 :rtype: unicode
381 :rtype: unicode
381 :returns: unicode words describing age
382 :returns: unicode words describing age
382 """
383 """
383 from pylons.i18n.translation import _, ungettext
384
384
385 def _get_relative_delta(now, prevdate):
385 def _get_relative_delta(now, prevdate):
386 base = dateutil.relativedelta.relativedelta(now, prevdate)
386 base = dateutil.relativedelta.relativedelta(now, prevdate)
@@ -460,12 +460,12 b' def age(prevdate, now=None, show_short_v'
460 }
460 }
461 else:
461 else:
462 fmt_funcs = {
462 fmt_funcs = {
463 'year': lambda d: ungettext(u'%d year', '%d years', d) % d,
463 'year': lambda d: _pluralize(u'${num} year', u'${num} years', d, mapping={'num': d}).interpolate(),
464 'month': lambda d: ungettext(u'%d month', '%d months', d) % d,
464 'month': lambda d: _pluralize(u'${num} month', u'${num} months', d, mapping={'num': d}).interpolate(),
465 'day': lambda d: ungettext(u'%d day', '%d days', d) % d,
465 'day': lambda d: _pluralize(u'${num} day', u'${num} days', d, mapping={'num': d}).interpolate(),
466 'hour': lambda d: ungettext(u'%d hour', '%d hours', d) % d,
466 'hour': lambda d: _pluralize(u'${num} hour', u'${num} hours', d, mapping={'num': d}).interpolate(),
467 'minute': lambda d: ungettext(u'%d minute', '%d minutes', d) % d,
467 'minute': lambda d: _pluralize(u'${num} minute', u'${num} minutes', d, mapping={'num': d}).interpolate(),
468 'second': lambda d: ungettext(u'%d second', '%d seconds', d) % d,
468 'second': lambda d: _pluralize(u'${num} second', u'${num} seconds', d, mapping={'num': d}).interpolate(),
469 }
469 }
470
470
471 i = 0
471 i = 0
@@ -483,33 +483,34 b' def age(prevdate, now=None, show_short_v'
483 _val = fmt_funcs[part](value)
483 _val = fmt_funcs[part](value)
484 if future:
484 if future:
485 if show_suffix:
485 if show_suffix:
486 return _(u'in %s') % _val
486 return _(u'in ${ago}', mapping={'ago': _val})
487 else:
487 else:
488 return _val
488 return _(_val)
489
489
490 else:
490 else:
491 if show_suffix:
491 if show_suffix:
492 return _(u'%s ago') % _val
492 return _(u'${ago} ago', mapping={'ago': _val})
493 else:
493 else:
494 return _val
494 return _(_val)
495
495
496 val = fmt_funcs[part](value)
496 val = fmt_funcs[part](value)
497 val_detail = fmt_funcs[sub_part](sub_value)
497 val_detail = fmt_funcs[sub_part](sub_value)
498 mapping = {'val': val, 'detail': val_detail}
498
499
499 if short_format:
500 if short_format:
500 datetime_tmpl = u'%s, %s'
501 datetime_tmpl = _(u'${val}, ${detail}', mapping=mapping)
501 if show_suffix:
502 if show_suffix:
502 datetime_tmpl = _(u'%s, %s ago')
503 datetime_tmpl = _(u'${val}, ${detail} ago', mapping=mapping)
503 if future:
504 if future:
504 datetime_tmpl = _(u'in %s, %s')
505 datetime_tmpl = _(u'in ${val}, ${detail}', mapping=mapping)
505 else:
506 else:
506 datetime_tmpl = _(u'%s and %s')
507 datetime_tmpl = _(u'${val} and ${detail}', mapping=mapping)
507 if show_suffix:
508 if show_suffix:
508 datetime_tmpl = _(u'%s and %s ago')
509 datetime_tmpl = _(u'${val} and ${detail} ago', mapping=mapping)
509 if future:
510 if future:
510 datetime_tmpl = _(u'in %s and %s')
511 datetime_tmpl = _(u'in ${val} and ${detail}', mapping=mapping)
511
512
512 return datetime_tmpl % (val, val_detail)
513 return datetime_tmpl
513 i += 1
514 i += 1
514 return _(u'just now')
515 return _(u'just now')
515
516
@@ -145,7 +145,12 b' def test_age(age_args, expected, kw, pyl'
145 from dateutil import relativedelta
145 from dateutil import relativedelta
146 n = datetime.datetime(year=2012, month=5, day=17)
146 n = datetime.datetime(year=2012, month=5, day=17)
147 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
147 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
148 assert age(n + delt(**age_args), now=n, **kw) == expected
148
149 def translate(elem):
150 return elem.interpolate()
151
152 assert translate(age(n + delt(**age_args), now=n, **kw)) == expected
153
149
154
150 @pytest.mark.parametrize("age_args, expected, kw", [
155 @pytest.mark.parametrize("age_args, expected, kw", [
151 ({}, u'just now', {}),
156 ({}, u'just now', {}),
@@ -172,7 +177,11 b' def test_age_in_future(age_args, expecte'
172 from dateutil import relativedelta
177 from dateutil import relativedelta
173 n = datetime.datetime(year=2012, month=5, day=17)
178 n = datetime.datetime(year=2012, month=5, day=17)
174 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
179 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
175 assert age(n + delt(**age_args), now=n, **kw) == expected
180
181 def translate(elem):
182 return elem.interpolate()
183
184 assert translate(age(n + delt(**age_args), now=n, **kw)) == expected
176
185
177
186
178 def test_tag_exctrator():
187 def test_tag_exctrator():
@@ -34,3 +34,10 b' class LazyString(object):'
34 def lazy_ugettext(*args, **kw):
34 def lazy_ugettext(*args, **kw):
35 """ Lazily evaluated version of _() """
35 """ Lazily evaluated version of _() """
36 return LazyString(*args, **kw)
36 return LazyString(*args, **kw)
37
38
39 def _pluralize(msgid1, msgid2, n, mapping=None):
40 if n == 1:
41 return _(msgid1, mapping=mapping)
42 else:
43 return _(msgid2, mapping=mapping)
General Comments 0
You need to be logged in to leave comments. Login now