1
1
import * as timeago from 'timeago.js' ;
2
2
import { newDate } from '~/lib/utils/datetime/date_calculation_utility' ;
3
3
import { DEFAULT_DATE_TIME_FORMAT , localeDateFormat } from '~/lib/utils/datetime/locale_dateformat' ;
4
- import { languageCode , s__ } from '~/locale' ;
4
+ import { languageCode , getPluralFormIndex , s__ , n__ } from '~/locale' ;
5
5
6
6
/**
7
7
* Timeago uses underscores instead of dashes to separate language from country code.
@@ -10,64 +10,104 @@ import { languageCode, s__ } from '~/locale';
10
10
*/
11
11
export const timeagoLanguageCode = languageCode ( ) . replace ( / - / g, '_' ) ;
12
12
13
+ const i18n = {
14
+ justNow : s__ ( 'Timeago|just now' ) ,
15
+ rightNow : s__ ( 'Timeago|right now' ) ,
16
+ secondsAgoPlural : ( n ) => n__ ( 'Timeago|1 second ago' , 'Timeago|%s seconds ago' , n ) ,
17
+ secondsRemainingPlural : ( n ) =>
18
+ n__ ( 'Timeago|1 second remaining' , 'Timeago|%s seconds remaining' , n ) ,
19
+ inSecondsPlural : ( n ) => n__ ( 'Timeago|in 1 second' , 'Timeago|in %s seconds' , n ) ,
20
+ durationSecondsPlural : ( n ) => n__ ( 'Duration|1 second' , 'Duration|%s seconds' , n ) ,
21
+ minutesAgoPlural : ( n ) => n__ ( 'Timeago|1 minute ago' , 'Timeago|%s minutes ago' , n ) ,
22
+ minutesRemainingPlural : ( n ) =>
23
+ n__ ( 'Timeago|1 minute remaining' , 'Timeago|%s minutes remaining' , n ) ,
24
+ inMinutesPlural : ( n ) => n__ ( 'Timeago|in 1 minute' , 'Timeago|in %s minutes' , n ) ,
25
+ durationMinutesPlural : ( n ) => n__ ( 'Duration|1 minute' , 'Duration|%s minutes' , n ) ,
26
+ hoursAgoPlural : ( n ) => n__ ( 'Timeago|1 hour ago' , 'Timeago|%s hours ago' , n ) ,
27
+ hoursRemainingPlural : ( n ) => n__ ( 'Timeago|1 hour remaining' , 'Timeago|%s hours remaining' , n ) ,
28
+ inHoursPlural : ( n ) => n__ ( 'Timeago|in 1 hour' , 'Timeago|in %s hours' , n ) ,
29
+ durationHoursPlural : ( n ) => n__ ( 'Duration|1 hour' , 'Duration|%s hours' , n ) ,
30
+ daysAgoPlural : ( n ) => n__ ( 'Timeago|1 day ago' , 'Timeago|%s days ago' , n ) ,
31
+ daysRemainingPlural : ( n ) => n__ ( 'Timeago|1 day remaining' , 'Timeago|%s days remaining' , n ) ,
32
+ inDaysPlural : ( n ) => n__ ( 'Timeago|in 1 day' , 'Timeago|in %s days' , n ) ,
33
+ durationDaysPlural : ( n ) => n__ ( 'Duration|1 day' , 'Duration|%s days' , n ) ,
34
+ weeksAgoPlural : ( n ) => n__ ( 'Timeago|1 week ago' , 'Timeago|%s weeks ago' , n ) ,
35
+ weeksRemainingPlural : ( n ) => n__ ( 'Timeago|1 week remaining' , 'Timeago|%s weeks remaining' , n ) ,
36
+ inWeeksPlural : ( n ) => n__ ( 'Timeago|in 1 week' , 'Timeago|in %s weeks' , n ) ,
37
+ durationWeeksPlural : ( n ) => n__ ( 'Duration|1 week' , 'Duration|%s weeks' , n ) ,
38
+ monthsAgoPlural : ( n ) => n__ ( 'Timeago|1 month ago' , 'Timeago|%s months ago' , n ) ,
39
+ monthsRemainingPlural : ( n ) => n__ ( 'Timeago|1 month remaining' , 'Timeago|%s months remaining' , n ) ,
40
+ inMonthsPlural : ( n ) => n__ ( 'Timeago|in 1 month' , 'Timeago|in %s months' , n ) ,
41
+ durationMonthsPlural : ( n ) => n__ ( 'Duration|1 month' , 'Duration|%s months' , n ) ,
42
+ yearsAgoPlural : ( n ) => n__ ( 'Timeago|1 year ago' , 'Timeago|%s years ago' , n ) ,
43
+ yearsRemainingPlural : ( n ) => n__ ( 'Timeago|1 year remaining' , 'Timeago|%s years remaining' , n ) ,
44
+ inYearsPlural : ( n ) => n__ ( 'Timeago|in 1 year' , 'Timeago|in %s years' , n ) ,
45
+ durationYearsPlural : ( n ) => n__ ( 'Duration|1 year' , 'Duration|%s years' , n ) ,
46
+ pastDue : s__ ( 'Timeago|Past due' ) ,
47
+ } ;
48
+
13
49
/**
14
50
* Registers timeago locales
15
51
*/
16
52
const memoizedLocaleRemaining = ( ) => {
17
53
const cache = [ ] ;
18
54
19
- const timeAgoLocaleRemaining = [
20
- ( ) => [ s__ ( 'Timeago|just now' ) , s__ ( 'Timeago|right now' ) ] ,
21
- ( ) => [ s__ ( 'Timeago|just now' ) , s__ ( 'Timeago|%s seconds remaining' ) ] ,
22
- ( ) => [ s__ ( 'Timeago|1 minute ago' ) , s__ ( 'Timeago|1 minute remaining' ) ] ,
23
- ( ) => [ s__ ( 'Timeago|%s minutes ago' ) , s__ ( 'Timeago|%s minutes remaining' ) ] ,
24
- ( ) => [ s__ ( 'Timeago|1 hour ago' ) , s__ ( 'Timeago|1 hour remaining' ) ] ,
25
- ( ) => [ s__ ( 'Timeago|%s hours ago' ) , s__ ( 'Timeago|%s hours remaining' ) ] ,
26
- ( ) => [ s__ ( 'Timeago|1 day ago' ) , s__ ( 'Timeago|1 day remaining' ) ] ,
27
- ( ) => [ s__ ( 'Timeago|%s days ago' ) , s__ ( 'Timeago|%s days remaining' ) ] ,
28
- ( ) => [ s__ ( 'Timeago|1 week ago' ) , s__ ( 'Timeago|1 week remaining' ) ] ,
29
- ( ) => [ s__ ( 'Timeago|%s weeks ago' ) , s__ ( 'Timeago|%s weeks remaining' ) ] ,
30
- ( ) => [ s__ ( 'Timeago|1 month ago' ) , s__ ( 'Timeago|1 month remaining' ) ] ,
31
- ( ) => [ s__ ( 'Timeago|%s months ago' ) , s__ ( 'Timeago|%s months remaining' ) ] ,
32
- ( ) => [ s__ ( 'Timeago|1 year ago' ) , s__ ( 'Timeago|1 year remaining' ) ] ,
33
- ( ) => [ s__ ( 'Timeago|%s years ago' ) , s__ ( 'Timeago|%s years remaining' ) ] ,
55
+ const locales = [
56
+ ( ) => [ i18n . justNow , i18n . rightNow ] ,
57
+ ( n ) => [ i18n . secondsAgoPlural ( n ) , i18n . secondsRemainingPlural ( n ) ] ,
58
+ ( ) => [ i18n . minutesAgoPlural ( 1 ) , i18n . minutesRemainingPlural ( 1 ) ] ,
59
+ ( n ) => [ i18n . minutesAgoPlural ( n ) , i18n . minutesRemainingPlural ( n ) ] ,
60
+ ( ) => [ i18n . hoursAgoPlural ( 1 ) , i18n . hoursRemainingPlural ( 1 ) ] ,
61
+ ( n ) => [ i18n . hoursAgoPlural ( n ) , i18n . hoursRemainingPlural ( n ) ] ,
62
+ ( ) => [ i18n . daysAgoPlural ( 1 ) , i18n . daysRemainingPlural ( 1 ) ] ,
63
+ ( n ) => [ i18n . daysAgoPlural ( n ) , i18n . daysRemainingPlural ( n ) ] ,
64
+ ( ) => [ i18n . weeksAgoPlural ( 1 ) , i18n . weeksRemainingPlural ( 1 ) ] ,
65
+ ( n ) => [ i18n . weeksAgoPlural ( n ) , i18n . weeksRemainingPlural ( n ) ] ,
66
+ ( ) => [ i18n . monthsAgoPlural ( 1 ) , i18n . monthsRemainingPlural ( 1 ) ] ,
67
+ ( n ) => [ i18n . monthsAgoPlural ( n ) , i18n . monthsRemainingPlural ( n ) ] ,
68
+ ( ) => [ i18n . yearsAgoPlural ( 1 ) , i18n . yearsRemainingPlural ( 1 ) ] ,
69
+ ( n ) => [ i18n . yearsAgoPlural ( n ) , i18n . yearsRemainingPlural ( n ) ] ,
34
70
] ;
35
71
36
72
return ( number , index ) => {
37
- if ( cache [ index ] ) {
38
- return cache [ index ] ;
73
+ const form = getPluralFormIndex ( number ) ;
74
+ const cacheKey = `${ index } -${ form } ` ;
75
+ if ( ! cache [ cacheKey ] ) {
76
+ cache [ cacheKey ] = locales [ index ] && locales [ index ] ( number ) ;
39
77
}
40
- cache [ index ] = timeAgoLocaleRemaining [ index ] && timeAgoLocaleRemaining [ index ] ( ) ;
41
- return cache [ index ] ;
78
+
79
+ return cache [ cacheKey ] ;
42
80
} ;
43
81
} ;
44
82
45
83
const memoizedLocale = ( ) => {
46
84
const cache = [ ] ;
47
85
48
- const timeAgoLocale = [
49
- ( ) => [ s__ ( 'Timeago|just now' ) , s__ ( 'Timeago|right now' ) ] ,
50
- ( ) => [ s__ ( 'Timeago|just now' ) , s__ ( 'Timeago|in %s seconds' ) ] ,
51
- ( ) => [ s__ ( 'Timeago|1 minute ago' ) , s__ ( 'Timeago|in 1 minute' ) ] ,
52
- ( ) => [ s__ ( 'Timeago|%s minutes ago' ) , s__ ( 'Timeago|in %s minutes' ) ] ,
53
- ( ) => [ s__ ( 'Timeago|1 hour ago' ) , s__ ( 'Timeago|in 1 hour' ) ] ,
54
- ( ) => [ s__ ( 'Timeago|%s hours ago' ) , s__ ( 'Timeago|in %s hours' ) ] ,
55
- ( ) => [ s__ ( 'Timeago|1 day ago' ) , s__ ( 'Timeago|in 1 day' ) ] ,
56
- ( ) => [ s__ ( 'Timeago|%s days ago' ) , s__ ( 'Timeago|in %s days' ) ] ,
57
- ( ) => [ s__ ( 'Timeago|1 week ago' ) , s__ ( 'Timeago|in 1 week' ) ] ,
58
- ( ) => [ s__ ( 'Timeago|%s weeks ago' ) , s__ ( 'Timeago|in %s weeks' ) ] ,
59
- ( ) => [ s__ ( 'Timeago|1 month ago' ) , s__ ( 'Timeago|in 1 month' ) ] ,
60
- ( ) => [ s__ ( 'Timeago|%s months ago' ) , s__ ( 'Timeago|in %s months' ) ] ,
61
- ( ) => [ s__ ( 'Timeago|1 year ago' ) , s__ ( 'Timeago|in 1 year' ) ] ,
62
- ( ) => [ s__ ( 'Timeago|%s years ago' ) , s__ ( 'Timeago|in %s years' ) ] ,
86
+ const locales = [
87
+ ( ) => [ i18n . justNow , i18n . rightNow ] ,
88
+ ( n ) => [ i18n . secondsAgoPlural ( n ) , i18n . inSecondsPlural ( n ) ] ,
89
+ ( ) => [ i18n . minutesAgoPlural ( 1 ) , i18n . inMinutesPlural ( 1 ) ] ,
90
+ ( n ) => [ i18n . minutesAgoPlural ( n ) , i18n . inMinutesPlural ( n ) ] ,
91
+ ( ) => [ i18n . hoursAgoPlural ( 1 ) , i18n . inHoursPlural ( 1 ) ] ,
92
+ ( n ) => [ i18n . hoursAgoPlural ( n ) , i18n . inHoursPlural ( n ) ] ,
93
+ ( ) => [ i18n . daysAgoPlural ( 1 ) , i18n . inDaysPlural ( 1 ) ] ,
94
+ ( n ) => [ i18n . daysAgoPlural ( n ) , i18n . inDaysPlural ( n ) ] ,
95
+ ( ) => [ i18n . weeksAgoPlural ( 1 ) , i18n . inWeeksPlural ( 1 ) ] ,
96
+ ( n ) => [ i18n . weeksAgoPlural ( n ) , i18n . inWeeksPlural ( n ) ] ,
97
+ ( ) => [ i18n . monthsAgoPlural ( 1 ) , i18n . inMonthsPlural ( 1 ) ] ,
98
+ ( n ) => [ i18n . monthsAgoPlural ( n ) , i18n . inMonthsPlural ( n ) ] ,
99
+ ( ) => [ i18n . yearsAgoPlural ( 1 ) , i18n . inYearsPlural ( 1 ) ] ,
100
+ ( n ) => [ i18n . yearsAgoPlural ( n ) , i18n . inYearsPlural ( n ) ] ,
63
101
] ;
64
102
65
103
return ( number , index ) => {
66
- if ( cache [ index ] ) {
67
- return cache [ index ] ;
104
+ const form = getPluralFormIndex ( number ) ;
105
+ const cacheKey = `${ index } -${ form } ` ;
106
+ if ( ! cache [ cacheKey ] ) {
107
+ cache [ cacheKey ] = locales [ index ] && locales [ index ] ( number ) ;
68
108
}
69
- cache [ index ] = timeAgoLocale [ index ] && timeAgoLocale [ index ] ( ) ;
70
- return cache [ index ] ;
109
+
110
+ return cache [ cacheKey ] ;
71
111
} ;
72
112
} ;
73
113
@@ -77,29 +117,31 @@ const memoizedLocale = () => {
77
117
const memoizedLocaleDuration = ( ) => {
78
118
const cache = [ ] ;
79
119
80
- const durations = [
81
- ( ) => [ s__ ( 'Duration|%s seconds' ) ] ,
82
- ( ) => [ s__ ( 'Duration|%s seconds' ) ] ,
83
- ( ) => [ s__ ( 'Duration|1 minute' ) ] ,
84
- ( ) => [ s__ ( 'Duration|%s minutes' ) ] ,
85
- ( ) => [ s__ ( 'Duration|1 hour' ) ] ,
86
- ( ) => [ s__ ( 'Duration|%s hours' ) ] ,
87
- ( ) => [ s__ ( 'Duration|1 day' ) ] ,
88
- ( ) => [ s__ ( 'Duration|%s days' ) ] ,
89
- ( ) => [ s__ ( 'Duration|1 week' ) ] ,
90
- ( ) => [ s__ ( 'Duration|%s weeks' ) ] ,
91
- ( ) => [ s__ ( 'Duration|1 month' ) ] ,
92
- ( ) => [ s__ ( 'Duration|%s months' ) ] ,
93
- ( ) => [ s__ ( 'Duration|1 year' ) ] ,
94
- ( ) => [ s__ ( 'Duration|%s years' ) ] ,
120
+ const locales = [
121
+ ( n ) => [ i18n . durationSecondsPlural ( n ) ] ,
122
+ ( n ) => [ i18n . durationSecondsPlural ( n ) ] ,
123
+ ( ) => [ i18n . durationMinutesPlural ( 1 ) ] ,
124
+ ( n ) => [ i18n . durationMinutesPlural ( n ) ] ,
125
+ ( ) => [ i18n . durationHoursPlural ( 1 ) ] ,
126
+ ( n ) => [ i18n . durationHoursPlural ( n ) ] ,
127
+ ( ) => [ i18n . durationDaysPlural ( 1 ) ] ,
128
+ ( n ) => [ i18n . durationDaysPlural ( n ) ] ,
129
+ ( ) => [ i18n . durationWeeksPlural ( 1 ) ] ,
130
+ ( n ) => [ i18n . durationWeeksPlural ( n ) ] ,
131
+ ( ) => [ i18n . durationMonthsPlural ( 1 ) ] ,
132
+ ( n ) => [ i18n . durationMonthsPlural ( n ) ] ,
133
+ ( ) => [ i18n . durationYearsPlural ( 1 ) ] ,
134
+ ( n ) => [ i18n . durationYearsPlural ( n ) ] ,
95
135
] ;
96
136
97
- return ( _ , index ) => {
98
- if ( cache [ index ] ) {
99
- return cache [ index ] ;
137
+ return ( number , index ) => {
138
+ const form = getPluralFormIndex ( number ) ;
139
+ const cacheKey = `${ index } -${ form } ` ;
140
+ if ( ! cache [ cacheKey ] ) {
141
+ cache [ cacheKey ] = locales [ index ] && locales [ index ] ( number ) ;
100
142
}
101
- cache [ index ] = durations [ index ] && durations [ index ] ( ) ;
102
- return cache [ index ] ;
143
+
144
+ return cache [ cacheKey ] ;
103
145
} ;
104
146
} ;
105
147
@@ -147,7 +189,7 @@ export const timeFor = (time, expiredLabel) => {
147
189
return '' ;
148
190
}
149
191
if ( new Date ( time ) < new Date ( ) ) {
150
- return expiredLabel || s__ ( 'Timeago|Past due' ) ;
192
+ return expiredLabel || i18n . pastDue ;
151
193
}
152
194
return timeago . format ( time , `${ timeagoLanguageCode } -remaining` ) . trim ( ) ;
153
195
} ;
0 commit comments