Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
const customFormats = {}
if (numberFormat) {
customFormats.number = numberFormat
}
let additionalOptions
if (timezone) {
// https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat#formatters
// customize formatters to be able to render dates to a specific timezone
additionalOptions = {
formatters: {
getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat),
getPluralRules: memoizeIntlConstructor(Intl.PluralRules),
getDateTimeFormat (locales, intlOptions) {
return {
format (date) {
if (!(date instanceof Date)) throw new Error('Invalid valid date passed to format')
const zone = momentTimezone.tz.zone(timezone)
if (!zone) throw new Error('Invalid timezone')
const clonedDate = new Date(date.getTime())
const fromOffset = clonedDate.getTimezoneOffset()
const toOffset = zone.parse(clonedDate)
const newDate = new Date(clonedDate.getTime() - (toOffset - fromOffset) * 60 * 1000)
return new Intl.DateTimeFormat(locales, intlOptions).format(newDate)
}
}
}
const customFormats = {}
if (numberFormat) {
customFormats.number = numberFormat
}
let additionalOptions
if (timezone) {
// https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat#formatters
// customize formatters to be able to render dates to a specific timezone
additionalOptions = {
formatters: {
getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat),
getPluralRules: memoizeIntlConstructor(Intl.PluralRules),
getDateTimeFormat (locales, intlOptions) {
return {
format (date) {
if (!(date instanceof Date)) throw new Error('Invalid valid date passed to format')
const zone = momentTimezone.tz.zone(timezone)
if (!zone) throw new Error('Invalid timezone')
const clonedDate = new Date(date.getTime())
const fromOffset = clonedDate.getTimezoneOffset()
const toOffset = zone.parse(clonedDate)
const newDate = new Date(clonedDate.getTime() - (toOffset - fromOffset) * 60 * 1000)
return new Intl.DateTimeFormat(locales, intlOptions).format(newDate)
} else if (defaults.hasOwnProperty(name)) {
opts[name] = defaults[name];
}
return opts;
}, {});
}
},
propTypes : typesSpec,
contextTypes : typesSpec,
childContextTypes: typesSpec,
getNumberFormat : intl$format$cache$$["default"](Intl.NumberFormat),
getDateTimeFormat: intl$format$cache$$["default"](Intl.DateTimeFormat),
getMessageFormat : intl$format$cache$$["default"](intl$messageformat$$["default"]),
getRelativeFormat: intl$format$cache$$["default"](intl$relativeformat$$["default"]),
getChildContext: function () {
var context = this.context;
var props = this.props;
return {
locales: props.locales || context.locales,
formats: props.formats || context.formats,
messages: props.messages || context.messages
};
},
formatDate: function (date, options) {
date = new Date(date);
assertIsDate(date, 'A date or timestamp must be provided to formatDate()');
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import IntlObj from 'intl';
import IntlMessageFormatObj from 'intl-messageformat';
import IntlRelativeFormatObj from 'intl-relativeformat';
import createFormatCacheObj from 'intl-format-cache';
import i18nJson from 'i18nJson';
/*
Intl libs are using out dated transpailer from ecmascript6.
* TODO: As soon as they fix it, remove this assignments!!!
* */
var Intl = window.Intl || IntlObj.default,
IntlMessageFormat = IntlMessageFormatObj.default,
IntlRelativeFormat = IntlRelativeFormatObj.default,
createFormatCache = createFormatCacheObj.default;
/*extract locale*/
var _locale = window.localStorage && localStorage.getItem('user_locale');
if (!_locale) {
if (window.navigator) {
_locale = navigator.language || navigator.userLanguage;
//For now removing the dashes from the language.
let indexOfDash = _locale.indexOf('-');
if (-1 !== indexOfDash) {
_locale = _locale.substr(0, indexOfDash);
}
}
if (!_locale) {
_locale = 'en';
}
if (obj.hasOwnProperty(name)) {
opts[name] = obj[name];
} else if (defaults.hasOwnProperty(name)) {
opts[name] = defaults[name];
}
return opts;
}, {});
}
},
propTypes : typesSpec,
contextTypes : typesSpec,
childContextTypes: typesSpec,
getNumberFormat : intl$format$cache$$["default"](Intl.NumberFormat),
getDateTimeFormat: intl$format$cache$$["default"](Intl.DateTimeFormat),
getMessageFormat : intl$format$cache$$["default"](intl$messageformat$$["default"]),
getRelativeFormat: intl$format$cache$$["default"](intl$relativeformat$$["default"]),
getChildContext: function () {
var context = this.context;
var props = this.props;
return {
locales: props.locales || context.locales,
formats: props.formats || context.formats,
messages: props.messages || context.messages
};
},
formatDate: function (date, options) {
opts[name] = obj[name];
} else if (defaults.hasOwnProperty(name)) {
opts[name] = defaults[name];
}
return opts;
}, {});
}
},
propTypes : typesSpec,
contextTypes : typesSpec,
childContextTypes: typesSpec,
getNumberFormat : intl$format$cache$$["default"](Intl.NumberFormat),
getDateTimeFormat: intl$format$cache$$["default"](Intl.DateTimeFormat),
getMessageFormat : intl$format$cache$$["default"](intl$messageformat$$["default"]),
getRelativeFormat: intl$format$cache$$["default"](intl$relativeformat$$["default"]),
getChildContext: function () {
var context = this.context;
var props = this.props;
return {
locales: props.locales || context.locales,
formats: props.formats || context.formats,
messages: props.messages || context.messages
};
},
formatDate: function (date, options) {
date = new Date(date);
opts[name] = defaults[name];
}
return opts;
}, {});
}
},
propTypes : typesSpec,
contextTypes : typesSpec,
childContextTypes: typesSpec,
getNumberFormat : intl$format$cache$$["default"](Intl.NumberFormat),
getDateTimeFormat: intl$format$cache$$["default"](Intl.DateTimeFormat),
getMessageFormat : intl$format$cache$$["default"](intl$messageformat$$["default"]),
getRelativeFormat: intl$format$cache$$["default"](intl$relativeformat$$["default"]),
getChildContext: function () {
var context = this.context;
var props = this.props;
return {
locales: props.locales || context.locales,
formats: props.formats || context.formats,
messages: props.messages || context.messages
};
},
formatDate: function (date, options) {
date = new Date(date);
assertIsDate(date, 'A date or timestamp must be provided to formatDate()');
return this._format('date', date, options);