From: Rafał Hirsz Date: Sat, 12 Nov 2011 13:47:22 +0000 (+0100) Subject: Added support for functions in addition to strings in the relativeTime object. X-Git-Tag: 1.1.2~21^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5bed4083f2a34f74c8a3f1ecd1d3873a878f98f;p=thirdparty%2Fmoment.git Added support for functions in addition to strings in the relativeTime object. 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). --- diff --git a/lang/pl.js b/lang/pl.js index 5c2b7e7f7..da48cbee6 100644 --- a/lang/pl.js +++ b/lang/pl.js @@ -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') { diff --git a/lang/test/pl.js b/lang/test/pl.js index a5dd5955e..35beec354 100644 --- a/lang/test/pl.js +++ b/lang/test/pl.js @@ -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ęcy", "46 days = 2 months"); - equal(start.from(moment([2007, 1, 28]).add({d:74}), true), "2 miesięcy", "75 days = 2 months"); - equal(start.from(moment([2007, 1, 28]).add({d:76}), true), "3 miesięcy", "76 days = 3 months"); + equal(start.from(moment([2007, 1, 28]).add({d:46}), true), "2 miesiące", "46 days = 2 months"); + equal(start.from(moment([2007, 1, 28]).add({d:74}), true), "2 miesiące", "75 days = 2 months"); + equal(start.from(moment([2007, 1, 28]).add({d:76}), true), "3 miesiące", "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"); }); diff --git a/moment.js b/moment.js index 0a74fdc87..e1bec5689 100644 --- a/moment.js +++ b/moment.js @@ -384,27 +384,28 @@ }); // 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 @@ -457,7 +458,7 @@ 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); }, diff --git a/sitesrc/docs.jade b/sitesrc/docs.jade index ce050e254..7fb0165a4 100644 --- a/sitesrc/docs.jade +++ b/sitesrc/docs.jade @@ -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",