Update date translations when locale changes

This commit is contained in:
Ben Grant 2021-05-25 17:29:56 +10:00
parent 96fa0b0488
commit 22eeacfe67
7 changed files with 35 additions and 15 deletions

View file

@ -1,5 +1,6 @@
import { useState, useRef, Fragment } from 'react'; import { useState, useRef, Fragment } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useLocaleUpdateStore } from 'stores';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import localeData from 'dayjs/plugin/localeData'; import localeData from 'dayjs/plugin/localeData';
import customParseFormat from 'dayjs/plugin/customParseFormat'; import customParseFormat from 'dayjs/plugin/customParseFormat';
@ -38,6 +39,8 @@ const AvailabilityEditor = ({
...props ...props
}) => { }) => {
const { t } = useTranslation('event'); const { t } = useTranslation('event');
const locale = useLocaleUpdateStore(state => state.locale);
const [selectingTimes, _setSelectingTimes] = useState([]); const [selectingTimes, _setSelectingTimes] = useState([]);
const staticSelectingTimes = useRef([]); const staticSelectingTimes = useRef([]);
const setSelectingTimes = newTimes => { const setSelectingTimes = newTimes => {
@ -73,7 +76,7 @@ const AvailabilityEditor = ({
</StyledMain> </StyledMain>
)} )}
<Wrapper> <Wrapper locale={locale}>
<ScrollWrapper> <ScrollWrapper>
<Container> <Container>
<TimeLabels> <TimeLabels>

View file

@ -5,7 +5,7 @@ import localeData from 'dayjs/plugin/localeData';
import customParseFormat from 'dayjs/plugin/customParseFormat'; import customParseFormat from 'dayjs/plugin/customParseFormat';
import relativeTime from 'dayjs/plugin/relativeTime'; import relativeTime from 'dayjs/plugin/relativeTime';
import { useSettingsStore } from 'stores'; import { useSettingsStore, useLocaleUpdateStore } from 'stores';
import { Legend, Center } from 'components'; import { Legend, Center } from 'components';
import { import {
@ -52,7 +52,9 @@ const AvailabilityViewer = ({
const [touched, setTouched] = useState(false); const [touched, setTouched] = useState(false);
const [tempFocus, setTempFocus] = useState(null); const [tempFocus, setTempFocus] = useState(null);
const [focusCount, setFocusCount] = useState(null); const [focusCount, setFocusCount] = useState(null);
const { t } = useTranslation('event'); const { t } = useTranslation('event');
const locale = useLocaleUpdateStore(state => state.locale);
const wrapper = useRef(); const wrapper = useRef();
@ -118,7 +120,7 @@ const AvailabilityViewer = ({
return ( return (
<Fragment key={i}> <Fragment key={i}>
<Date> <Date>
{isSpecificDates && <DateLabel>{parsedDate.format('MMM D')}</DateLabel>} {isSpecificDates && <DateLabel locale={locale}>{parsedDate.format('MMM D')}</DateLabel>}
<DayLabel>{parsedDate.format('ddd')}</DayLabel> <DayLabel>{parsedDate.format('ddd')}</DayLabel>
<Times <Times

View file

@ -6,7 +6,7 @@ import localeData from 'dayjs/plugin/localeData';
import updateLocale from 'dayjs/plugin/updateLocale'; import updateLocale from 'dayjs/plugin/updateLocale';
import { Button, ToggleField } from 'components'; import { Button, ToggleField } from 'components';
import { useSettingsStore } from 'stores'; import { useSettingsStore, useLocaleUpdateStore } from 'stores';
import { import {
Wrapper, Wrapper,
@ -57,6 +57,8 @@ const CalendarField = ({
...props ...props
}) => { }) => {
const weekStart = useSettingsStore(state => state.weekStart); const weekStart = useSettingsStore(state => state.weekStart);
const locale = useLocaleUpdateStore(state => state.locale);
const setLocale = useLocaleUpdateStore(state => state.setLocale);
const { t, i18n } = useTranslation('home'); const { t, i18n } = useTranslation('home');
const [type, setType] = useState(0); const [type, setType] = useState(0);
@ -93,6 +95,7 @@ const CalendarField = ({
if (Object.keys(localeImports).includes(i18n.language)) { if (Object.keys(localeImports).includes(i18n.language)) {
localeImports[i18n.language]().then(() => { localeImports[i18n.language]().then(() => {
dayjs.locale(i18n.language); dayjs.locale(i18n.language);
setLocale(dayjs.locale());
if (weekStart !== dayjs.Ls[i18n.language].weekStart) { if (weekStart !== dayjs.Ls[i18n.language].weekStart) {
dayjs.updateLocale(i18n.language, { weekStart }); dayjs.updateLocale(i18n.language, { weekStart });
} }
@ -104,10 +107,10 @@ const CalendarField = ({
} }
} }
setDates(calculateMonth(month, year, weekStart)); setDates(calculateMonth(month, year, weekStart));
}, [weekStart, month, year, i18n.language]); }, [weekStart, month, year, i18n.language, setLocale]);
return ( return (
<Wrapper> <Wrapper locale={locale}>
{label && <StyledLabel htmlFor={id}>{label}</StyledLabel>} {label && <StyledLabel htmlFor={id}>{label}</StyledLabel>}
{subLabel && <StyledSubLabel htmlFor={id}>{subLabel}</StyledSubLabel>} {subLabel && <StyledSubLabel htmlFor={id}>{subLabel}</StyledSubLabel>}
<input <input

View file

@ -1,5 +1,5 @@
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useRecentsStore } from 'stores'; import { useRecentsStore, useLocaleUpdateStore } from 'stores';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'; import relativeTime from 'dayjs/plugin/relativeTime';
@ -10,6 +10,7 @@ dayjs.extend(relativeTime);
const Recents = () => { const Recents = () => {
const recents = useRecentsStore(state => state.recents); const recents = useRecentsStore(state => state.recents);
const locale = useLocaleUpdateStore(state => state.locale);
const { t } = useTranslation(['home', 'common']); const { t } = useTranslation(['home', 'common']);
return !!recents.length && ( return !!recents.length && (
@ -19,7 +20,7 @@ const Recents = () => {
{recents.map(event => ( {recents.map(event => (
<Recent href={`/${event.id}`} key={event.id}> <Recent href={`/${event.id}`} key={event.id}>
<span className="name">{event.name}</span> <span className="name">{event.name}</span>
<span className="date" title={dayjs.unix(event.created).format('D MMMM, YYYY')}>{t('common:created', { date: dayjs.unix(event.created).fromNow() })}</span> <span locale={locale} className="date" title={dayjs.unix(event.created).format('D MMMM, YYYY')}>{t('common:created', { date: dayjs.unix(event.created).fromNow() })}</span>
</Recent> </Recent>
))} ))}
</StyledMain> </StyledMain>

View file

@ -1,7 +1,7 @@
import { useState, useEffect, useRef } from 'react'; import { useState, useEffect, useRef } from 'react';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { useSettingsStore } from 'stores'; import { useSettingsStore, useLocaleUpdateStore } from 'stores';
import { import {
Wrapper, Wrapper,
@ -22,6 +22,7 @@ const TimeRangeField = ({
...props ...props
}) => { }) => {
const timeFormat = useSettingsStore(state => state.timeFormat); const timeFormat = useSettingsStore(state => state.timeFormat);
const locale = useLocaleUpdateStore(state => state.locale);
const [start, setStart] = useState(9); const [start, setStart] = useState(9);
const [end, setEnd] = useState(17); const [end, setEnd] = useState(17);
@ -53,7 +54,7 @@ const TimeRangeField = ({
}; };
return ( return (
<Wrapper> <Wrapper locale={locale}>
{label && <StyledLabel htmlFor={id}>{label}</StyledLabel>} {label && <StyledLabel htmlFor={id}>{label}</StyledLabel>}
{subLabel && <StyledSubLabel htmlFor={id}>{subLabel}</StyledSubLabel>} {subLabel && <StyledSubLabel htmlFor={id}>{subLabel}</StyledSubLabel>}
<input <input

View file

@ -35,7 +35,7 @@ import {
} from './eventStyle'; } from './eventStyle';
import api from 'services'; import api from 'services';
import { useSettingsStore, useRecentsStore } from 'stores'; import { useSettingsStore, useRecentsStore, useLocaleUpdateStore } from 'stores';
import logo from 'res/logo.svg'; import logo from 'res/logo.svg';
import timezones from 'res/timezones.json'; import timezones from 'res/timezones.json';
@ -51,6 +51,8 @@ const Event = (props) => {
const weekStart = useSettingsStore(state => state.weekStart); const weekStart = useSettingsStore(state => state.weekStart);
const addRecent = useRecentsStore(state => state.addRecent); const addRecent = useRecentsStore(state => state.addRecent);
const setLocale = useLocaleUpdateStore(state => state.setLocale);
const locale = useLocaleUpdateStore(state => state.locale);
const { t, i18n } = useTranslation(['common', 'event']); const { t, i18n } = useTranslation(['common', 'event']);
@ -77,9 +79,12 @@ const Event = (props) => {
useEffect(() => { useEffect(() => {
if (Object.keys(localeImports).includes(i18n.language)) { if (Object.keys(localeImports).includes(i18n.language)) {
localeImports[i18n.language]().then(() => dayjs.locale(i18n.language)); localeImports[i18n.language]().then(() => {
dayjs.locale(i18n.language);
setLocale(dayjs.locale());
});
} }
}, [i18n.language]); }, [i18n.language, setLocale]);
useEffect(() => { useEffect(() => {
const fetchEvent = async () => { const fetchEvent = async () => {
@ -210,7 +215,7 @@ const Event = (props) => {
return [...allDates, date]; return [...allDates, date];
}, [])); }, []));
} }
}, [times, timeFormat]); }, [times, timeFormat, locale]);
useEffect(() => { useEffect(() => {
const fetchUser = async () => { const fetchUser = async () => {
@ -296,7 +301,7 @@ const Event = (props) => {
{(!!event || isLoading) ? ( {(!!event || isLoading) ? (
<> <>
<EventName isLoading={isLoading}>{event?.name}</EventName> <EventName isLoading={isLoading}>{event?.name}</EventName>
<EventDate isLoading={isLoading} title={event?.created && dayjs.unix(event?.created).format('D MMMM, YYYY')}>{event?.created && t('common:created', { date: dayjs.unix(event?.created).fromNow() })}</EventDate> <EventDate isLoading={isLoading} locale={locale} title={event?.created && dayjs.unix(event?.created).format('D MMMM, YYYY')}>{event?.created && t('common:created', { date: dayjs.unix(event?.created).fromNow() })}</EventDate>
<ShareInfo <ShareInfo
onClick={() => navigator.clipboard?.writeText(`https://crab.fit/${id}`) onClick={() => navigator.clipboard?.writeText(`https://crab.fit/${id}`)
.then(() => { .then(() => {

View file

@ -39,3 +39,8 @@ export const useTWAStore = create(set => ({
TWA: undefined, TWA: undefined,
setTWA: TWA => set({ TWA }), setTWA: TWA => set({ TWA }),
})); }));
export const useLocaleUpdateStore = create(set => ({
locale: undefined,
setLocale: locale => set({ locale }),
}));