Commit 061e95f8 authored by Matija Obreza's avatar Matija Obreza

Merge branch '718-date-localization-with-moment-js' into 'master'

Date localization with moment.js

Closes #718

See merge request genesys-pgr/genesys-ui!697
parents 9d0c1bed ed64495f
......@@ -17,7 +17,7 @@ import { i18nClient } from '../i18n/i18n-client';
import detectLocaleFromPath from '../server/middleware/detectLocaleFromPath';
import getDir from '../server/middleware/detectDirection';
import { receiveLang } from 'actions/applicationConfig';
import TimeAgo from 'javascript-time-ago';
import * as moment from 'moment';
// JSS & MUI
import { MuiThemeProvider } from '@material-ui/core/styles';
......@@ -47,7 +47,12 @@ const SsrI18nProvider = withSSR()(I18nextProvider) as any;
// Configure axios for client
configureBackendApi({ apiUrl: store.getState().applicationConfig.apiUrl || process.env.API_URL || 'http://localhost:8080' });
// Set date's locale for client
import(`javascript-time-ago/locale/${detectedLang}`).then((locale) => TimeAgo.addLocale(locale));
// moment.js uses en locale as default and don't have separate file for it so import('moment/locale/en') causes error
if (detectedLang !== 'en') {
import(`moment/locale/${detectedLang}`).then(() => moment.locale(detectedLang));
} else {
moment.locale(detectedLang);
}
if (__PRELOADED_STATE__ === undefined) {
......
This diff is collapsed.
......@@ -14,7 +14,7 @@ import { I18nextProvider } from 'react-i18next';
import thunk from 'redux-thunk';
import rootReducer from 'reducers';
import languages from 'data/Languages';
import TimeAgo from 'javascript-time-ago';
import * as moment from 'moment';
// import { create as createJss } from 'jss';
// import jssPreset from 'jss-preset-default';
......@@ -70,7 +70,11 @@ const prerenderer = (html, errHtml) => (req, res) => {
function setLocale(locale) {
// console.log('Detected locale for SSR is', locale);
req.i18n.changeLanguage(locale);
import(`javascript-time-ago/locale/${locale}`).then((timeAgoLocale) => TimeAgo.locale(timeAgoLocale));
if (locale !== 'en') {
import(`moment/locale/${locale}`).then(() => moment.locale(locale));
} else {
moment.locale(locale);
}
}
function renderView() {
......
......@@ -2,8 +2,6 @@ import * as React from 'react';
import { WithTranslation, withTranslation } from 'react-i18next';
import * as moment from 'moment';
import TimeAgo from 'react-time-ago';
export default withTranslation()(function PrettyDate({
value,
t,
......@@ -19,7 +17,7 @@ export default withTranslation()(function PrettyDate({
return (
<span className="prettydate">
{ diffDays < 4 ?
<TimeAgo date={ valueTime } locale={ i18n.language }/>
moment(value).fromNow()
:
moment(value).format(withoutDay ? 'MMMM YYYY' : 'D MMMM YYYY')
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment