]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added support for functions in addition to strings in the relativeTime object.
authorRafał Hirsz <hirszdesign@gmail.com>
Sat, 12 Nov 2011 13:47:22 +0000 (14:47 +0100)
committerRafał Hirsz <hirszdesign@gmail.com>
Sat, 12 Nov 2011 13:47:22 +0000 (14:47 +0100)
The functions should take a single number as an argument and return the correct string for it.
This should provide better support for languages with more complex declension and plural forms (like Polish).

lang/pl.js
lang/test/pl.js
moment.js
sitesrc/docs.jade

index 5c2b7e7f7b8172ecee5a0ea2189bb7c845322ecb..da48cbee6dcdd152b1f02e0cfafbe3c0378f722f 100644 (file)
@@ -1,34 +1,51 @@
 (function () {
-    var lang = {
-            months : "styczeń_luty_marzec_kwiecień_maj_czerwiec_lipiec_sierpień_wrzesień_październik_listopad_grudzień".split("_"),
-            monthsShort : "sty_lut_mar_kwi_maj_cze_lip_sie_wrz_paź_lis_gru".split("_"),
-            weekdays : "niedziela_poniedziałek_wtorek_środa_czwartek_piątek_sobota".split("_"),
-            weekdaysShort : "nie_pon_wt_śr_czw_pt_sb".split("_"),
-            longDateFormat : {
-                L : "DD-MM-YYYY",
-                LL : "D MMMM YYYY",
-                LLL : "D MMMM YYYY HH:mm",
-                LLLL : "dddd, D MMMM YYYY HH:mm"
-            },
-            relativeTime : {
-                future : "za %s",
-                past : "%s temu",
-                s : "kilka sekund",
-                m : "minutę",
-                mm : "%d minut",
-                h : "godzinę",
-                hh : "%d godzin",
-                d : "1 dzień",
-                dd : "%d dni",
-                M : "miesiąc",
-                MM : "%d miesięcy",
-                y : "rok",
-                yy : "%d lat"
-            },
-            ordinal : function (number) {
-                return '.';
-            }
+    var singular = function (ws, a, b) {
+        return ws ? a : b;
+    },
+    plural = function (n, a, b) { 
+        return ((n % 10 < 5) && (n % 10 > 1) && (~~(n / 10) !== 1)) ? n + ' ' + a : n + ' ' + b;
+    },
+    singularf = function (a, b) {
+        return function (n, ws) {
+            return singular(ws, a, b);
         };
+    },
+    pluralf = function (a, b) {
+        return function (n) {
+            return plural(n, a, b);
+        };
+    },
+  
+    lang = {
+        months : "styczeń_luty_marzec_kwiecień_maj_czerwiec_lipiec_sierpień_wrzesień_październik_listopad_grudzień".split("_"),
+        monthsShort : "sty_lut_mar_kwi_maj_cze_lip_sie_wrz_paź_lis_gru".split("_"),
+        weekdays : "niedziela_poniedziałek_wtorek_środa_czwartek_piątek_sobota".split("_"),
+        weekdaysShort : "nie_pon_wt_śr_czw_pt_sb".split("_"),
+        longDateFormat : {
+            L : "DD-MM-YYYY",
+            LL : "D MMMM YYYY",
+            LLL : "D MMMM YYYY HH:mm",
+            LLLL : "dddd, D MMMM YYYY HH:mm"
+        },
+        relativeTime : {
+            future : "za %s",
+            past : "%s temu",
+            s : "kilka sekund",
+            m : singularf('minuta', 'minutę'),
+            mm : pluralf('minuty', 'minut'),
+            h : singularf('godzina', 'godzinę'),
+            hh : pluralf('godziny', 'godzin'),
+            d : "1 dzień",
+            dd : '%d dni',
+            M : "miesiąc",
+            MM : pluralf('miesiące', 'miesięcy'),
+            y : "rok",
+            yy : pluralf('lata', 'lat')
+        },
+        ordinal : function (number) {
+            return '.';
+        }
+    };
 
     // Node
     if (typeof module !== 'undefined') {
index a5dd5955eb1d1cbbf8c0da413f466f834b745009..35beec3549dd85ee225837b3e848982541822c7b 100644 (file)
@@ -105,13 +105,13 @@ test("from", 30, function() {
     moment.lang('pl');
     var start = moment([2007, 1, 28]);
     equal(start.from(moment([2007, 1, 28]).add({s:44}), true),  "kilka sekund",  "44 seconds = a few seconds");
-    equal(start.from(moment([2007, 1, 28]).add({s:45}), true),  "minutę",        "45 seconds = a minute");
-    equal(start.from(moment([2007, 1, 28]).add({s:89}), true),  "minutę",        "89 seconds = a minute");
-    equal(start.from(moment([2007, 1, 28]).add({s:90}), true),  "2 minut",       "90 seconds = 2 minutes");
-    equal(start.from(moment([2007, 1, 28]).add({m:44}), true),  "44 minut",      "44 minutes = 44 minutes");
-    equal(start.from(moment([2007, 1, 28]).add({m:45}), true),  "godzinę",       "45 minutes = an hour");
-    equal(start.from(moment([2007, 1, 28]).add({m:89}), true),  "godzinę",       "89 minutes = an hour");
-    equal(start.from(moment([2007, 1, 28]).add({m:90}), true),  "2 godzin",      "90 minutes = 2 hours");
+    equal(start.from(moment([2007, 1, 28]).add({s:45}), true),  "minuta",        "45 seconds = a minute");
+    equal(start.from(moment([2007, 1, 28]).add({s:89}), true),  "minuta",        "89 seconds = a minute");
+    equal(start.from(moment([2007, 1, 28]).add({s:90}), true),  "2 minuty",      "90 seconds = 2 minutes");
+    equal(start.from(moment([2007, 1, 28]).add({m:44}), true),  "44 minuty",     "44 minutes = 44 minutes");
+    equal(start.from(moment([2007, 1, 28]).add({m:45}), true),  "godzina",       "45 minutes = an hour");
+    equal(start.from(moment([2007, 1, 28]).add({m:89}), true),  "godzina",       "89 minutes = an hour");
+    equal(start.from(moment([2007, 1, 28]).add({m:90}), true),  "2 godziny",     "90 minutes = 2 hours");
     equal(start.from(moment([2007, 1, 28]).add({h:5}), true),   "5 godzin",      "5 hours = 5 hours");
     equal(start.from(moment([2007, 1, 28]).add({h:21}), true),  "21 godzin",     "21 hours = 21 hours");
     equal(start.from(moment([2007, 1, 28]).add({h:22}), true),  "1 dzień",       "22 hours = a day");
@@ -123,15 +123,15 @@ test("from", 30, function() {
     equal(start.from(moment([2007, 1, 28]).add({d:26}), true),  "miesiąc",       "26 days = a month");
     equal(start.from(moment([2007, 1, 28]).add({d:30}), true),  "miesiąc",       "30 days = a month");
     equal(start.from(moment([2007, 1, 28]).add({d:45}), true),  "miesiąc",       "45 days = a month");
-    equal(start.from(moment([2007, 1, 28]).add({d:46}), true),  "2 miesiÄ\99cy",    "46 days = 2 months");
-    equal(start.from(moment([2007, 1, 28]).add({d:74}), true),  "2 miesiÄ\99cy",    "75 days = 2 months");
-    equal(start.from(moment([2007, 1, 28]).add({d:76}), true),  "3 miesiÄ\99cy",    "76 days = 3 months");
+    equal(start.from(moment([2007, 1, 28]).add({d:46}), true),  "2 miesiÄ\85ce",    "46 days = 2 months");
+    equal(start.from(moment([2007, 1, 28]).add({d:74}), true),  "2 miesiÄ\85ce",    "75 days = 2 months");
+    equal(start.from(moment([2007, 1, 28]).add({d:76}), true),  "3 miesiÄ\85ce",    "76 days = 3 months");
     equal(start.from(moment([2007, 1, 28]).add({M:1}), true),   "miesiąc",       "1 month = a month");
     equal(start.from(moment([2007, 1, 28]).add({M:5}), true),   "5 miesięcy",    "5 months = 5 months");
     equal(start.from(moment([2007, 1, 28]).add({d:344}), true), "11 miesięcy",   "344 days = 11 months");
     equal(start.from(moment([2007, 1, 28]).add({d:345}), true), "rok",           "345 days = a year");
     equal(start.from(moment([2007, 1, 28]).add({d:547}), true), "rok",           "547 days = a year");
-    equal(start.from(moment([2007, 1, 28]).add({d:548}), true), "2 lat",         "548 days = 2 years");
+    equal(start.from(moment([2007, 1, 28]).add({d:548}), true), "2 lata",        "548 days = 2 years");
     equal(start.from(moment([2007, 1, 28]).add({y:1}), true),   "rok",           "1 year = a year");
     equal(start.from(moment([2007, 1, 28]).add({y:5}), true),   "5 lat",         "5 years = 5 years");
 });
@@ -149,9 +149,10 @@ test("now from now", 1, function() {
 });
 
 
-test("fromNow", 2, function() {
+test("fromNow", 3, function() {
     moment.lang('pl');
     equal(moment().add({s:30}).fromNow(), "za kilka sekund", "in a few seconds");
+    equal(moment().add({h:1}).fromNow(), "za godzinę", "in an hour");
     equal(moment().add({d:5}).fromNow(), "za 5 dni", "in 5 days");
 });
 
index 0a74fdc872cd3f08081bc419d6e643ac6bbaae7c..e1bec5689399c09592cfefaf343122a0da858aed 100644 (file)
--- a/moment.js
+++ b/moment.js
     });
 
     // helper function for _date.from() and _date.fromNow()
-    function substituteTimeAgo(string, number) {
-        return moment.relativeTime[string].replace(/%d/i, number || 1);
+    function substituteTimeAgo(string, number, withoutSuffix) {
+        var rt = moment.relativeTime[string];
+        return (typeof rt === 'function') ? rt(number || 1, !!withoutSuffix) : rt.replace(/%d/i, number || 1);
     }
 
-    function relativeTime(milliseconds) {
+    function relativeTime(milliseconds, withoutSuffix) {
         var seconds = Math.abs(milliseconds) / 1000,
             minutes = seconds / 60,
             hours = minutes / 60,
             days = hours / 24,
             years = days / 365;
-        return seconds < 45 && substituteTimeAgo('s', round(seconds)) ||
-            round(minutes) === 1 && substituteTimeAgo('m') ||
-            minutes < 45 && substituteTimeAgo('mm', round(minutes)) ||
-            round(hours) === 1 && substituteTimeAgo('h') ||
-            hours < 22 && substituteTimeAgo('hh', round(hours)) ||
-            round(days) === 1 && substituteTimeAgo('d') ||
-            days <= 25 && substituteTimeAgo('dd', round(days)) ||
-            days <= 45 && substituteTimeAgo('M') ||
-            days < 345 && substituteTimeAgo('MM', round(days / 30)) ||
-            round(years) === 1 && substituteTimeAgo('y') ||
-            substituteTimeAgo('yy', round(years));
+        return seconds < 45 && substituteTimeAgo('s', round(seconds), withoutSuffix) ||
+            round(minutes) === 1 && substituteTimeAgo('m', 1, withoutSuffix) ||
+            minutes < 45 && substituteTimeAgo('mm', round(minutes), withoutSuffix) ||
+            round(hours) === 1 && substituteTimeAgo('h', 1, withoutSuffix) ||
+            hours < 22 && substituteTimeAgo('hh', round(hours), withoutSuffix) ||
+            round(days) === 1 && substituteTimeAgo('d', 1, withoutSuffix) ||
+            days <= 25 && substituteTimeAgo('dd', round(days), withoutSuffix) ||
+            days <= 45 && substituteTimeAgo('M', 1, withoutSuffix) ||
+            days < 345 && substituteTimeAgo('MM', round(days / 30), withoutSuffix) ||
+            round(years) === 1 && substituteTimeAgo('y', 1, withoutSuffix) ||
+            substituteTimeAgo('yy', round(years), withoutSuffix);
     }
 
     // shortcut for prototype
         from : function (time, withoutSuffix) {
             var difference = this.diff(time),
                 rel = moment.relativeTime,
-                output = relativeTime(difference);
+                output = relativeTime(difference, withoutSuffix);
             return withoutSuffix ? output : (difference <= 0 ? rel.past : rel.future).replace(/%s/i, output);
         },
 
index ce050e25488305878815bce7b903c51a0a07c583..7fb0165a4d5f2cbbcaac536040f9985c8f6de256 100644 (file)
@@ -969,7 +969,7 @@ block content
         code moment.relativeTime
         | should be an object of the replacement strings for 
         code moment.fn.from
-        | .
+        | . The functions should return the correct string for a given number.
       pre moment.relativeTime = {\n
         |     future: "in %s",
         |     past: "%s ago",