Skip to content

Translation #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions App/Lib/DateUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import Locale from '../Locale';

var i18nLib = Locale.lib();

global._Date = global._Date || Date;

var DateUtil = {
// localEpoch: function(epochSeconds) {
// var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
// d.setUTCSeconds(epochSeconds);
// return d;
// },

now: function() {
return new _Date();
},

minutes: function(number) {
return number * 60 * 1000;
},

hours: function(number) {
return number * this.minutes(60);
},

days: function(number) {
return number * this.hours(24);
},

timeAgoInWords: function(milliseconds) {
if (milliseconds < (new Date('January 01, 1970 00:00:00')).getTime()) {
console.warn('timeAgoInSeconds requires milliseconds >= Epoch');
return null;
}
const now = this.now().getTime(),
minutesAgo = Math.floor((now - milliseconds) / 1000 / 60);

if (minutesAgo < 1) return i18n.t('time_ago_less_than');
if (minutesAgo === 1) return i18n.t('time_ago_min');
if (minutesAgo < 60) return i18n.t('time_ago_mins', {minutes: minutesAgo});

const hoursAgo = Math.floor(minutesAgo / 60);
if (hoursAgo === 1) return i18n.t('time_ago_hour');
if (hoursAgo < 24) return i18n.t('time_ago_hours', {hours: hoursAgo});

const daysAgo = Math.floor(hoursAgo / 24);
if (daysAgo === 1) return i18n.t('time_ago_day');
if (daysAgo < 7) return i18n.t('time_ago_days', {days: daysAgo});

const weeksAgo = Math.floor(daysAgo / 7);
if (weeksAgo === 1) return i18n.t('time_ago_week');
return i18n.t('time_ago_weeks', {weeks: weeksAgo});
},

isToday(date) {
var parsedDate = this.parse(date);
if (!parsedDate) return false;

return parsedDate.toDateString() === this.now().toDateString();
},

isPast(date) {
var parsedDate = this.parse(date);
if (!parsedDate) return false;

return parsedDate < this.now();
},

isFuture(date) {
return !this.isPast(date);
},

// use this one instead of strftime, please. it's better for i18n
format: function(date, name) {
var pattern = i18nLib.t('datetime.' + name);
return this.strftime(date, pattern);
},

// strftime stuff we support:
// https://github.com/fnando/i18n-js#date-formatting
// %+A : Today, Tomorrow, Monday
strftime: function(date, pattern) {
date = this.parse(date);
if (!date) return null;

if (pattern.indexOf('%+A') >= 0) {
// %+A : Today, Tomorrow, Monday
var dayName = '%A'; // normal one
var today = new _Date();
var tomorrow = new _Date();
tomorrow.setDate(tomorrow.getDate() + 1);

if (date.toDateString() === today.toDateString()) {
dayName = i18n.t('today');
}
else if (date.toDateString() === tomorrow.toDateString()) {
dayName = i18n.t('tomorrow');
}

pattern = pattern.replace(/%\+A/g, dayName);
}

return i18nLib.strftime(date, pattern);
},

localISOString: function(date) {
var d = this.parse(date);
d.setHours(0, -d.getTimezoneOffset(), 0, 0);

return d.toISOString().slice(0, 10);
},

dayWindowName: function(date) {
date = this.parse(date);
if (!date) return null;

var hour = date.getHours();
if (hour < 12) {
return i18n.t('morning');
}
else if (hour < 16) {
return i18n.t('afternoon');
}
else {
return i18n.t('evening');
}
},

parse: function(intOrDateOrString) {
if (intOrDateOrString === null) return null;

// have to know if it's an number if it's in millseconds or seconds
if (typeof intOrDateOrString === 'number') {
// using number of digits to decide (12 digits is 1973)
if (intOrDateOrString.toString().length <= 12) {
intOrDateOrString = intOrDateOrString * 1000;
}
}
return i18nLib.parseDate(intOrDateOrString);
},

toEpoch: function(intOrDateOrString) {
var date = this.parse(intOrDateOrString);
if (date === null) return null;

return Math.round(date.getTime() / 1000);
},
};

var i18n = Locale.key('DateUtil', {
morning: 'Morning',
afternoon: 'Afternoon',
evening: 'Evening',
today: 'Today',
tomorrow: 'Tomorrow',
time_ago_less_than: 'less than 1 min ago',
time_ago_min: '1 min ago',
time_ago_mins: '%{minutes} mins ago',
time_ago_hour: '1 hour ago',
time_ago_hours: '%{hours} hours ago',
time_ago_day: '1 day ago',
time_ago_days: '%{days} days ago',
time_ago_week: '1 week ago',
time_ago_weeks: '%{weeks} weeks ago',
});

export default DateUtil;
2 changes: 1 addition & 1 deletion App/Locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Manager.prototype.t = function(scope, options) {
scope = this.key + '.' + scope; // prepend our key
}
return I18n.t(scope, options);
}
};

var Locale = {
key: function(object, enHash) {
Expand Down
6 changes: 3 additions & 3 deletions App/Locales/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ I18n.translations['en-GB'] = require('../Locales/en-GB.js').default;
I18n.translations['en-GB-xtra'] = require('../Locales/en-GB-xtra.js').default; // programmatic - ideally empty and in translated


I18n.default_locale = "en-US";
I18n.locale = "en-US";
I18n.default_locale = 'en-US';
I18n.locale = 'en-US';

I18n.fallbacks = {
'en-US': ['en', 'base'],
'en-GB': ['en-GB-xtra', 'en', 'base']
'en-GB': ['en-GB-xtra', 'en', 'base'],
};

const countryIsoCodesToLocale = {
Expand Down
5 changes: 3 additions & 2 deletions App/Locales/en-GB-xtra.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
// Programatic GB translations - do not add directly
// Use npm run translation:backfill

};
export default {};
1 change: 1 addition & 0 deletions App/Locales/en-GB.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
short_day: '%a %b %-d',
short_time: '%-H:%M',
month_day: '%B %-d',
short_month_day: '%b %-d',
month_day_year: '%e %b %Y',
month_day_year_at_time: '%-d %B %Y at %-H:%M',
short_weekday_name: '%a',
Expand Down
21 changes: 11 additions & 10 deletions App/Locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

export default {
datetime: {
day_and_time: "%a, %b %-d, %-I:%M %p",
day_header: "%+A - %B %d",
short_day: "%a %b %-d",
short_time: "%-I:%M %p",
month_day: "%B %-d",
month_day_year: "%b %e, %Y",
month_day_year_at_time: "%B %-d, %Y at %-I:%M %p",
short_weekday_name: "%a",
long_day_of_month: "%d",
time_zone: "%Z"
day_and_time: '%a, %b %-d, %-I:%M %p',
day_header: '%+A - %B %d',
short_day: '%a %b %-d',
short_time: '%-I:%M %p',
month_day: '%B %-d',
short_month_day: '%b %-d',
month_day_year: '%b %e, %Y',
month_day_year_at_time: '%B %-d, %Y at %-I:%M %p',
short_weekday_name: '%a',
long_day_of_month: '%d',
time_zone: '%Z',
},
number: {
currency: {
Expand Down
Loading