aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-09-04 10:53:05 -0700
committerAnton Tananaev <anton@traccar.org>2023-09-04 10:53:05 -0700
commit9a59d6524c45d5542433302a498f9ada4e45e806 (patch)
treeb779287db9104054ca5fbecf9f6218b2db46d8fe /modern/src/common
parent63eaa20047613c872979e2894fbdb5a0caab31f7 (diff)
downloadtrackermap-web-9a59d6524c45d5542433302a498f9ada4e45e806.tar.gz
trackermap-web-9a59d6524c45d5542433302a498f9ada4e45e806.tar.bz2
trackermap-web-9a59d6524c45d5542433302a498f9ada4e45e806.zip
Migrate to DayJs from moment
Diffstat (limited to 'modern/src/common')
-rw-r--r--modern/src/common/components/LocalizationProvider.jsx123
-rw-r--r--modern/src/common/util/formatter.js21
2 files changed, 74 insertions, 70 deletions
diff --git a/modern/src/common/components/LocalizationProvider.jsx b/modern/src/common/components/LocalizationProvider.jsx
index 2ace1e24..d48b12ce 100644
--- a/modern/src/common/components/LocalizationProvider.jsx
+++ b/modern/src/common/components/LocalizationProvider.jsx
@@ -2,67 +2,66 @@
import React, {
createContext, useContext, useEffect, useMemo,
} from 'react';
-import moment from 'moment';
-import 'moment/min/locales.min';
-
-import af from '../../resources/l10n/af.json';
-import ar from '../../resources/l10n/ar.json';
-import az from '../../resources/l10n/az.json';
-import bg from '../../resources/l10n/bg.json';
-import bn from '../../resources/l10n/bn.json';
-import ca from '../../resources/l10n/ca.json';
-import cs from '../../resources/l10n/cs.json';
-import da from '../../resources/l10n/da.json';
-import de from '../../resources/l10n/de.json';
-import el from '../../resources/l10n/el.json';
-import en from '../../resources/l10n/en.json';
-import es from '../../resources/l10n/es.json';
-import fa from '../../resources/l10n/fa.json';
-import fi from '../../resources/l10n/fi.json';
-import fr from '../../resources/l10n/fr.json';
-import gl from '../../resources/l10n/gl.json';
-import he from '../../resources/l10n/he.json';
-import hi from '../../resources/l10n/hi.json';
-import hr from '../../resources/l10n/hr.json';
-import hu from '../../resources/l10n/hu.json';
-import id from '../../resources/l10n/id.json';
-import it from '../../resources/l10n/it.json';
-import ja from '../../resources/l10n/ja.json';
-import ka from '../../resources/l10n/ka.json';
-import kk from '../../resources/l10n/kk.json';
-import km from '../../resources/l10n/km.json';
-import ko from '../../resources/l10n/ko.json';
-import lo from '../../resources/l10n/lo.json';
-import lt from '../../resources/l10n/lt.json';
-import lv from '../../resources/l10n/lv.json';
-import ml from '../../resources/l10n/ml.json';
-import mn from '../../resources/l10n/mn.json';
-import ms from '../../resources/l10n/ms.json';
-import nb from '../../resources/l10n/nb.json';
-import ne from '../../resources/l10n/ne.json';
-import nl from '../../resources/l10n/nl.json';
-import nn from '../../resources/l10n/nn.json';
-import pl from '../../resources/l10n/pl.json';
-import pt from '../../resources/l10n/pt.json';
-import ptBR from '../../resources/l10n/pt_BR.json';
-import ro from '../../resources/l10n/ro.json';
-import ru from '../../resources/l10n/ru.json';
-import si from '../../resources/l10n/si.json';
-import sk from '../../resources/l10n/sk.json';
-import sl from '../../resources/l10n/sl.json';
-import sq from '../../resources/l10n/sq.json';
-import sr from '../../resources/l10n/sr.json';
-import sv from '../../resources/l10n/sv.json';
-import ta from '../../resources/l10n/ta.json';
-import th from '../../resources/l10n/th.json';
-import tr from '../../resources/l10n/tr.json';
-import uk from '../../resources/l10n/uk.json';
-import uz from '../../resources/l10n/uz.json';
-import vi from '../../resources/l10n/vi.json';
-import zh from '../../resources/l10n/zh.json';
-import zhTW from '../../resources/l10n/zh_TW.json';
+import dayjs from 'dayjs';
import usePersistedState from '../util/usePersistedState';
+import af from '../../resources/l10n/af.json'; import 'dayjs/locale/af';
+import ar from '../../resources/l10n/ar.json'; import 'dayjs/locale/ar';
+import az from '../../resources/l10n/az.json'; import 'dayjs/locale/az';
+import bg from '../../resources/l10n/bg.json'; import 'dayjs/locale/bg';
+import bn from '../../resources/l10n/bn.json'; import 'dayjs/locale/bn';
+import ca from '../../resources/l10n/ca.json'; import 'dayjs/locale/ca';
+import cs from '../../resources/l10n/cs.json'; import 'dayjs/locale/cs';
+import da from '../../resources/l10n/da.json'; import 'dayjs/locale/da';
+import de from '../../resources/l10n/de.json'; import 'dayjs/locale/de';
+import el from '../../resources/l10n/el.json'; import 'dayjs/locale/el';
+import en from '../../resources/l10n/en.json'; import 'dayjs/locale/en';
+import es from '../../resources/l10n/es.json'; import 'dayjs/locale/es';
+import fa from '../../resources/l10n/fa.json'; import 'dayjs/locale/fa';
+import fi from '../../resources/l10n/fi.json'; import 'dayjs/locale/fi';
+import fr from '../../resources/l10n/fr.json'; import 'dayjs/locale/fr';
+import gl from '../../resources/l10n/gl.json'; import 'dayjs/locale/gl';
+import he from '../../resources/l10n/he.json'; import 'dayjs/locale/he';
+import hi from '../../resources/l10n/hi.json'; import 'dayjs/locale/hi';
+import hr from '../../resources/l10n/hr.json'; import 'dayjs/locale/hr';
+import hu from '../../resources/l10n/hu.json'; import 'dayjs/locale/hu';
+import id from '../../resources/l10n/id.json'; import 'dayjs/locale/id';
+import it from '../../resources/l10n/it.json'; import 'dayjs/locale/it';
+import ja from '../../resources/l10n/ja.json'; import 'dayjs/locale/ja';
+import ka from '../../resources/l10n/ka.json'; import 'dayjs/locale/ka';
+import kk from '../../resources/l10n/kk.json'; import 'dayjs/locale/kk';
+import km from '../../resources/l10n/km.json'; import 'dayjs/locale/km';
+import ko from '../../resources/l10n/ko.json'; import 'dayjs/locale/ko';
+import lo from '../../resources/l10n/lo.json'; import 'dayjs/locale/lo';
+import lt from '../../resources/l10n/lt.json'; import 'dayjs/locale/lt';
+import lv from '../../resources/l10n/lv.json'; import 'dayjs/locale/lv';
+import ml from '../../resources/l10n/ml.json'; import 'dayjs/locale/ml';
+import mn from '../../resources/l10n/mn.json'; import 'dayjs/locale/mn';
+import ms from '../../resources/l10n/ms.json'; import 'dayjs/locale/ms';
+import nb from '../../resources/l10n/nb.json'; import 'dayjs/locale/nb';
+import ne from '../../resources/l10n/ne.json'; import 'dayjs/locale/ne';
+import nl from '../../resources/l10n/nl.json'; import 'dayjs/locale/nl';
+import nn from '../../resources/l10n/nn.json'; import 'dayjs/locale/nn';
+import pl from '../../resources/l10n/pl.json'; import 'dayjs/locale/pl';
+import pt from '../../resources/l10n/pt.json'; import 'dayjs/locale/pt';
+import ptBR from '../../resources/l10n/pt_BR.json'; import 'dayjs/locale/pt-br';
+import ro from '../../resources/l10n/ro.json'; import 'dayjs/locale/ro';
+import ru from '../../resources/l10n/ru.json'; import 'dayjs/locale/ru';
+import si from '../../resources/l10n/si.json'; import 'dayjs/locale/si';
+import sk from '../../resources/l10n/sk.json'; import 'dayjs/locale/sk';
+import sl from '../../resources/l10n/sl.json'; import 'dayjs/locale/sl';
+import sq from '../../resources/l10n/sq.json'; import 'dayjs/locale/sq';
+import sr from '../../resources/l10n/sr.json'; import 'dayjs/locale/sr';
+import sv from '../../resources/l10n/sv.json'; import 'dayjs/locale/sv';
+import ta from '../../resources/l10n/ta.json'; import 'dayjs/locale/ta';
+import th from '../../resources/l10n/th.json'; import 'dayjs/locale/th';
+import tr from '../../resources/l10n/tr.json'; import 'dayjs/locale/tr';
+import uk from '../../resources/l10n/uk.json'; import 'dayjs/locale/uk';
+import uz from '../../resources/l10n/uz.json'; import 'dayjs/locale/uz';
+import vi from '../../resources/l10n/vi.json'; import 'dayjs/locale/vi';
+import zh from '../../resources/l10n/zh.json'; import 'dayjs/locale/zh';
+import zhTW from '../../resources/l10n/zh_TW.json'; import 'dayjs/locale/zh-tw';
+
const languages = {
af: { data: af, country: 'ZA', name: 'Afrikaans' },
ar: { data: ar, country: 'AE', name: 'العربية' },
@@ -156,14 +155,12 @@ export const LocalizationProvider = ({ children }) => {
useEffect(() => {
let selected;
- if (language === 'zh') {
- selected = 'zh-cn';
- } else if (language.length > 2) {
+ if (language.length > 2) {
selected = `${language.slice(0, 2)}-${language.slice(-2).toLowerCase()}`;
} else {
selected = language;
}
- moment.locale([selected, 'en']);
+ dayjs.locale(selected);
}, [language]);
return (
diff --git a/modern/src/common/util/formatter.js b/modern/src/common/util/formatter.js
index b00896c2..370542d1 100644
--- a/modern/src/common/util/formatter.js
+++ b/modern/src/common/util/formatter.js
@@ -1,4 +1,8 @@
-import moment from 'moment';
+import dayjs from 'dayjs';
+import 'dayjs/plugin/duration';
+import duration from 'dayjs/plugin/duration';
+import relativeTime from 'dayjs/plugin/relativeTime';
+
import {
altitudeFromMeters,
altitudeUnitString,
@@ -11,6 +15,9 @@ import {
} from './converter';
import { prefixString } from './stringUtils';
+dayjs.extend(duration)
+dayjs.extend(relativeTime)
+
export const formatBoolean = (value, t) => (value ? t('sharedYes') : t('sharedNo'));
export const formatNumber = (value, precision = 1) => Number(value.toFixed(precision));
@@ -25,16 +32,16 @@ export const formatConsumption = (value, t) => `${value} ${t('sharedLiterPerHour
export const formatTime = (value, format, hours12) => {
if (value) {
- const m = moment(value);
+ const d = dayjs(value);
switch (format) {
case 'date':
- return m.format('YYYY-MM-DD');
+ return d.format('YYYY-MM-DD');
case 'time':
- return m.format(hours12 ? 'hh:mm:ss A' : 'HH:mm:ss');
+ return d.format(hours12 ? 'hh:mm:ss A' : 'HH:mm:ss');
case 'minutes':
- return m.format(hours12 ? 'YYYY-MM-DD hh:mm A' : 'YYYY-MM-DD HH:mm');
+ return d.format(hours12 ? 'YYYY-MM-DD hh:mm A' : 'YYYY-MM-DD HH:mm');
default:
- return m.format(hours12 ? 'YYYY-MM-DD hh:mm:ss A' : 'YYYY-MM-DD HH:mm:ss');
+ return d.format(hours12 ? 'YYYY-MM-DD hh:mm:ss A' : 'YYYY-MM-DD HH:mm:ss');
}
}
return '';
@@ -60,7 +67,7 @@ export const formatSpeed = (value, unit, t) => `${speedFromKnots(value, unit).to
export const formatVolume = (value, unit, t) => `${volumeFromLiters(value, unit).toFixed(2)} ${volumeUnitString(unit, t)}`;
-export const formatHours = (value) => moment.duration(value).humanize();
+export const formatHours = (value) => dayjs.duration(value).humanize();
export const formatNumericHours = (value, t) => {
const hours = Math.floor(value / 3600000);