localise dates
This commit is contained in:
parent
65b42ba1e7
commit
a8584096d8
|
@ -1,5 +1,6 @@
|
|||
### [Unreleased]
|
||||
#### Added
|
||||
- Localise dates based on the WordPress locale
|
||||
#### Changed
|
||||
- Clearly list features in `readme.txt` description
|
||||
#### Deprecated
|
||||
|
|
|
@ -1,47 +1,43 @@
|
|||
import test from 'ava'
|
||||
import DateTimeWrapper from './date-time-wrapper'
|
||||
|
||||
test.before(() => {
|
||||
DateTimeWrapper.setDefaultLocale('en-GB')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date', t => {
|
||||
const d = new DateTimeWrapper('2020-12-24T16:45:00Z')
|
||||
const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#get24Time usual time', t => {
|
||||
const d = new DateTimeWrapper('2020-12-24T16:45:00Z')
|
||||
t.is(d.get24Time(), '17:45')
|
||||
const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.get24Time(), '17:45')
|
||||
})
|
||||
|
||||
test('#equalsDate same date, different time', t => {
|
||||
const d = new DateTimeWrapper('2020-12-24T16:45:00Z')
|
||||
const e = new DateTimeWrapper('2020-12-24T17:46:01Z')
|
||||
const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T17:46:01Z' })
|
||||
t.true(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different date, different time', t => {
|
||||
const d = new DateTimeWrapper('2020-12-24T16:45:00Z')
|
||||
const e = new DateTimeWrapper('2021-11-25T17:46:01Z')
|
||||
const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ locale: 'en-GB', text: '2021-11-25T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different day, different time', t => {
|
||||
const d = new DateTimeWrapper('2020-12-24T16:45:00Z')
|
||||
const e = new DateTimeWrapper('2020-12-25T17:46:01Z')
|
||||
const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-25T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different month, different time', t => {
|
||||
const d = new DateTimeWrapper('2020-12-24T16:45:00Z')
|
||||
const e = new DateTimeWrapper('2020-11-24T17:46:01Z')
|
||||
const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ locale: 'en-GB', text: '2020-11-24T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different year, different time', t => {
|
||||
const d = new DateTimeWrapper('2020-12-24T16:45:00Z')
|
||||
const e = new DateTimeWrapper('2021-12-24T17:46:01Z')
|
||||
const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ locale: 'en-GB', text: '2021-12-24T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { DateTime, Settings } from 'luxon'
|
||||
import { DateTime } from 'luxon'
|
||||
|
||||
export default class DateTimeWrapper {
|
||||
|
||||
constructor(text) {
|
||||
this.dateTime = DateTime.fromISO(text)
|
||||
constructor({ locale, text }) {
|
||||
this.dateTime = DateTime.fromISO(text, { locale })
|
||||
}
|
||||
|
||||
getShortDate() {
|
||||
|
@ -25,8 +25,4 @@ export default class DateTimeWrapper {
|
|||
static getCurrentDatetimeAsString() {
|
||||
return DateTime.now().toString()
|
||||
}
|
||||
|
||||
static setDefaultLocale(locale) {
|
||||
Settings.defaultLocale = locale
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import { createAnchorElement } from './html-creator'
|
|||
const NAME = '<wordpress-name>'
|
||||
|
||||
function displayEvents(data, list) {
|
||||
const locale = list.getAttribute('data-locale')
|
||||
const maxEventsCount = list.getAttribute('data-maximum')
|
||||
const events = data.events ? data.events.elements : data.group.organizedEvents.elements
|
||||
const eventsCount = Math.min(maxEventsCount, events.length)
|
||||
|
@ -17,7 +18,7 @@ function displayEvents(data, list) {
|
|||
const br = document.createElement('br')
|
||||
li.appendChild(br)
|
||||
|
||||
const date = Formatter.formatDate({ start: events[i].beginsOn, end: events[i].endsOn })
|
||||
const date = Formatter.formatDate({ locale, start: events[i].beginsOn, end: events[i].endsOn })
|
||||
const textnode = document.createTextNode(date)
|
||||
li.appendChild(textnode)
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@ import test from 'ava'
|
|||
import Formatter from './formatter'
|
||||
|
||||
test('#formatDate one date', t => {
|
||||
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-15T15:30:00Z' })
|
||||
const date = Formatter.formatDate({ locale: 'en-GB', start: '2021-04-15T10:30:00Z', end: '2021-04-15T15:30:00Z' })
|
||||
t.is(date, '15/04/2021 12:30 - 17:30')
|
||||
})
|
||||
|
||||
test('#formatDate two dates', t => {
|
||||
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-16T15:30:00Z' })
|
||||
const date = Formatter.formatDate({ locale: 'en-GB', start: '2021-04-15T10:30:00Z', end: '2021-04-16T15:30:00Z' })
|
||||
t.is(date, '15/04/2021 12:30 - 16/04/2021 17:30')
|
||||
})
|
||||
|
||||
|
|
|
@ -2,9 +2,9 @@ import DateTimeWrapper from './date-time-wrapper'
|
|||
|
||||
export default class Formatter {
|
||||
|
||||
static formatDate({ start, end }) {
|
||||
const startDateTime = new DateTimeWrapper(start)
|
||||
const endDateTime = new DateTimeWrapper(end)
|
||||
static formatDate({ locale, start, end }) {
|
||||
const startDateTime = new DateTimeWrapper({ locale, text: start })
|
||||
const endDateTime = new DateTimeWrapper({ locale, text: end })
|
||||
let dateText = startDateTime.getShortDate()
|
||||
dateText += ' ' + startDateTime.get24Time()
|
||||
dateText += ' - '
|
||||
|
|
|
@ -26,6 +26,7 @@ class EventsListShortcut {
|
|||
|
||||
$classNamePrefix = NAME;
|
||||
$eventsCount = $atts_with_overriden_defaults['events-count'];
|
||||
$locale = str_replace('_', '-', get_locale());
|
||||
$groupName = $atts_with_overriden_defaults['group-name'];
|
||||
$url = Settings::getUrl();
|
||||
$textDomain = TEXT_DOMAIN;
|
||||
|
|
|
@ -27,6 +27,7 @@ class EventsListWidget extends \WP_Widget {
|
|||
|
||||
$classNamePrefix = NAME;
|
||||
$eventsCount = $options['eventsCount'];
|
||||
$locale = str_replace('_', '-', get_locale());
|
||||
$groupName = isset($options['groupName']) ? $options['groupName'] : '';
|
||||
$url = Settings::getUrl();
|
||||
$textDomain = TEXT_DOMAIN;
|
||||
|
|
|
@ -6,6 +6,7 @@ if (!defined('ABSPATH')) {
|
|||
?>
|
||||
<ul class="<?php echo esc_attr($classNamePrefix); ?>_events-list"
|
||||
data-url="<?php echo esc_attr($url); ?>"
|
||||
data-locale="<?php echo esc_attr($locale); ?>"
|
||||
data-maximum="<?php echo esc_attr($eventsCount); ?>"
|
||||
data-group-name="<?php echo esc_attr($groupName); ?>">
|
||||
<li style="display: none;"><?php echo esc_html_e('The events could not be loaded!', $textDomain); ?></li>
|
||||
|
|
Loading…
Reference in New Issue