mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
- The existing common.i18n library contained code for core and theme translations - There is some shared logic and some theme-specific logic, and the theme-specific logic has dependencies we don't want in lib/common - This refactor introduces an I18n base class that does all the main shared logic, with no dependencies on other parts of the codebase - ThemeI18n then extends this logic, and replaces the functions it needs to handle differently and adds it's dependencies on config and settingsCache - The class has several methods broken down into smaller pieces to make it easier to extend only the necessary parts - The class also encapsulates all of its logic, without external functions or variables - The function loadThemeTranslations becomes the 'init()' function overridden in themeI18n.
47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
// # Date Helper
|
|
// Usage: `{{date format="DD MM, YYYY"}}`, `{{date updated_at format="DD MM, YYYY"}}`
|
|
//
|
|
// Formats a date using moment-timezone.js. Formats published_at by default but will also take a date as a parameter
|
|
|
|
const {SafeString, themeI18n} = require('./proxy');
|
|
const moment = require('moment-timezone');
|
|
|
|
module.exports = function (date, options) {
|
|
let timezone;
|
|
|
|
if (!options && Object.prototype.hasOwnProperty.call(date, 'hash')) {
|
|
options = date;
|
|
date = undefined;
|
|
timezone = options.data.site.timezone;
|
|
|
|
// set to published_at by default, if it's available
|
|
// otherwise, this will print the current date
|
|
if (this.published_at) {
|
|
date = moment(this.published_at).tz(timezone).format();
|
|
}
|
|
}
|
|
|
|
const {
|
|
format = 'MMM DD, YYYY',
|
|
timeago
|
|
} = options.hash;
|
|
|
|
// ensure that context is undefined, not null, as that can cause errors
|
|
date = date === null ? undefined : date;
|
|
timezone = options.data.site.timezone;
|
|
const timeNow = moment().tz(timezone);
|
|
|
|
// i18n: Making dates, including month names, translatable to any language.
|
|
// Documentation: http://momentjs.com/docs/#/i18n/
|
|
// Locales: https://github.com/moment/moment/tree/develop/locale
|
|
const dateMoment = moment(date);
|
|
dateMoment.locale(themeI18n.locale());
|
|
|
|
if (timeago) {
|
|
date = timezone ? dateMoment.tz(timezone).from(timeNow) : dateMoment.fromNow();
|
|
} else {
|
|
date = timezone ? dateMoment.tz(timezone).format(format) : dateMoment.format(format);
|
|
}
|
|
|
|
return new SafeString(date);
|
|
};
|