[PATCH 4 of 4] i18n: make age() function translatable using plural forms

Andrei Polushin polushin at gmail.com
Mon Dec 19 17:02:37 CST 2011


# HG changeset patch
# User Andrei Polushin <polushin at gmail.com>
# Date 1324251074 -25200
# Node ID 7a8ff92e57e6730791d4c4c1a58846040e3758d0
# Parent  dcce87b206cba3eff318fb9c03b313277fb49fde
i18n: make age() function translatable using plural forms

diff -r dcce87b206cb -r 7a8ff92e57e6 mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Sun Dec 18 03:54:58 2011 +0700
+++ b/mercurial/templatefilters.py	Mon Dec 19 06:31:14 2011 +0700
@@ -5,6 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+from i18n import _, ngettext
 import cgi, re, os, time, urllib
 import encoding, node, util
 import hbisect
@@ -15,26 +16,21 @@
     """
     return text.replace('\n', '<br/>\n')
 
-agescales = [("year", 3600 * 24 * 365),
-             ("month", 3600 * 24 * 30),
-             ("week", 3600 * 24 * 7),
-             ("day", 3600 * 24),
-             ("hour", 3600),
-             ("minute", 60),
-             ("second", 1)]
+agescales = [
+    ((lambda n: ngettext("%d year", "%d years", n)), 3600 * 24 * 365),
+    ((lambda n: ngettext("%d month", "%d months", n)), 3600 * 24 * 30),
+    ((lambda n: ngettext("%d week", "%d weeks", n)), 3600 * 24 * 7),
+    ((lambda n: ngettext("%d day", "%d days", n)), 3600 * 24),
+    ((lambda n: ngettext("%d hour", "%d hours", n)), 3600),
+    ((lambda n: ngettext("%d minute", "%d minutes", n)), 60),
+    ((lambda n: ngettext("%d second", "%d seconds", n)), 1),
+    ]
 
 def age(date):
     """:age: Date. Returns a human-readable date/time difference between the
     given date/time and the current date/time.
     """
 
-    def plural(t, c):
-        if c == 1:
-            return t
-        return t + "s"
-    def fmt(t, c):
-        return "%d %s" % (c, plural(t, c))
-
     now = time.time()
     then = date[0]
     future = False
@@ -42,7 +38,7 @@
         future = True
         delta = max(1, int(then - now))
         if delta > agescales[0][1] * 30:
-            return 'in the distant future'
+            return _('in the distant future')
     else:
         delta = max(1, int(now - then))
         if delta > agescales[0][1] * 2:
@@ -51,9 +47,13 @@
     for t, s in agescales:
         n = delta // s
         if n >= 2 or s == 1:
+            formatted = t(n) % n
             if future:
-                return '%s from now' % fmt(t, n)
-            return '%s ago' % fmt(t, n)
+                # i18n: age in the future
+                return _('%s from now') % formatted
+            else:
+                # i18n: age in the past
+                return _('%s ago') % formatted
 
 def basename(path):
     """:basename: Any text. Treats the text as a path, and returns the last


More information about the Mercurial-devel mailing list