From d053979d83500ba7b063e3a9fcc9527bb8008cd4 Mon Sep 17 00:00:00 2001 From: Marcin Kuzminski Date: Fri, 13 Jan 2017 12:25:44 +0100 Subject: [PATCH] utils: switched age function to use lazy translated pyramid translation mechanism. --- rhodecode/lib/utils2.py | 37 +++++++++++++++--------------- rhodecode/tests/other/test_libs.py | 13 +++++++++-- rhodecode/translation.py | 7 ++++++ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/rhodecode/lib/utils2.py b/rhodecode/lib/utils2.py index 0882e5da..0c63f3e6 100644 --- a/rhodecode/lib/utils2.py +++ b/rhodecode/lib/utils2.py @@ -44,6 +44,7 @@ import webob import routes.util import rhodecode +from rhodecode.translation import _, _pluralize def md5(s): @@ -380,7 +381,6 @@ def age(prevdate, now=None, show_short_version=False, show_suffix=True, :rtype: unicode :returns: unicode words describing age """ - from pylons.i18n.translation import _, ungettext def _get_relative_delta(now, prevdate): base = dateutil.relativedelta.relativedelta(now, prevdate) @@ -460,12 +460,12 @@ def age(prevdate, now=None, show_short_version=False, show_suffix=True, } else: fmt_funcs = { - 'year': lambda d: ungettext(u'%d year', '%d years', d) % d, - 'month': lambda d: ungettext(u'%d month', '%d months', d) % d, - 'day': lambda d: ungettext(u'%d day', '%d days', d) % d, - 'hour': lambda d: ungettext(u'%d hour', '%d hours', d) % d, - 'minute': lambda d: ungettext(u'%d minute', '%d minutes', d) % d, - 'second': lambda d: ungettext(u'%d second', '%d seconds', d) % d, + 'year': lambda d: _pluralize(u'${num} year', u'${num} years', d, mapping={'num': d}).interpolate(), + 'month': lambda d: _pluralize(u'${num} month', u'${num} months', d, mapping={'num': d}).interpolate(), + 'day': lambda d: _pluralize(u'${num} day', u'${num} days', d, mapping={'num': d}).interpolate(), + 'hour': lambda d: _pluralize(u'${num} hour', u'${num} hours', d, mapping={'num': d}).interpolate(), + 'minute': lambda d: _pluralize(u'${num} minute', u'${num} minutes', d, mapping={'num': d}).interpolate(), + 'second': lambda d: _pluralize(u'${num} second', u'${num} seconds', d, mapping={'num': d}).interpolate(), } i = 0 @@ -483,33 +483,34 @@ def age(prevdate, now=None, show_short_version=False, show_suffix=True, _val = fmt_funcs[part](value) if future: if show_suffix: - return _(u'in %s') % _val + return _(u'in ${ago}', mapping={'ago': _val}) else: - return _val + return _(_val) else: if show_suffix: - return _(u'%s ago') % _val + return _(u'${ago} ago', mapping={'ago': _val}) else: - return _val + return _(_val) val = fmt_funcs[part](value) val_detail = fmt_funcs[sub_part](sub_value) + mapping = {'val': val, 'detail': val_detail} if short_format: - datetime_tmpl = u'%s, %s' + datetime_tmpl = _(u'${val}, ${detail}', mapping=mapping) if show_suffix: - datetime_tmpl = _(u'%s, %s ago') + datetime_tmpl = _(u'${val}, ${detail} ago', mapping=mapping) if future: - datetime_tmpl = _(u'in %s, %s') + datetime_tmpl = _(u'in ${val}, ${detail}', mapping=mapping) else: - datetime_tmpl = _(u'%s and %s') + datetime_tmpl = _(u'${val} and ${detail}', mapping=mapping) if show_suffix: - datetime_tmpl = _(u'%s and %s ago') + datetime_tmpl = _(u'${val} and ${detail} ago', mapping=mapping) if future: - datetime_tmpl = _(u'in %s and %s') + datetime_tmpl = _(u'in ${val} and ${detail}', mapping=mapping) - return datetime_tmpl % (val, val_detail) + return datetime_tmpl i += 1 return _(u'just now') diff --git a/rhodecode/tests/other/test_libs.py b/rhodecode/tests/other/test_libs.py index d0c28503..69cc9c16 100644 --- a/rhodecode/tests/other/test_libs.py +++ b/rhodecode/tests/other/test_libs.py @@ -145,7 +145,12 @@ def test_age(age_args, expected, kw, pylonsapp): from dateutil import relativedelta n = datetime.datetime(year=2012, month=5, day=17) delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) - assert age(n + delt(**age_args), now=n, **kw) == expected + + def translate(elem): + return elem.interpolate() + + assert translate(age(n + delt(**age_args), now=n, **kw)) == expected + @pytest.mark.parametrize("age_args, expected, kw", [ ({}, u'just now', {}), @@ -172,7 +177,11 @@ def test_age_in_future(age_args, expected, kw, pylonsapp): from dateutil import relativedelta n = datetime.datetime(year=2012, month=5, day=17) delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) - assert age(n + delt(**age_args), now=n, **kw) == expected + + def translate(elem): + return elem.interpolate() + + assert translate(age(n + delt(**age_args), now=n, **kw)) == expected def test_tag_exctrator(): diff --git a/rhodecode/translation.py b/rhodecode/translation.py index 007da662..b53c1702 100644 --- a/rhodecode/translation.py +++ b/rhodecode/translation.py @@ -34,3 +34,10 @@ class LazyString(object): def lazy_ugettext(*args, **kw): """ Lazily evaluated version of _() """ return LazyString(*args, **kw) + + +def _pluralize(msgid1, msgid2, n, mapping=None): + if n == 1: + return _(msgid1, mapping=mapping) + else: + return _(msgid2, mapping=mapping)