1
0
mirror of https://github.com/dwaxweiler/connector-mobilizon synced 2025-02-17 20:21:05 +01:00

localise dates

This commit is contained in:
Daniel Waxweiler 2021-05-04 11:16:18 +02:00
parent 65b42ba1e7
commit a8584096d8
9 changed files with 27 additions and 30 deletions

View File

@ -1,5 +1,6 @@
### [Unreleased] ### [Unreleased]
#### Added #### Added
- Localise dates based on the WordPress locale
#### Changed #### Changed
- Clearly list features in `readme.txt` description - Clearly list features in `readme.txt` description
#### Deprecated #### Deprecated

View File

@ -1,47 +1,43 @@
import test from 'ava' import test from 'ava'
import DateTimeWrapper from './date-time-wrapper' import DateTimeWrapper from './date-time-wrapper'
test.before(() => {
DateTimeWrapper.setDefaultLocale('en-GB')
})
test('#getShortDate usual date', t => { 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') t.is(d.getShortDate(), '24/12/2020')
}) })
test('#get24Time usual time', t => { test('#get24Time usual time', 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.get24Time(), '17:45') t.is(d.get24Time(), '17:45')
}) })
test('#equalsDate same date, different time', t => { test('#equalsDate same date, different time', t => {
const d = new DateTimeWrapper('2020-12-24T16:45:00Z') const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
const e = new DateTimeWrapper('2020-12-24T17:46:01Z') const e = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T17:46:01Z' })
t.true(d.equalsDate(e)) t.true(d.equalsDate(e))
}) })
test('#equalsDate different date, different time', t => { test('#equalsDate different date, different time', t => {
const d = new DateTimeWrapper('2020-12-24T16:45:00Z') const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
const e = new DateTimeWrapper('2021-11-25T17:46:01Z') const e = new DateTimeWrapper({ locale: 'en-GB', text: '2021-11-25T17:46:01Z' })
t.false(d.equalsDate(e)) t.false(d.equalsDate(e))
}) })
test('#equalsDate different day, different time', t => { test('#equalsDate different day, different time', t => {
const d = new DateTimeWrapper('2020-12-24T16:45:00Z') const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
const e = new DateTimeWrapper('2020-12-25T17:46:01Z') const e = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-25T17:46:01Z' })
t.false(d.equalsDate(e)) t.false(d.equalsDate(e))
}) })
test('#equalsDate different month, different time', t => { test('#equalsDate different month, different time', t => {
const d = new DateTimeWrapper('2020-12-24T16:45:00Z') const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
const e = new DateTimeWrapper('2020-11-24T17:46:01Z') const e = new DateTimeWrapper({ locale: 'en-GB', text: '2020-11-24T17:46:01Z' })
t.false(d.equalsDate(e)) t.false(d.equalsDate(e))
}) })
test('#equalsDate different year, different time', t => { test('#equalsDate different year, different time', t => {
const d = new DateTimeWrapper('2020-12-24T16:45:00Z') const d = new DateTimeWrapper({ locale: 'en-GB', text: '2020-12-24T16:45:00Z' })
const e = new DateTimeWrapper('2021-12-24T17:46:01Z') const e = new DateTimeWrapper({ locale: 'en-GB', text: '2021-12-24T17:46:01Z' })
t.false(d.equalsDate(e)) t.false(d.equalsDate(e))
}) })

View File

@ -1,9 +1,9 @@
import { DateTime, Settings } from 'luxon' import { DateTime } from 'luxon'
export default class DateTimeWrapper { export default class DateTimeWrapper {
constructor(text) { constructor({ locale, text }) {
this.dateTime = DateTime.fromISO(text) this.dateTime = DateTime.fromISO(text, { locale })
} }
getShortDate() { getShortDate() {
@ -25,8 +25,4 @@ export default class DateTimeWrapper {
static getCurrentDatetimeAsString() { static getCurrentDatetimeAsString() {
return DateTime.now().toString() return DateTime.now().toString()
} }
static setDefaultLocale(locale) {
Settings.defaultLocale = locale
}
} }

View File

@ -5,6 +5,7 @@ import { createAnchorElement } from './html-creator'
const NAME = '<wordpress-name>' const NAME = '<wordpress-name>'
function displayEvents(data, list) { function displayEvents(data, list) {
const locale = list.getAttribute('data-locale')
const maxEventsCount = list.getAttribute('data-maximum') const maxEventsCount = list.getAttribute('data-maximum')
const events = data.events ? data.events.elements : data.group.organizedEvents.elements const events = data.events ? data.events.elements : data.group.organizedEvents.elements
const eventsCount = Math.min(maxEventsCount, events.length) const eventsCount = Math.min(maxEventsCount, events.length)
@ -17,7 +18,7 @@ function displayEvents(data, list) {
const br = document.createElement('br') const br = document.createElement('br')
li.appendChild(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) const textnode = document.createTextNode(date)
li.appendChild(textnode) li.appendChild(textnode)

View File

@ -2,12 +2,12 @@ import test from 'ava'
import Formatter from './formatter' import Formatter from './formatter'
test('#formatDate one date', t => { 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') t.is(date, '15/04/2021 12:30 - 17:30')
}) })
test('#formatDate two dates', t => { 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') t.is(date, '15/04/2021 12:30 - 16/04/2021 17:30')
}) })

View File

@ -2,9 +2,9 @@ import DateTimeWrapper from './date-time-wrapper'
export default class Formatter { export default class Formatter {
static formatDate({ start, end }) { static formatDate({ locale, start, end }) {
const startDateTime = new DateTimeWrapper(start) const startDateTime = new DateTimeWrapper({ locale, text: start })
const endDateTime = new DateTimeWrapper(end) const endDateTime = new DateTimeWrapper({ locale, text: end })
let dateText = startDateTime.getShortDate() let dateText = startDateTime.getShortDate()
dateText += ' ' + startDateTime.get24Time() dateText += ' ' + startDateTime.get24Time()
dateText += ' - ' dateText += ' - '

View File

@ -26,6 +26,7 @@ class EventsListShortcut {
$classNamePrefix = NAME; $classNamePrefix = NAME;
$eventsCount = $atts_with_overriden_defaults['events-count']; $eventsCount = $atts_with_overriden_defaults['events-count'];
$locale = str_replace('_', '-', get_locale());
$groupName = $atts_with_overriden_defaults['group-name']; $groupName = $atts_with_overriden_defaults['group-name'];
$url = Settings::getUrl(); $url = Settings::getUrl();
$textDomain = TEXT_DOMAIN; $textDomain = TEXT_DOMAIN;

View File

@ -27,6 +27,7 @@ class EventsListWidget extends \WP_Widget {
$classNamePrefix = NAME; $classNamePrefix = NAME;
$eventsCount = $options['eventsCount']; $eventsCount = $options['eventsCount'];
$locale = str_replace('_', '-', get_locale());
$groupName = isset($options['groupName']) ? $options['groupName'] : ''; $groupName = isset($options['groupName']) ? $options['groupName'] : '';
$url = Settings::getUrl(); $url = Settings::getUrl();
$textDomain = TEXT_DOMAIN; $textDomain = TEXT_DOMAIN;

View File

@ -6,6 +6,7 @@ if (!defined('ABSPATH')) {
?> ?>
<ul class="<?php echo esc_attr($classNamePrefix); ?>_events-list" <ul class="<?php echo esc_attr($classNamePrefix); ?>_events-list"
data-url="<?php echo esc_attr($url); ?>" data-url="<?php echo esc_attr($url); ?>"
data-locale="<?php echo esc_attr($locale); ?>"
data-maximum="<?php echo esc_attr($eventsCount); ?>" data-maximum="<?php echo esc_attr($eventsCount); ?>"
data-group-name="<?php echo esc_attr($groupName); ?>"> 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> <li style="display: none;"><?php echo esc_html_e('The events could not be loaded!', $textDomain); ?></li>