Skip to content

Commit 0620dfa

Browse files
authored
Merge pull request taskrabbit#35 from taskrabbit/translation
Translation
2 parents b2b7a8e + a007e98 commit 0620dfa

15 files changed

+946
-276
lines changed

App/Lib/DateUtil.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import Locale from '../Locale';
2+
3+
var i18nLib = Locale.lib();
4+
5+
global._Date = global._Date || Date;
6+
7+
var DateUtil = {
8+
// localEpoch: function(epochSeconds) {
9+
// var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
10+
// d.setUTCSeconds(epochSeconds);
11+
// return d;
12+
// },
13+
14+
now: function() {
15+
return new _Date();
16+
},
17+
18+
minutes: function(number) {
19+
return number * 60 * 1000;
20+
},
21+
22+
hours: function(number) {
23+
return number * this.minutes(60);
24+
},
25+
26+
days: function(number) {
27+
return number * this.hours(24);
28+
},
29+
30+
timeAgoInWords: function(milliseconds) {
31+
if (milliseconds < (new Date('January 01, 1970 00:00:00')).getTime()) {
32+
console.warn('timeAgoInSeconds requires milliseconds >= Epoch');
33+
return null;
34+
}
35+
const now = this.now().getTime(),
36+
minutesAgo = Math.floor((now - milliseconds) / 1000 / 60);
37+
38+
if (minutesAgo < 1) return i18n.t('time_ago_less_than');
39+
if (minutesAgo === 1) return i18n.t('time_ago_min');
40+
if (minutesAgo < 60) return i18n.t('time_ago_mins', {minutes: minutesAgo});
41+
42+
const hoursAgo = Math.floor(minutesAgo / 60);
43+
if (hoursAgo === 1) return i18n.t('time_ago_hour');
44+
if (hoursAgo < 24) return i18n.t('time_ago_hours', {hours: hoursAgo});
45+
46+
const daysAgo = Math.floor(hoursAgo / 24);
47+
if (daysAgo === 1) return i18n.t('time_ago_day');
48+
if (daysAgo < 7) return i18n.t('time_ago_days', {days: daysAgo});
49+
50+
const weeksAgo = Math.floor(daysAgo / 7);
51+
if (weeksAgo === 1) return i18n.t('time_ago_week');
52+
return i18n.t('time_ago_weeks', {weeks: weeksAgo});
53+
},
54+
55+
isToday(date) {
56+
var parsedDate = this.parse(date);
57+
if (!parsedDate) return false;
58+
59+
return parsedDate.toDateString() === this.now().toDateString();
60+
},
61+
62+
isPast(date) {
63+
var parsedDate = this.parse(date);
64+
if (!parsedDate) return false;
65+
66+
return parsedDate < this.now();
67+
},
68+
69+
isFuture(date) {
70+
return !this.isPast(date);
71+
},
72+
73+
// use this one instead of strftime, please. it's better for i18n
74+
format: function(date, name) {
75+
var pattern = i18nLib.t('datetime.' + name);
76+
return this.strftime(date, pattern);
77+
},
78+
79+
// strftime stuff we support:
80+
// https://github.com/fnando/i18n-js#date-formatting
81+
// %+A : Today, Tomorrow, Monday
82+
strftime: function(date, pattern) {
83+
date = this.parse(date);
84+
if (!date) return null;
85+
86+
if (pattern.indexOf('%+A') >= 0) {
87+
// %+A : Today, Tomorrow, Monday
88+
var dayName = '%A'; // normal one
89+
var today = new _Date();
90+
var tomorrow = new _Date();
91+
tomorrow.setDate(tomorrow.getDate() + 1);
92+
93+
if (date.toDateString() === today.toDateString()) {
94+
dayName = i18n.t('today');
95+
}
96+
else if (date.toDateString() === tomorrow.toDateString()) {
97+
dayName = i18n.t('tomorrow');
98+
}
99+
100+
pattern = pattern.replace(/%\+A/g, dayName);
101+
}
102+
103+
return i18nLib.strftime(date, pattern);
104+
},
105+
106+
localISOString: function(date) {
107+
var d = this.parse(date);
108+
d.setHours(0, -d.getTimezoneOffset(), 0, 0);
109+
110+
return d.toISOString().slice(0, 10);
111+
},
112+
113+
dayWindowName: function(date) {
114+
date = this.parse(date);
115+
if (!date) return null;
116+
117+
var hour = date.getHours();
118+
if (hour < 12) {
119+
return i18n.t('morning');
120+
}
121+
else if (hour < 16) {
122+
return i18n.t('afternoon');
123+
}
124+
else {
125+
return i18n.t('evening');
126+
}
127+
},
128+
129+
parse: function(intOrDateOrString) {
130+
if (intOrDateOrString === null) return null;
131+
132+
// have to know if it's an number if it's in millseconds or seconds
133+
if (typeof intOrDateOrString === 'number') {
134+
// using number of digits to decide (12 digits is 1973)
135+
if (intOrDateOrString.toString().length <= 12) {
136+
intOrDateOrString = intOrDateOrString * 1000;
137+
}
138+
}
139+
return i18nLib.parseDate(intOrDateOrString);
140+
},
141+
142+
toEpoch: function(intOrDateOrString) {
143+
var date = this.parse(intOrDateOrString);
144+
if (date === null) return null;
145+
146+
return Math.round(date.getTime() / 1000);
147+
},
148+
};
149+
150+
var i18n = Locale.key('DateUtil', {
151+
morning: 'Morning',
152+
afternoon: 'Afternoon',
153+
evening: 'Evening',
154+
today: 'Today',
155+
tomorrow: 'Tomorrow',
156+
time_ago_less_than: 'less than 1 min ago',
157+
time_ago_min: '1 min ago',
158+
time_ago_mins: '%{minutes} mins ago',
159+
time_ago_hour: '1 hour ago',
160+
time_ago_hours: '%{hours} hours ago',
161+
time_ago_day: '1 day ago',
162+
time_ago_days: '%{days} days ago',
163+
time_ago_week: '1 week ago',
164+
time_ago_weeks: '%{weeks} weeks ago',
165+
});
166+
167+
export default DateUtil;

App/Locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Manager.prototype.t = function(scope, options) {
1717
scope = this.key + '.' + scope; // prepend our key
1818
}
1919
return I18n.t(scope, options);
20-
}
20+
};
2121

2222
var Locale = {
2323
key: function(object, enHash) {

App/Locales/boot.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ I18n.translations['en-GB'] = require('../Locales/en-GB.js').default;
99
I18n.translations['en-GB-xtra'] = require('../Locales/en-GB-xtra.js').default; // programmatic - ideally empty and in translated
1010

1111

12-
I18n.default_locale = "en-US";
13-
I18n.locale = "en-US";
12+
I18n.default_locale = 'en-US';
13+
I18n.locale = 'en-US';
1414

1515
I18n.fallbacks = {
1616
'en-US': ['en', 'base'],
17-
'en-GB': ['en-GB-xtra', 'en', 'base']
17+
'en-GB': ['en-GB-xtra', 'en', 'base'],
1818
};
1919

2020
const countryIsoCodesToLocale = {

App/Locales/en-GB-xtra.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export default {
1+
// Programatic GB translations - do not add directly
2+
// Use npm run translation:backfill
23

3-
};
4+
export default {};

App/Locales/en-GB.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default {
77
short_day: '%a %b %-d',
88
short_time: '%-H:%M',
99
month_day: '%B %-d',
10+
short_month_day: '%b %-d',
1011
month_day_year: '%e %b %Y',
1112
month_day_year_at_time: '%-d %B %Y at %-H:%M',
1213
short_weekday_name: '%a',

App/Locales/en-US.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
export default {
44
datetime: {
5-
day_and_time: "%a, %b %-d, %-I:%M %p",
6-
day_header: "%+A - %B %d",
7-
short_day: "%a %b %-d",
8-
short_time: "%-I:%M %p",
9-
month_day: "%B %-d",
10-
month_day_year: "%b %e, %Y",
11-
month_day_year_at_time: "%B %-d, %Y at %-I:%M %p",
12-
short_weekday_name: "%a",
13-
long_day_of_month: "%d",
14-
time_zone: "%Z"
5+
day_and_time: '%a, %b %-d, %-I:%M %p',
6+
day_header: '%+A - %B %d',
7+
short_day: '%a %b %-d',
8+
short_time: '%-I:%M %p',
9+
month_day: '%B %-d',
10+
short_month_day: '%b %-d',
11+
month_day_year: '%b %e, %Y',
12+
month_day_year_at_time: '%B %-d, %Y at %-I:%M %p',
13+
short_weekday_name: '%a',
14+
long_day_of_month: '%d',
15+
time_zone: '%Z',
1516
},
1617
number: {
1718
currency: {

0 commit comments

Comments
 (0)