Skip to content

Commit 5fa20ce

Browse files
committed
more progress towards full migration to moment.js
1 parent 875151f commit 5fa20ce

54 files changed

Lines changed: 3484 additions & 33 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/assets/javascripts/discourse/components/formatter.js

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Discourse.Formatter = (function(){
2-
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge, relativeAgeTiny, relativeAgeMedium;
2+
3+
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge, relativeAgeTiny, relativeAgeMedium, relativeAgeMediumSpan;
4+
5+
var shortDateNoYear = Ember.String.i18n("dates.short_date_no_year");
6+
var longDate = Ember.String.i18n("dates.long_date");
7+
var shortDate = Ember.String.i18n("dates.short_date");
38

49
updateRelativeAge = function(elems) {
510
elems.each(function(){
@@ -19,7 +24,7 @@ Discourse.Formatter = (function(){
1924
relativeAgeTiny = function(date, options){
2025
var format = "tiny";
2126
var distance = Math.round((new Date() - date) / 1000);
22-
var distance_in_minutes = Math.round(distance / 60.0);
27+
var distanceInMinutes = Math.round(distance / 60.0);
2328

2429
var formatted;
2530
var t = function(key,opts){
@@ -28,29 +33,29 @@ Discourse.Formatter = (function(){
2833

2934
switch(true){
3035

31-
case(distance_in_minutes < 1):
36+
case(distanceInMinutes < 1):
3237
formatted = t("less_than_x_minutes", {count: 1});
3338
break;
34-
case(distance_in_minutes >= 1 && distance_in_minutes <= 44):
35-
formatted = t("x_minutes", {count: distance_in_minutes});
39+
case(distanceInMinutes >= 1 && distanceInMinutes <= 44):
40+
formatted = t("x_minutes", {count: distanceInMinutes});
3641
break;
37-
case(distance_in_minutes >= 45 && distance_in_minutes <= 89):
42+
case(distanceInMinutes >= 45 && distanceInMinutes <= 89):
3843
formatted = t("about_x_hours", {count: 1});
3944
break;
40-
case(distance_in_minutes >= 90 && distance_in_minutes <= 1439):
41-
formatted = t("about_x_hours", {count: Math.round(distance_in_minutes / 60.0)});
45+
case(distanceInMinutes >= 90 && distanceInMinutes <= 1439):
46+
formatted = t("about_x_hours", {count: Math.round(distanceInMinutes / 60.0)});
4247
break;
43-
case(distance_in_minutes >= 1440 && distance_in_minutes <= 2519):
48+
case(distanceInMinutes >= 1440 && distanceInMinutes <= 2519):
4449
formatted = t("x_days", {count: 1});
4550
break;
46-
case(distance_in_minutes >= 2520 && distance_in_minutes <= 129599):
47-
formatted = t("x_days", {count: Math.round(distance_in_minutes / 1440.0)});
51+
case(distanceInMinutes >= 2520 && distanceInMinutes <= 129599):
52+
formatted = t("x_days", {count: Math.round(distanceInMinutes / 1440.0)});
4853
break;
49-
case(distance_in_minutes >= 129600 && distance_in_minutes <= 525599):
50-
formatted = t("x_months", {count: Math.round(distance_in_minutes / 43200.0)});
54+
case(distanceInMinutes >= 129600 && distanceInMinutes <= 525599):
55+
formatted = t("x_months", {count: Math.round(distanceInMinutes / 43200.0)});
5156
break;
5257
default:
53-
var months = Math.round(distance_in_minutes / 43200.0);
58+
var months = Math.round(distanceInMinutes / 43200.0);
5459
if (months < 24) {
5560
formatted = t("x_months", {count: months});
5661
} else {
@@ -62,37 +67,61 @@ Discourse.Formatter = (function(){
6267
return formatted;
6368
};
6469

70+
relativeAgeMediumSpan = function(distance, leaveAgo) {
71+
var formatted, distanceInMinutes;
72+
73+
distanceInMinutes = Math.round(distance / 60.0);
74+
75+
var t = function(key, opts){
76+
return Ember.String.i18n("dates.medium" + (leaveAgo?"_with_ago":"") + "." + key, opts);
77+
}
78+
79+
switch(true){
80+
case(distanceInMinutes >= 1 && distanceInMinutes <= 56):
81+
formatted = t("x_minutes", {count: distanceInMinutes});
82+
break;
83+
case(distanceInMinutes >= 56 && distanceInMinutes <= 89):
84+
formatted = t("x_hours", {count: 1});
85+
break;
86+
case(distanceInMinutes >= 90 && distanceInMinutes <= 1379):
87+
formatted = t("x_hours", {count: Math.round(distanceInMinutes / 60.0)});
88+
break;
89+
case(distanceInMinutes >= 1380 && distanceInMinutes <= 2159):
90+
formatted = t("x_days", {count: 1});
91+
break;
92+
case(distanceInMinutes >= 2160):
93+
formatted = t("x_days", {count: Math.round((distanceInMinutes - 720.0) / 1440.0)});
94+
break;
95+
}
96+
97+
return formatted;
98+
};
99+
65100
relativeAgeMedium = function(date, options){
66-
var displayDate, fiveDaysAgo, oneMinuteAgo, fullReadable, humanized, leaveAgo, val;
101+
var displayDate, fiveDaysAgo, oneMinuteAgo, fullReadable, leaveAgo, val;
67102

68103
leaveAgo = options.leaveAgo;
104+
var distance = Math.round((new Date() - date) / 1000);
69105

70106
if (!date) {
71107
return "&mdash;";
72108
}
73109

74-
fullReadable = date.format("long");
110+
fullReadable = moment(date).format(longDate);
75111
displayDate = "";
76-
fiveDaysAgo = (new Date()) - 432000000;
77-
oneMinuteAgo = (new Date()) - 60000;
112+
fiveDaysAgo = 432000;
113+
oneMinuteAgo = 60;
78114

79-
if (oneMinuteAgo <= date.getTime() && date.getTime() <= (new Date())) {
115+
if (distance >= 0 && distance < oneMinuteAgo) {
80116
displayDate = Em.String.i18n("now");
81-
} else if (fiveDaysAgo > (date.getTime())) {
117+
} else if (distance > fiveDaysAgo) {
82118
if ((new Date()).getFullYear() !== date.getFullYear()) {
83-
displayDate = date.format("short");
119+
displayDate = moment(date).format(shortDate);
84120
} else {
85-
displayDate = date.format("short_no_year");
121+
displayDate = moment(date).format(shortDateNoYear);
86122
}
87123
} else {
88-
humanized = date.relative();
89-
if (!humanized) {
90-
return "";
91-
}
92-
displayDate = humanized;
93-
if (!leaveAgo) {
94-
displayDate = (date.millisecondsAgo()).duration();
95-
}
124+
displayDate = relativeAgeMediumSpan(distance, leaveAgo);
96125
}
97126
return "<span class='date' title='" + fullReadable + "'>" + displayDate + "</span>";
98127
};

config/locales/client.en.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
en:
99
js:
1010
dates:
11+
short_date_no_year: "D MMM"
12+
short_date: "D MMM, YYYY"
13+
long_date: "MMMM D, YYYY h:mma"
1114
tiny:
1215
half_a_minute: "< 1m"
1316
less_than_x_seconds:
@@ -43,6 +46,26 @@ en:
4346
almost_x_years:
4447
one: "1y"
4548
other: "%{count}y"
49+
medium:
50+
x_minutes:
51+
one: "1 minute"
52+
other: "%{count} minutes"
53+
x_hours:
54+
one: "1 hour"
55+
other: "%{count} hours"
56+
x_days:
57+
one: "1 day"
58+
other: "%{count} days"
59+
medium_with_ago:
60+
x_minutes:
61+
one: "1 minute ago"
62+
other: "%{count} minutes ago"
63+
x_hours:
64+
one: "1 hour ago"
65+
other: "%{count} hours ago"
66+
x_days:
67+
one: "1 day ago"
68+
other: "%{count} days ago"
4669
share:
4770
topic: 'share a link to this topic'
4871
post: 'share a link to this post'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// moment.js language configuration
2+
// language : Moroccan Arabic (ar-ma)
3+
// author : ElFadili Yassine : https://github.com/ElFadiliY
4+
// author : Abdel Said : https://github.com/abdelsaid
5+
6+
require('../moment').lang('ar-ma', {
7+
months : "يناير_فبراير_مارس_أبريل_ماي_يونيو_يوليوز_غشت_شتنبر_أكتوبر_نونبر_دجنبر".split("_"),
8+
monthsShort : "يناير_فبراير_مارس_أبريل_ماي_يونيو_يوليوز_غشت_شتنبر_أكتوبر_نونبر_دجنبر".split("_"),
9+
weekdays : "الأحد_الإتنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت".split("_"),
10+
weekdaysShort : "احد_اتنين_ثلاثاء_اربعاء_خميس_جمعة_سبت".split("_"),
11+
weekdaysMin : "ح_ن_ث_ر_خ_ج_س".split("_"),
12+
longDateFormat : {
13+
LT : "HH:mm",
14+
L : "DD/MM/YYYY",
15+
LL : "D MMMM YYYY",
16+
LLL : "D MMMM YYYY LT",
17+
LLLL : "dddd D MMMM YYYY LT"
18+
},
19+
calendar : {
20+
sameDay: "[اليوم على الساعة] LT",
21+
nextDay: '[غدا على الساعة] LT',
22+
nextWeek: 'dddd [على الساعة] LT',
23+
lastDay: '[أمس على الساعة] LT',
24+
lastWeek: 'dddd [على الساعة] LT',
25+
sameElse: 'L'
26+
},
27+
relativeTime : {
28+
future : "في %s",
29+
past : "منذ %s",
30+
s : "ثوان",
31+
m : "دقيقة",
32+
mm : "%d دقائق",
33+
h : "ساعة",
34+
hh : "%d ساعات",
35+
d : "يوم",
36+
dd : "%d أيام",
37+
M : "شهر",
38+
MM : "%d أشهر",
39+
y : "سنة",
40+
yy : "%d سنوات"
41+
},
42+
week : {
43+
dow : 6, // Saturday is the first day of the week.
44+
doy : 12 // The week that contains Jan 1st is the first week of the year.
45+
}
46+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// moment.js language configuration
2+
// language : Arabic (ar)
3+
// author : Abdel Said : https://github.com/abdelsaid
4+
// changes in months, weekdays : Ahmed Elkhatib
5+
6+
require('../moment').lang('ar', {
7+
months : "يناير/ كانون الثاني_فبراير/ شباط_مارس/ آذار_أبريل/ نيسان_مايو/ أيار_يونيو/ حزيران_يوليو/ تموز_أغسطس/ آب_سبتمبر/ أيلول_أكتوبر/ تشرين الأول_نوفمبر/ تشرين الثاني_ديسمبر/ كانون الأول".split("_"),
8+
monthsShort : "يناير/ كانون الثاني_فبراير/ شباط_مارس/ آذار_أبريل/ نيسان_مايو/ أيار_يونيو/ حزيران_يوليو/ تموز_أغسطس/ آب_سبتمبر/ أيلول_أكتوبر/ تشرين الأول_نوفمبر/ تشرين الثاني_ديسمبر/ كانون الأول".split("_"),
9+
weekdays : "الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت".split("_"),
10+
weekdaysShort : "الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت".split("_"),
11+
weekdaysMin : "ح_ن_ث_ر_خ_ج_س".split("_"),
12+
longDateFormat : {
13+
LT : "HH:mm",
14+
L : "DD/MM/YYYY",
15+
LL : "D MMMM YYYY",
16+
LLL : "D MMMM YYYY LT",
17+
LLLL : "dddd D MMMM YYYY LT"
18+
},
19+
calendar : {
20+
sameDay: "[اليوم على الساعة] LT",
21+
nextDay: '[غدا على الساعة] LT',
22+
nextWeek: 'dddd [على الساعة] LT',
23+
lastDay: '[أمس على الساعة] LT',
24+
lastWeek: 'dddd [على الساعة] LT',
25+
sameElse: 'L'
26+
},
27+
relativeTime : {
28+
future : "في %s",
29+
past : "منذ %s",
30+
s : "ثوان",
31+
m : "دقيقة",
32+
mm : "%d دقائق",
33+
h : "ساعة",
34+
hh : "%d ساعات",
35+
d : "يوم",
36+
dd : "%d أيام",
37+
M : "شهر",
38+
MM : "%d أشهر",
39+
y : "سنة",
40+
yy : "%d سنوات"
41+
},
42+
week : {
43+
dow : 6, // Saturday is the first day of the week.
44+
doy : 12 // The week that contains Jan 1st is the first week of the year.
45+
}
46+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// moment.js language configuration
2+
// language : bulgarian (bg)
3+
// author : Krasen Borisov : https://github.com/kraz
4+
5+
require('../moment').lang('bg', {
6+
months : "януари_февруари_март_април_май_юни_юли_август_септември_октомври_ноември_декември".split("_"),
7+
monthsShort : "янр_фев_мар_апр_май_юни_юли_авг_сеп_окт_ное_дек".split("_"),
8+
weekdays : "неделя_понеделник_вторник_сряда_четвъртък_петък_събота".split("_"),
9+
weekdaysShort : "нед_пон_вто_сря_чет_пет_съб".split("_"),
10+
weekdaysMin : "нд_пн_вт_ср_чт_пт_сб".split("_"),
11+
longDateFormat : {
12+
LT : "h:mm",
13+
L : "D.MM.YYYY",
14+
LL : "D MMMM YYYY",
15+
LLL : "D MMMM YYYY LT",
16+
LLLL : "dddd, D MMMM YYYY LT"
17+
},
18+
calendar : {
19+
sameDay : '[Днес в] LT',
20+
nextDay : '[Утре в] LT',
21+
nextWeek : 'dddd [в] LT',
22+
lastDay : '[Вчера в] LT',
23+
lastWeek : function () {
24+
switch (this.day()) {
25+
case 0:
26+
case 3:
27+
case 6:
28+
return '[В изминалата] dddd [в] LT';
29+
case 1:
30+
case 2:
31+
case 4:
32+
case 5:
33+
return '[В изминалия] dddd [в] LT';
34+
}
35+
},
36+
sameElse : 'L'
37+
},
38+
relativeTime : {
39+
future : "след %s",
40+
past : "преди %s",
41+
s : "няколко секунди",
42+
m : "минута",
43+
mm : "%d минути",
44+
h : "час",
45+
hh : "%d часа",
46+
d : "ден",
47+
dd : "%d дни",
48+
M : "месец",
49+
MM : "%d месеца",
50+
y : "година",
51+
yy : "%d години"
52+
},
53+
ordinal : function (number) {
54+
var lastDigit = number % 10,
55+
last2Digits = number % 100;
56+
if (number === 0) {
57+
return number + '-ев';
58+
} else if (last2Digits === 0) {
59+
return number + '-ен';
60+
} else if (last2Digits > 10 && last2Digits < 20) {
61+
return number + '-ти';
62+
} else if (lastDigit === 1) {
63+
return number + '-ви';
64+
} else if (lastDigit === 2) {
65+
return number + '-ри';
66+
} else if (lastDigit === 7 || lastDigit === 8) {
67+
return number + '-ми';
68+
} else {
69+
return number + '-ти';
70+
}
71+
},
72+
week : {
73+
dow : 1, // Monday is the first day of the week.
74+
doy : 7 // The week that contains Jan 1st is the first week of the year.
75+
}
76+
});

0 commit comments

Comments
 (0)