2018-02-08 18:50:19 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2018-02-15 19:01:54 +00:00
|
|
|
import i18next from './i18next';
|
2018-02-08 18:50:19 +00:00
|
|
|
|
2018-08-01 16:41:54 +00:00
|
|
|
// allows for moment durations to be formatted
|
|
|
|
import 'moment-duration-format';
|
|
|
|
|
2018-02-08 18:50:19 +00:00
|
|
|
// MomentJS uses static language bundle loading, so in order to support dynamic
|
|
|
|
// language selection in the app we need to load all bundles that we support in
|
|
|
|
// the app.
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/af');
|
|
|
|
require('moment/locale/ar');
|
|
|
|
require('moment/locale/be');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/bg');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/ca');
|
|
|
|
require('moment/locale/cs');
|
|
|
|
require('moment/locale/da');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/de');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/el');
|
|
|
|
require('moment/locale/en-gb');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/eo');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/es-us');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/es');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/et');
|
|
|
|
require('moment/locale/eu');
|
2020-12-23 14:29:07 +00:00
|
|
|
require('moment/locale/fa');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/fi');
|
|
|
|
require('moment/locale/fr-ca');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/fr');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/gl');
|
|
|
|
require('moment/locale/he');
|
|
|
|
require('moment/locale/hr');
|
|
|
|
require('moment/locale/hu');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/hy-am');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/id');
|
|
|
|
require('moment/locale/is');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/it');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/ja');
|
|
|
|
require('moment/locale/ko');
|
|
|
|
require('moment/locale/lt');
|
|
|
|
require('moment/locale/lv');
|
|
|
|
require('moment/locale/ml');
|
|
|
|
require('moment/locale/mn');
|
|
|
|
require('moment/locale/mr');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/nb');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/nl');
|
|
|
|
require('moment/locale/oc-lnc');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/pl');
|
|
|
|
require('moment/locale/pt');
|
|
|
|
require('moment/locale/pt-br');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/ro');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/ru');
|
|
|
|
require('moment/locale/sk');
|
|
|
|
require('moment/locale/sl');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/sr');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/sv');
|
|
|
|
require('moment/locale/tr');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/uk');
|
|
|
|
require('moment/locale/vi');
|
2018-02-08 18:50:19 +00:00
|
|
|
require('moment/locale/zh-cn');
|
2021-03-04 14:20:27 +00:00
|
|
|
require('moment/locale/zh-tw');
|
2018-02-08 18:50:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a localized date formatter initialized with a specific {@code Date}
|
2018-02-15 19:01:54 +00:00
|
|
|
* or timestamp ({@code number}).
|
2018-02-08 18:50:19 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Date | number} dateOrTimeStamp - The date or unix timestamp (ms)
|
|
|
|
* to format.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export function getLocalizedDateFormatter(dateOrTimeStamp: Date | number) {
|
|
|
|
return moment(dateOrTimeStamp).locale(_getSupportedLocale());
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:33:03 +00:00
|
|
|
/**
|
|
|
|
* Returns a localized duration formatter initialized with a
|
|
|
|
* specific duration ({@code number}).
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {number} duration - The duration (ms)
|
|
|
|
* to format.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export function getLocalizedDurationFormatter(duration: number) {
|
2018-06-04 21:02:37 +00:00
|
|
|
// FIXME The flow-type definition of moment is v2.3 while our package.json
|
|
|
|
// states v2.19 so maybe locale on moment's duration was introduced in
|
|
|
|
// between?
|
|
|
|
//
|
2018-08-01 16:41:54 +00:00
|
|
|
|
|
|
|
// If the conference is under an hour long we want to display it without
|
|
|
|
// showing the hour and we want to include the hour if the conference is
|
|
|
|
// more than an hour long
|
|
|
|
|
2022-08-01 07:14:54 +00:00
|
|
|
// @ts-ignore
|
2018-08-01 16:41:54 +00:00
|
|
|
if (moment.duration(duration).format('h') !== '0') {
|
2022-08-01 07:14:54 +00:00
|
|
|
// @ts-ignore
|
2018-08-01 16:41:54 +00:00
|
|
|
return moment.duration(duration).format('h:mm:ss');
|
|
|
|
}
|
|
|
|
|
2022-08-01 07:14:54 +00:00
|
|
|
// @ts-ignore
|
2018-08-01 16:41:54 +00:00
|
|
|
return moment.duration(duration).format('mm:ss', { trim: false });
|
2018-02-13 23:33:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:50:19 +00:00
|
|
|
/**
|
|
|
|
* A lenient locale matcher to match language and dialect if possible.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function _getSupportedLocale() {
|
|
|
|
const i18nLocale = i18next.language;
|
|
|
|
let supportedLocale;
|
|
|
|
|
|
|
|
if (i18nLocale) {
|
|
|
|
const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
|
|
|
|
const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
|
|
|
|
|
|
|
|
if (localeResult) {
|
|
|
|
const currentLocaleRegexp
|
|
|
|
= new RegExp(
|
|
|
|
`^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
|
|
|
|
|
|
|
|
supportedLocale
|
2018-06-04 21:02:37 +00:00
|
|
|
|
|
|
|
// FIXME The flow-type definition of moment is v2.3 while our
|
|
|
|
// package.json states v2.19 so maybe locales on moment was
|
|
|
|
// introduced in between?
|
2018-02-08 18:50:19 +00:00
|
|
|
= moment.locales().find(lang => currentLocaleRegexp.exec(lang));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return supportedLocale || 'en';
|
|
|
|
}
|