react-intl
Version:
Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.
110 lines (107 loc) • 3.7 kB
JavaScript
;
/*
HTML escaping is the same as React's
(on purpose.) Therefore, it has the following Copyright and Licensing:
Copyright 2013-2014, Facebook, Inc.
All rights reserved.
This source code is licensed under the BSD-style license found in the LICENSE
file in the root directory of React's source tree.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var intl_messageformat_1 = require("intl-messageformat");
var intl_format_cache_1 = require("intl-format-cache");
// Since rollup cannot deal with namespace being a function,
// this is to interop with TypeScript since `invariant`
// does not export a default
// https://github.com/rollup/rollup/issues/1267
var invariant_ = require("invariant");
var invariant = invariant_.default || invariant_;
var ESCAPED_CHARS = {
38: '&',
62: '>',
60: '<',
34: '"',
39: ''',
};
var UNSAFE_CHARS_REGEX = /[&><"']/g;
function escape(str) {
return ('' + str).replace(UNSAFE_CHARS_REGEX, function (match) { return ESCAPED_CHARS[match.charCodeAt(0)]; });
}
exports.escape = escape;
function filterProps(props, whitelist, defaults) {
if (defaults === void 0) { defaults = {}; }
return whitelist.reduce(function (filtered, name) {
if (props.hasOwnProperty(name)) {
filtered[name] = props[name];
}
else if (defaults.hasOwnProperty(name)) {
filtered[name] = defaults[name];
}
return filtered;
}, {});
}
exports.filterProps = filterProps;
function invariantIntlContext(intl) {
invariant(intl, '[React Intl] Could not find required `intl` object. ' +
'<IntlProvider> needs to exist in the component ancestry.');
}
exports.invariantIntlContext = invariantIntlContext;
function createError(message, exception) {
var eMsg = exception ? "\n" + exception.stack : '';
return "[React Intl] " + message + eMsg;
}
exports.createError = createError;
function defaultErrorHandler(error) {
if (process.env.NODE_ENV !== 'production') {
console.error(error);
}
}
exports.defaultErrorHandler = defaultErrorHandler;
exports.DEFAULT_INTL_CONFIG = {
formats: {},
messages: {},
timeZone: undefined,
textComponent: React.Fragment,
defaultLocale: 'en',
defaultFormats: {},
onError: defaultErrorHandler,
};
function createIntlCache() {
return {
dateTime: {},
number: {},
message: {},
relativeTime: {},
pluralRules: {},
};
}
exports.createIntlCache = createIntlCache;
/**
* Create intl formatters and populate cache
* @param cache explicit cache to prevent leaking memory
*/
function createFormatters(cache) {
if (cache === void 0) { cache = createIntlCache(); }
var RelativeTimeFormat = Intl.RelativeTimeFormat;
return {
getDateTimeFormat: intl_format_cache_1.default(Intl.DateTimeFormat, cache.dateTime),
getNumberFormat: intl_format_cache_1.default(Intl.NumberFormat, cache.number),
getMessageFormat: intl_format_cache_1.default(intl_messageformat_1.default, cache.message),
getRelativeTimeFormat: intl_format_cache_1.default(RelativeTimeFormat, cache.relativeTime),
getPluralRules: intl_format_cache_1.default(Intl.PluralRules, cache.pluralRules),
};
}
exports.createFormatters = createFormatters;
function getNamedFormat(formats, type, name, onError) {
var formatType = formats && formats[type];
var format;
if (formatType) {
format = formatType[name];
}
if (format) {
return format;
}
onError(createError("No " + type + " format named: " + name));
}
exports.getNamedFormat = getNamedFormat;